POJ/PKU 2513 并查集+字典树+欧拉回路
来源:互联网 发布:什么是阿里云 编辑:程序博客网 时间:2024/05/21 00:56
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 20750 Accepted: 5482
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
要仔细看题,木棍不仅仅只是可以一个方向放置,还可以倒过来,所以简单的说,这个题是一个无向图的欧拉回路判断
但是直接用map要超时。。我先开始就这样TLE了。后来看了下后面的discuss,发现要用字典树
于是又自己手动写了个字典树。
所以综上,我们需要:
1、字典树对每一个字符串进行标号
2、并查集判断整个图是否连通
3、判断无向图的欧拉回路是否存在
我的代码:
#include<stdio.h>#include<string.h>#include<algorithm>#define maxn 500005using namespace std;struct node{int num;node *next[26];void init();};node *root;int father[maxn];int temp[maxn];int sum[maxn];int cnt=0;void node::init(){this->num=0;}int find(int p){if(father[p]==p)return p;return father[p]=find(father[p]);}void Union(int a,int b){int x,y;x=find(a);y=find(b);if(x!=y)father[x]=y;}void init(){root= new node;int i;for(i=0;i<26;i++)root->next[i]=NULL;root->init();}int search(char *s){int i,len=strlen(s),j;node *p,*t;p=root;for(i=0;i<len;i++){if(p->next[s[i]-'a']==NULL){t=new node;for(j=0;j<26;j++)t->next[j]=NULL;t->init();p->next[s[i]-'a']=t;}p=p->next[s[i]-'a'];}if(p->num==0){cnt++;return p->num=cnt;}elsereturn p->num;}int main(){int i,a,b;char s1[15],s2[15];memset(sum,0,sizeof(sum));for(i=1;i<=maxn;i++)father[i]=i;init();while(scanf("%s%s",s1,s2)!=EOF){a=search(s1);b=search(s2);Union(a,b);sum[a]++;sum[b]++;}int NUM=0;for(i=1;i<=cnt;i++)if(father[i]==i)NUM++;if(NUM>1){printf("Impossible\n");return 0;}NUM=0;for(i=1;i<=cnt;i++)if(sum[i]&1)NUM++;if(NUM==0||NUM==2)printf("Possible\n");elseprintf("Impossible\n");return 0;}
- POJ/PKU 2513 并查集+字典树+欧拉回路
- poj 2513 欧拉回路+并查集+字典树
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
- 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 +并查集 欧拉回路
- 强烈推荐Mac下数据恢复软件Data rescue 3.2
- 3.事件冒泡、表单提交4种方法0808
- oracle查找前N条记录
- Warning: Call-time pass-by-reference has been deprecated。。。的问题
- DYNAM or NODYNAM compiler option
- POJ/PKU 2513 并查集+字典树+欧拉回路
- POJ1847 dijkstra 水题
- 关于IO的同步,异步,阻塞,非阻塞
- rainlist
- lua学习(三)--------lua调用c++函数和简单的错误处理
- android 编译问题
- Perl中的grep和map
- android的5个进程等级
- 说说union