并查集+欧拉回路+字典树 Colored Sticks POJ 2513
来源:互联网 发布:linux shell 捕获错误 编辑:程序博客网 时间:2024/06/05 06:26
输入多组数据,每组数据两种颜色,表示一根木头两端的颜色,现在要将这些木头相连,要求相连部分颜色相同,问能否全部连通
提示
1)一个要判断所有的木头是否在一个集合中,即是否能相连
2)判断一种颜色出现的数量
3)一棵树如果只有0或2个点出现次数为奇数,则树可以一笔画成
#include <stdio.h>#include <string.h>#define maxn 500005int tot;int f[maxn];int num[maxn];struct trie{trie *next[30];int id;trie(){for(int i=0;i<30;i++)next[i]=NULL;id=0;}};trie *root;int find(int i){if(i==f[i])return f[i];f[i]=find(f[i]);return f[i];}int build(char *str) //建立字典树{int len=strlen(str);trie *p=root;for(int i=0;i<len;i++){int id=str[i]-'a';if(p->next[id]==NULL)p->next[id]=new trie;p=p->next[id];}if(p->id==0)p->id=tot++;return p->id;}int main(){char str1[50];char str2[50];int a,b;int fr,ed;int i;for(i=0;i<maxn;i++){f[i]=i;num[i]=0;}tot=1;int tt=0,ff=0;root=new trie;while(scanf("%s%s",str1,str2)!=-1){a=build(str1);b=build(str2);fr=find(a);ed=find(b);if(fr!=ed)f[fr]=ed; //集合num[a]++; //统计出现次数num[b]++;}for(i=1;i<tot;i++)if(num[i]%2==1)tt++;for(i=1;i<tot;i++)if(f[i]==i)ff++;if(((tt==0)||(tt==2))&&(ff==1||ff==0))printf("Possible\n");elseprintf("Impossible\n");return 0;}
0 0
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- POJ 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- poj 2513 Colored Sticks 欧拉回路(字典树 +并查集)
- POJ 2513 Colored sticks(字典树 + 欧拉回路 + 并查集)
- POJ 题目2513 Colored Sticks(字典树+并查集+欧拉回路)
- POJ 2513 - Colored Sticks(字典树+欧拉回路+并查集 = 好题!)
- POJ 2513 Colored Sticks(字典树+并查集+欧拉回路)
- poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)
- POJ 2513 Colored Sticks(欧拉回路+字典树+并查集)
- 连单词成欧拉路 欧拉回路+字典树+并查集 POJ 2513 Colored Sticks
- poj-2513-Colored Sticks-字典树trie / hash +并查集 欧拉回路
- POJ Colored Sticks 2513(字典树+并查集+欧拉回路)
- POJ 2513 Colored Sticks——字典树+并查集+欧拉回路
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- oracle 知识点
- ZOJ 2523 —— Number of People(数论,胡搞)
- SONY VAIO 触摸板使用
- java switch语句的参数类型(转帖)
- 记一次IBM面试经历
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- POJ 3286 How many 0's?
- 陈老师的多校联合20140816A题||spoj 10228 动态规划
- Linux系统中存储设备的两种表示方法
- 07-2. A+B和C (15)
- leetcode Distinct Subsequences
- MeasureSpec介绍及使用详解 ViewGroup.getChildMeasureSpec(0,0,0)
- 以此纪念开通博客
- Android_Fragment 学习进阶(一)