pku 2513 Colored Sticks(字典树+并查集)
来源:互联网 发布:如何才能学好编程 编辑:程序博客网 时间:2024/04/30 00:40
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 27266 Accepted: 7211
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
题意:给n条棍,每条棍2边有颜色,若2条棍的一边颜色相同则可以连起来,问是否能将所有棍连在一起
题解:可以把没种颜色看作一个点,然后这就是一个欧拉路问题(一笔画问题)。用字典树来分配并对应其颜色的下标,用并查集来判是否连通图,然后易知有奇数条边相连的点不能超过2个则存在欧拉路
#include<stdio.h>#include<string.h>struct tree{ int next[26],data;}tire[1000005];int v[1000005],cnt[1000005];int cou=0,cou2=1;int fin(int x){ if(x==v[x]) return x; return v[x]=fin(v[x]);}int inter(char *s){ int p=0; while(*s) { if(tire[p].next[*s-'a']==-1) { tire[p].next[*s-'a']=cou; p=cou++; } else p=tire[p].next[*s-'a']; s++; } if(tire[p].data!=-1) return tire[p].data; v[cou2]=cou2; return tire[p].data=cou2++;}int main(){ char a[13],b[13]; int x,y,i; memset(tire,-1,sizeof(tire)); memset(cnt,0,sizeof(cnt)); while(scanf("%s%s",a,b)>0) { x=inter(a),y=inter(b); cnt[x]++,cnt[y]++; x=fin(x),y=fin(y); if(x!=y) v[x]=y; } x=y=0; for(i=1;i<cou2;i++) { if(cnt[i]&1) x++; if(fin(i)==i) y++; if(y>1||x>2) break; } if(y<=1&&x<=2) printf("Possible\n"); else printf("Impossible\n"); return 0;}
- pku 2513 Colored Sticks(字典树+并查集)
- POJ 2513 Colored Sticks(字典树+并查集)
- poj 2513 Colored Sticks (字典树 + 并查集)
- POJ 2513Colored 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(字典树 + 欧拉回路 + 并查集)
- POJ 题目2513 Colored Sticks(字典树+并查集+欧拉回路)
- POJ 2513 Colored Sticks(欧拉回路+字典树+并查集)
- POJ 2513 && NYOJ 230 Colored Sticks (并查集 && 字典树 && 欧拉图)
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- poj -2513-Colored Sticks-哈希&并查集或者字典树&并查集
- 【字典树+并查集】 poj2513 Colored Sticks
- ios 平台 cocos2d-x 项目集成 socketcc 注意事项
- Quantity Of The Stones
- 设置eclipse中window的选项内容
- java设计模式---代理模式
- hud 1233 还是畅通工程( kruskal和prim两种方法)
- pku 2513 Colored Sticks(字典树+并查集)
- 《算法导论》学习之基数排序
- 2013年7月19日 19:50:39
- uva 11111 Generalized Matrioshkas(栈)
- 自我反省(总是自以为是)
- hdu 3483 矩阵构造
- 一种另类的解决URL中文乱码问题
- B. Easy Number Challenge
- Java基础总结大纲(一)