poj 2513 Colored Sticks (无向欧拉路+字典树)
2017-11-01 20:29
387 查看
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.
给出m对数 判断是否构成欧拉路(所有边当且仅遍历一遍),当奇数度数大于2时不成立;成立条件:一条直线,或者环,或者直线加环;
有向为(有一个入度-出度=1,出度-入度=1);
用map(内部排序)标记超时,poj不支持unordered_map,所以用字典树优化
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.
给出m对数 判断是否构成欧拉路(所有边当且仅遍历一遍),当奇数度数大于2时不成立;成立条件:一条直线,或者环,或者直线加环;
有向为(有一个入度-出度=1,出度-入度=1);
用map(内部排序)标记超时,poj不支持unordered_map,所以用字典树优化
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <map> #include <queue> #include <string> using namespace std; const int N = 500000+5; char s1[30], s2[30]; int degree , f ; struct node { int id; node *next2[30]; node(){ id=0; for(int i=0;i<26;i++) next2[i]=NULL; } }; node *root; int k; int getid(char *s) { int len=strlen(s); node *p=root; node *tmp=NULL; for(int i=0;i<len;i++) { if(!p->next2[s[i]-'a']) { tmp=new node; p->next2[s[i]-'a']=tmp; } p=p->next2[s[i]-'a']; } if(p->id==0) p->id=++k; return p->id; } int getf(int x) { if(x!=f[x]) f[x]=getf(f[x]); return f[x]; } void unio(int x,int y) { int t1=getf(x), t2=getf(y); if(t1!=t2) f[t1]=t2; return ; } int main() { k=0; root=new node; for(int i=0; i<N; i++) f[i]=i; while(scanf("%s %s", s1, s2)!=EOF) { int id1=getid(s1), id2=getid(s2); degree[id1]++,degree[id2]++; unio(id1,id2); } int s=getf(1),num=0; for(int i=1; i<k; i++) { if(num>2) { puts("Impossible"); return 0; } if(getf(i)!=s) { puts("Impossible"); return 0; } if(degree[i]%2==1) num++; } if(num>2) puts("Impossible"); else puts("Possible"); return 0; }
相关文章推荐
- POJ 2513 Colored Sticks 欧拉路的判断+字典树
- POJ_2513_Colored Sticks(欧拉路+字典树)
- POJ 2513 Colored Sticks(字典树+无向图欧拉路)
- POJ 2513 Colored Sticks 欧拉路+字典树
- POJ2513 Colored Sticks 欧拉路+字典树标号
- POJ 2513--Colored Sticks【字典树编号 && 并查集判连通 && 无向图欧拉路】
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- poj2513 Colored Sticks(欧拉回路判断+字典树)
- POJ 2513Colored Sticks 并查集,字典树
- POJ 2513 Colored Sticks (排序+并查集判断欧拉路)
- POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
- POJ 2513 - Colored Sticks(字典树+欧拉回路+并查集 = 好题!)
- POJ 2513 Colored Sticks 好题 字典树+并查集+欧拉路
- POJ 2513 && NYOJ 230 Colored Sticks (并查集 && 字典树 && 欧拉图)
- poj 2513 Colored Sticks (字典树 + 并查集)
- POJ 2513 Colored Sticks 字典树 + 并查集 + 欧拉通路
- POJ 2513-Colored Sticks (字典树)
- POJ 2513 Colored Sticks(字典树+欧拉回路)
- POJ 2513 Colored Sticks (Tire+欧拉路+并查集)
- POJ 2513 Colored Sticks(字典树 + 并查集 + 欧拉回路)