POJ 2513 Colored Sticks 欧拉回路+trie树
来源:互联网 发布:微信诱导付费源码 编辑:程序博客网 时间:2024/06/06 01:42
题目
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
即木棍相接的颜色得相同~
本来想用哈希处理,WA了,提供从建议改用trie树,现在看来练习中难的够呛的算法也只不过是为了其他算法做配角
不仅仅说是并查集检验欧拉回路==trie树不也是嘛
#include <iostream>#include<cstdio>#include<cstring>using namespace std;int total;class trie{public: trie* next[26]; char *color; int value; //标记这里是不是一个单词 trie() { for(int i=0;i<26;i++) next[i]=0; color=NULL; value=-1; }}root;int insert(trie *p,char* s){ int k=0; while(s[k]!='\0') { if(!p->next[s[k]-'a']) p->next[s[k]-'a']=new trie; p=p->next[s[k]-'a']; k++; } if(p->value==-1) p->value=(total++); return p->value; //在这里可根据具体题意来加东西}int a[500019];int total1[500010],total2[500010];int find(int x){ if(x!=a[x]) a[x]=find(a[x]); return a[x];}void uni(int x,int y){ x=find(x),y=find(y); if(x!=y) a[x]=y;}int main(){ //freopen("cin.txt","r",stdin); int t,n,sum; char str1[20],str2[20]; trie *p=&root; // scanf("%d",&n); // memset(vis,false,sizeof(vis)); for(int i=0;i<=500010;i++) a[i]=i; sum=0,total=0; int in=0,out=0; memset(total1,0,sizeof(total1)); memset(total2,0,sizeof(total2)); while(~scanf("%s%s",str1,str2)) { int num1=insert(p,str1); int num2=insert(p,str2); total1[num1]++;//total2[num2]++; total1[num2]++; uni(num1,num2); // printf("%d %d\n",num1,num2); // if(!vis[num1]){ total++;vis[num1]=true;} // if(!vis[num2]){ total++;vis[num2]=true;} //total=max(total,max(num1,num2)); } for(int i=0;i<total;i++) { if(i==a[i]) sum++; /*if(total1[i]!=total2[i]) { if(total1[i]==total2[i]+1) in++;//cout<<i<<endl; else if(total1[i]==total2[i]-1) out++; else {sum+=100;break;} }*/ if(total1[i]&1) in++; if(sum>1) break; if(in>2) break; } // if((sum==0||sum==1)&&((in==1&&out==1)||(in==0&&out==0)))printf("Possible\n"); if((sum==0||sum==1)&&(in==0||in==2)) printf("Possible\n"); else printf("Impossible\n"); // printf("sum=%d in=%d out=%d\n",sum,in,out); return 0;}
0 0
- POJ Colored Sticks(2513) -trie树&欧拉回路
- POJ 2513 Colored Sticks 欧拉回路+trie树
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks (并查集 Trie树 欧拉回路)
- poj-2513-Colored Sticks-字典树trie / hash +并查集 欧拉回路
- [欧拉回路] 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(字典树,并查集,欧拉回路)
- POJ 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- poj 2513 Colored Sticks 欧拉回路(字典树 +并查集)
- POJ 2513 Colored sticks(字典树 + 欧拉回路 + 并查集)
- POJ 题目2513 Colored Sticks(字典树+并查集+欧拉回路)
- Programming in scala学习笔记(二)Next steps in scala
- java 泛型generic type
- DOM(二)-09-(示例-创建表格-指定行列)
- eclipse中maven项目引入依赖后自动下载源码
- ios开发 加急审核
- POJ 2513 Colored Sticks 欧拉回路+trie树
- qt中如何把插件中的窗口布局到UI设计师的窗口上
- rpm 命令参数使用详解
- Linux下安装数据库时设置内核参数
- python 获取股票的交易数据
- kafka 常用命令说明
- 黑马程序员——继承、抽象类、接口、多态
- DEV GridControl(1)
- QCOM平台子系统通过AP处理异常