Poj 2513 Colored Sticks(字典树+欧拉回路)
来源:互联网 发布:ubuntu login进不去 编辑:程序博客网 时间:2024/06/10 08:38
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 31553 Accepted: 8339
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.
题意:
给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{ int flag; int no; node *next[26];}*head;int degree[250050];int bin[150050];int color;int Search(char *s){ node *p=head; node *q; int i=0; while(s[i]!='\0'){ if(p->next[ s[i]-'a' ]==NULL){ q=new node; for(int j=0;j<26;j++){ q->next[j]=NULL; } q->flag=0; q->no=0; p->next[ s[i]-'a' ]=q; } p=p->next[ s[i]-'a' ]; i++; } if(p->flag) return p->no; else{ p->flag=1; p->no=color++; return p->no; }}int Find(int x){ if(bin[x]==-1){ return x; } return x==bin[x]?x:Find(bin[x]);}void Merge(int x,int y){ int f1,f2; f1=Find(x); f2=Find(y); if(f1!=f2) bin[f1]=f2;}int main(){ char str1[15],str2[15]; memset(degree,0,sizeof(degree)); memset(bin,-1,sizeof(bin)); head=new node; for(int i=0;i<26;i++){ head->next[i]=NULL; } head->flag=0; head->no=0; color=0; while(scanf("%s%s",str1,str2)!=EOF){ int x=Search(str1); int y=Search(str2); degree[x]++; degree[y]++; Merge(x,y); } int odd=0,null=0; int i; for(i=0;i<color;i++){ if(degree[i]%2) odd++; if(bin[i]==-1) null++; if(odd==3) break; if(null==2) break; } if(i==color) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; return 0;}
0 0
- 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 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(欧拉回路 并查集 路径压缩 字典树)(困难)
- linux的secureCRT的连接
- 最新发布树莓派2Wi-Fi自动连接实战(适合初学者)
- 一个清华大学毕业生做猎头的感受(转)
- 并发编程11-测试并发程序
- 在android中的文件放在不同位置也有不同的读取方式
- Poj 2513 Colored Sticks(字典树+欧拉回路)
- 简单介绍C语言三种动态开辟空间函数.
- Tabbar只用图片,图片不居中的解决方案
- Microsoft Windows Azure 提供了哪些服务
- CPU访问外设
- Poj 2531 Network Saboteur
- 参赛约束 A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。
- Perl学习笔记(一)文件迁移脚本
- Unity3D架构系列之FSM有限状态机设计(五)