poj 2513 Colored Sticks Trie树
2015-07-04 02:50
246 查看
Colored Sticks
Description
You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?
Input
Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.
Output
If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.
Sample Input
Sample Output
Hint
Huge input,scanf is recommended.
Source
The UofA Local 2000.10.14
给出一些木棍两头都有颜色,问是否能连成一条线,欧拉路径+并查集+trie数
Time Limit: 5000MS | Memory Limit: 128000K | |
Total Submissions: 32172 | Accepted: 8493 |
You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?
Input
Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.
Output
If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.
Sample Input
blue red red violet cyan blue blue magenta magenta cyan
Sample Output
Possible
Hint
Huge input,scanf is recommended.
Source
The UofA Local 2000.10.14
给出一些木棍两头都有颜色,问是否能连成一条线,欧拉路径+并查集+trie数
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; #define Max 500010 struct node { int x; node *next[26]; }; int degree[Max],fa[Max],cnt,ant; node *root , mem[Max*10]; node * create() { node * p =&mem[ant++]; for(int i=0;i<26;i++) { p->next[i]=NULL; } return p; } void insert(char *s,int x) { node * p =root; int k; for(int i=0;i<strlen(s);i++) { k=s[i]-'a'; if(p->next[k]==NULL) { p->next[k]=create(); } p=p->next[k]; } p->x=x; } int search(char *s) { node *p = root; int k; for(int i=0;i<strlen(s);i++) { k=s[i]-'a'; if(p->next[k]==NULL) {return 0;} p=p->next[k]; } return p->x; } void init() { for(int i=1;i<=Max;i++) { fa[i]=i; } memset(degree,0,sizeof(degree)); } int find(int x) { if(x!=fa[x]) { fa[x]=find(fa[x]); } return fa[x]; } void Union(int a,int b) { fa[a]=b; } bool judge() { int u=0; for(int i=1;i<=cnt;i++) { u+=degree[i]%2; } if(u>2||u==1) return false; u=find(1); for(int i=2;i<=cnt;i++) { if(find(i)!=u) {cout<<i;return false;} } return true; } int main() { int x,y; int xx,yy; char s1[15],s2[15]; init(); root = create(); cnt =0 ; while(~scanf("%s %s",s1,s2)) { x=search(s1); y=search(s2); if(x==0) insert(s1,x = ++cnt); if(y==0) insert(s2,y = ++cnt); degree[x]++; degree[y]++; xx=find(x); yy=find(y); if(xx!=yy) { Union(xx,yy); } } if(judge()) cout<<"Possible"; else cout<<"Impossible"; return 0; }
相关文章推荐
- java中FileOutputStream和FileInputStream类用法(总结的挺好)
- vim中输入tab符
- 博客创建
- httpclient请求方式下的泛型json转换问题
- Cocos2d-x 的内存管理
- 人工智能领域中声源定位的研究与发展------第二章 声源定位系统 (6)
- C#代码读写XML
- maven踩坑
- 1004--Java集合--Set 接口
- 数据结构基础温故-2.栈
- 快讯:阳光动力2号胜利抵达美国夏威夷
- 看了奇点大学的总决赛相关的视频,一些感悟
- UVa 11384 Help is needed for Dexter 正整数序列
- int ,long , long long类型的范围
- java实现下雪雪花飘落并堆积效果
- 【leetcode】Distinct Subsequences
- BZOJ 2145 悄悄话
- 人工智能领域中声源定位的研究与发展------第二章 声源定位系统 (5)
- Ubuntu nginx 配置404错误页面
- Error parsing XML: not well-formed (invalid token) 报错+R文件消失解决办法