Question link:
Http://poj.org/problem? Id = 2513
Colored sticks
Time limit:5000 Ms |
|
Memory limit:128000 K |
Total submissions:30955 |
|
Accepted:8159 |
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 blue redred violetcyan blueblue magentamagenta cyan Sample output Possible Hint Huge input, scanf is recommended.Source The uofa local 2000.10.14 |
[Submit] [Go Back] [Status] [discuss]
Question meaning:
There are n wooden sticks, each of which has two colors at both ends. check whether these sticks can be together so that the backend color of the former wooden stick is the same as the front-end color of the former wooden stick.
Solution:
Euler path + dictionary tree + query set
Note that it is a undirected edge. Pay attention to the connectivity judgment. The direct use of map ing will time out and write the dictionary tree by yourself.
Code:
//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 550000int dei[Maxn],a[Maxn],cnt,la;struct Node{ struct Node * next[30]; int nu;}node[Maxn*15],root;int Insert(char *a){ Node * p=&root; while(*a) { if(p->next[*a-'a'+1]==NULL) { node[++la].nu=0; memset(node[la].next,NULL,sizeof(node[la].next)); p->next[*a-'a'+1]=&node[la]; } p=p->next[*a-'a'+1]; a++; } if(!p->nu) p->nu=++cnt; return p->nu;}int Find(int x){ int temp=x; while(x!=fa[x]) x=fa[x]; while(fa[temp]!=x) { int cur=fa[temp]; fa[temp]=x; temp=cur; } return x;}void Unio(int a,int b){ a=Find(a),b=Find(b); if(a!=b) fa[a]=b;}int main(){ memset(dei,0,sizeof(dei)); string a,b; char sa[15],sb[15]; cnt=la=0; for(int i=1;i<=Maxn-5000;i++) fa[i]=i; memset(root.next,NULL,sizeof(root.next)); root.nu=0; while(~scanf("%s%s",sa+1,sb+1)) { int a=Insert(sa+1); int b=Insert(sb+1); dei[a]++,dei[b]++; Unio(a,b); } bool ans=true; int nui=0,nuo=0,la=-1; for(int i=1;i<=cnt;i++) { if(la==-1) la=Find(i); else if(la!=Find(i)) { ans=false; break; } if(dei[i]&1) nui++; } if(!ans||nui>2||nui==1) printf("Impossible\n"); else printf("Possible\n"); //system("pause"); return 0;}
[Euler Loop] poj 2513 colored sticks