poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
题目:http://poj.org/problem?id=2513
参考博客:http://blog.csdn.net/lyy289065406/article/details/6647445
http://www.cnblogs.com/LK1994/p/3263462.html
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<stack> 6 #include<queue> 7 #include<iomanip> 8 #include<cmath> 9 #include<map> 10 #include<vector> 11 #include<algorithm> 12 #define N 500010 13 using namespace std; 14 15 int degree[N],bin[N],id = 1; 16 17 struct tree 18 { 19 int flag,id; 20 struct tree* next[26]; 21 }*root; 22 struct tree* creat() 23 { 24 struct tree *p=(struct tree*)malloc(sizeof(struct tree)); 25 p->flag =0; 26 for(int i = 0; i < 26; i++) 27 p->next[i] = NULL; 28 return p; 29 } 30 31 int find(int x) 32 { 33 if(bin[x] != x) 34 bin[x] = find(bin[x]); 35 return bin[x]; 36 } 37 38 int hash(char s[]) 39 { 40 struct tree *p = root; 41 for(int i = 0; s[i]; i++) 42 { 43 if(p->next[s[i]-'a'] == NULL) 44 p->next[s[i]-'a'] = creat(); 45 p = p->next[s[i]-'a']; 46 } 47 if(p->flag != 1) 48 { 49 p->flag = 1; 50 p->id = id++; 51 } 52 return p->id; 53 } 54 55 int check() 56 { 57 int sum = 0; 58 int x = find(1); 59 for(int i = 2; i < id; i++) 60 if(find(i) != x) 61 return 0; 62 for(int i = 1; i < id; i++) 63 { 64 if(degree[i]%2) 65 sum++; 66 } 67 if(sum == 0 || sum == 2) 68 return 1; 69 return 0; 70 } 71 72 int main() 73 { 74 int u,v,x,y; 75 char s1[15],s2[15]; 76 memset(degree,0,sizeof(degree)); 77 for(int i=1; i<=500000; i++) 78 bin[i]=i; 79 root=creat(); 80 while(scanf("%s %s",s1,s2)!=EOF) 81 { 82 u =hash(s1); v =hash(s2); 83 degree[u]++; degree[v]++; 84 x=find(u); 85 y=find(v); 86 if(x!=y) 87 bin[x]=y; 88 } 89 if(check()) printf("Possible\n"); 90 else printf("Impossible\n"); 91 return 0; 92 }
转载于:https://www.cnblogs.com/bfshm/p/3279871.html
poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)相关推荐
- POJ - 2513 Colored Sticks(字典树+并查集+欧拉回路)
题目链接:点击查看 题目大意:给出n个木棍,问若两两相连,最终能否构成一根长直木棍,相连的规则是两个木棍的相接端点的颜色需要保持相同 题目分析:关于这个题目,我们可以将每个木棍视为一条边,每个木棍的两 ...
- 欧拉路径 之 poj 2513 Colored Sticks
/* 欧拉路径 之 poj 2513 Colored Sticks欧拉路径: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径.无向图存在欧拉路径 充要条件:1) 图是连通的 ...
- POJ - 2513 Colored Sticks 欧拉通路+并查集+静态树
一开始想用map来搞,但是感觉好复杂,然后想了一下看大佬们用trie做的,感觉十分合理就敲了一发. 一开始re,数组要开到550000 只会静态的字典树,在每个根节点看是否出现过改颜色,如果没有就把该 ...
- POJ 2513 Colored Sticks
POJ_2513 这个题目实际上要求木棍能够组成一个欧拉道路即可.为了练字典树所以用字典树写的,当然,这个题目用哈希做也可以. 需要注意的是由于木棍有25W,因此不同的顶点最多有50W,所以存储图顶点 ...
- POJ-2513 Colored Sticks 字典树,欧拉回路
这题用map就超时了,所以用字典树来优化,第一次写静态的,现在都不习惯用指针了. 由于这里不要回到源点,所以不许要所有点的度都为偶数,零个或者两个均可,图也必须是连通的. 代码如下: #include ...
- poj 2513 Colored Sticks
// 判断图是否联通 在连通的基础上还要判断是否存在欧拉通路// 判断连通就并查集了 判断是否存在欧拉通路: 点度数为数的点 ==1 >=3就是不存在的 其它是存在的// 我开始用 map 判重 ...
- 数据结构之树的应用:并查集
树的应用:并查集 并查集的概念: 三种基本操作: 例: 代码实现: 并查集的概念: 将所有的数据元素放在一个集合中,将集合分成若干个互不相交的子集,每一个子集对应一颗树,所有的自己组成森林. 三种基本 ...
- POJ_2513Colored Sticks 字典树+
比较考察技术含量的一道题. 参考链接:http://blog.csdn.net/lyy289065406/article/details/6647445 题目链接:http://poj.org/pro ...
- [poj 2001] Shortest Prefixes (字典树)
Language: Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20503 Accepte ...
最新文章
- TCP三次握手、糊涂窗口、粘包问题
- EXE与SYS通信(缓冲模式)
- 【Java入门】package包
- 快速幂算法相关题目(Leetcode题解-Python语言)
- html5+php调用android手机图片,html5+exif.js+canvas+php实现手机上传图片,图片损坏无法打开...
- enum mysql byte_九、臭名昭著的 MySQL ENUM 类型 ( 上 )
- 【OpenCV】OpenCV实战从入门到精通之 -- 图像对比度、亮度值调整
- c语言指针代码大全,C语言之指针(示例代码)
- 【AD】AD19/20笔记及快捷键
- jQuery常用工具方法
- QT实现Linux下系统监控小工具
- 点云 数据 (偏向于研究大小)
- 谷歌十大高薪职位:首席软件工程师居首
- ES根据日期查询数据
- cocoStudio工具的使用-----场景编辑器
- BWA处理WES文件
- 关于ABR、CBR、VBR Twitch的工程师如是说
- ERP综合竞争力排名价值几何?
- CF1444C Team-Building 可撤销种类并查集
- 姑苏城外寒山寺,夜半钟声到客船