poj 2513 欧拉路+Trie树+并查集
来源:互联网 发布:软件测试作业 编辑:程序博客网 时间:2024/05/22 15:38
传送门
题意:很多棒棒,首位有颜色,如果一个棒棒的首颜色和另一个棒棒的尾颜色相同,则可以连接在一起,问所以棒棒能否连成一线。
个人心得:通过这题学习了欧拉路,知道了用Trie把字符串映射成数字。
思路:把颜色看作节点,把棒看成边,图就建完了,之后看是否在一个连通图中,这里用并查集实现,如果在,再判断是否构成欧拉路,是就也能了。
欧拉路条件:图连通并且入度为奇数的点只有0个或2个。
Trie树映射,不会的可以学习下,据说map很慢。
吐槽:各自变量什么的都开大点,不然RE。。。。。。。。
吐槽(与本题无关):今天出去做题,被各种大牛虐的很惨,心情很不爽,励志要发愤涂墙!。。。。。。。
#include<iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;struct node{ int c[27]; int id;}t[5000000];int num,tnum,in[550005];int f[550005];int trie(char *a){ int l=strlen(a); int p=0; for(int i=0;i<l;i++) { if(t[p].c[a[i]-'a']==0) { t[p].c[a[i]-'a']=++tnum; memset(t[tnum].c,0,sizeof(t[tnum].c)); p=tnum; t[p].id=0; } else { p=t[p].c[a[i]-'a']; } } if(t[p].id==0) { t[p].id=++num; f[num]=num; } return t[p].id;}int find(int i){ if(f[i]==i)return i; f[i]=find(f[i]); return f[i];}void un(int u,int v){ int x=find(u); int y=find(v); if(x<y)f[y]=x; else f[x]=y;}int main(){ num=0; tnum=0; memset(in,0,sizeof(in)); char a[30],b[30]; memset(t[0].c,0,sizeof(t[0].c)); while(scanf("%s%s",a,b)!=EOF) { int u=trie(a); int v=trie(b); un(u,v); in[u]++; in[v]++; } int flag=0; int oddnum=0; for(int i=1;i<=num;i++) { if(f[i]!=f[1]) { flag=1; break; } if(in[i]%2==1)oddnum++; } if(flag)cout<<"Impossible"<<endl; else if(oddnum==0||oddnum==2)cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; return 0;}
- POJ 2513 trie+并查集+欧拉路
- POJ 2513 Trie树+并查集
- poj 2513 欧拉路+Trie树+并查集
- poj 2513 并查集+trie,欧拉图
- POJ-2513 Colored Sticks【并查集+Trie+欧拉路】
- POJ 2513 欧拉通路+并查集+trie树
- poj 2513 trie树+并查集+欧拉回路
- poj 2513 Colored Sticks trie树+欧拉图+并查集
- POJ 2513 Colored Sticks (Trie树+并查集+欧拉路)
- poj 2513 trie树+欧拉路判断+并查集=(首尾相接的木棍)
- POJ 2513 trie树+并查集判断无向图的欧拉路
- poj2513(trie树+欧拉路+并查集)
- POJ-2513(trie+并查集+欧拉回路)
- poj 2513(trie树+并查集+欧拉回路条件)(记得要初始化指针数组)
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- poj 2513 欧拉回路+并查集判断是否联通+Trie树
- [ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)
- POJ 2513 Colored Sticks (并查集 Trie树 欧拉回路)
- Mysql alter语法
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
- 新解法之翻转句子中单词的顺序(不用两次翻转,用两个指针)
- HDU4682 The Happy Triangles
- 修改eclipse的背景色
- poj 2513 欧拉路+Trie树+并查集
- uva 11800 - Determine the Shape
- 《sort帮你排序》-linux命令五分钟系列之二十六
- hdu 4587
- UVa11995 I Can Guess the Data Structure!
- do while(0)在宏定义中的应用
- jdk和tomcat的配置
- hdoj 3400 三分
- 努力改正缺点