测试赛A - Colored Sticks(并查集+字典树+欧拉回路)
来源:互联网 发布:网易公开课英语知乎 编辑:程序博客网 时间:2024/06/11 15:44
A - Colored Sticks
Time Limit:5000MS Memory Limit:128000KB 64bit IO Format:%I64d & %I64uDescription
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.
使用字典树来给每个颜色定一个序号,并统计出现的次数,使用并查集判断是否全部的点联通,在用欧拉回路,看整个图是不是可以一次走完,
判断方法,如果奇数度数的点为0或2个,那么一定可以走通
#include <cstdio>#include <cstring>int p[520000] , top , q[520000];struct node{ int flag ; node *next[27] ;} *head;node *newnode(){ node *p = new node ; p->flag = 0; for(int i = 0 ; i < 27 ; i++) p->next[i] = NULL ; return p;}int gettree(node *head,char *s){ int i , l = strlen(s) ; node *p = head ; for(i = 0 ; i < l ; i++) { int k = s[i] - 'a' ; if(p->next[k]==NULL) p->next[k] = newnode(); p = p->next[k] ; } if( p->flag == 0 ) p->flag = top++ ; return p->flag ;}int f(int x){ int r , k , l ; r = x ; while( r != p[r] ) r = p[r] ; k = x ; while( k != r ) { l = p[k] ; p[k] = r ; k = l ; } return r ;}void add(int u,int v){ u = f(u) ; v = f(v) ; if( u != v ) p[u] = v ;}char s1[11] , s2[11] ;int main(){ int i , n , k1 , k2 ; memset(q,0,sizeof(q)); head = newnode() ; for(i = 0 ; i <= 520000 ; i++) p[i] = i ; top = 1 ; while( scanf("%s %s", s1, s2 ) !=EOF ) { k1 = gettree(head,s1); k2 = gettree(head,s2); q[k1]++ ; q[k2]++ ; add(k1,k2) ; } int k = f(1) ; for(i = 2 ; i < top ; i++) if( k != f(i) ) break; if( i < top ) printf("Impossible\n"); else { int ji = 0 ; for(i = 1 ; i < top ; i++) if( q[i]%2 ) ji++ ; if(ji ==0 || ji == 2) printf("Possible\n"); else printf("Impossible\n"); } return 0;}
0 0
- 测试赛A - Colored Sticks(并查集+字典树+欧拉回路)
- Colored Sticks --欧拉回路,并查集,字典树,
- POJ 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)
- poj 2513 Colored Sticks 欧拉回路(字典树 +并查集)
- POJ 2513 Colored sticks(字典树 + 欧拉回路 + 并查集)
- POJ 题目2513 Colored Sticks(字典树+并查集+欧拉回路)
- POJ 2513 Colored Sticks(欧拉回路+字典树+并查集)
- Colored Sticks(欧拉回路+字典树+并查集)
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- POJ 2513 - Colored Sticks(字典树+欧拉回路+并查集 = 好题!)
- POJ 2513 Colored Sticks(字典树+并查集+欧拉回路)
- poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)
- POJ2513 Colored Sticks(字典树+并查集+欧拉回路)
- SAS学习笔记之PROC FORMAT
- Linux 知识点
- PHP 判断时间区间
- SICA手机pos机警惕骗子借知名电视节目行骗
- 在APP中集成iAd Banner展示广告盈利
- 测试赛A - Colored Sticks(并查集+字典树+欧拉回路)
- 转载_C语言的inline
- FreeRTOS随记
- tomcat部署配置环境变量
- 【Swift】学习笔记(一)——初识 基础数据类型,代码风格,元组,断言
- Android中用Application类实现全局数据变量的使用
- Developer Tips for Android
- springmvc3.2最少jar包
- 100行代码实现最简单的基于FFMPEG+SDL的视频播放器