poj2513 Colored Sticks 字典树+并查集+欧拉路
2016-02-19 21:41
351 查看
DescriptionYou 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?InputInput 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.OutputIf the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.Sample Input
blue red red violet cyan blue blue magenta magenta cyanSample Output
Possible
就是让你把这几根棒子排在一起看能不能成环,先存着,下次来写
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct trie{bool flag;int id;trie* next[27];};int color=0,du[500001]={0},f[500001];int find(int x){if(f[x]!=x)f[x]=find(f[x]);return f[x];}void union1(int a,int b){f[find(b)]=find(a);}trie* built(){trie* leaf=new trie;leaf->flag=false;leaf->id=0;memset(leaf->next,0,sizeof(leaf->next));return leaf;}int build(trie *&root,char* s){trie* p=root;int len=0;while(s[len]!='\0'){int x=s[len++]-'a';if(!p->next[x])p->next[x]=built();p=p->next[x];}if(p->flag)return p->id;else{p->flag=true;p->id=++color;return p->id;}}int main(){trie* root=built();for(int k=1;k<=500000;k++)f[k]=k;char a[20],b[20];while(cin>>a>>b){int i=build(root,a),j=build(root,b);du[i]++,du[j]++;union1(i,j);}int num=0,s=find(1);for(int i=1;i<=color;i++){if(du[i]%2==1)num++;if(num>2||find(i)!=s){cout<<"Impossible\n";return 0;}}if(num==1)cout<<"Impossible\n";else cout<<"Possible\n";return 0;}[/code]
相关文章推荐
- 软考之路(1)——浅解网络基础知识
- 一道题目
- hrbust 1216/哈理工oj 1216 数的划分【dp】
- 百练2755 奇妙的口袋 【深搜】or【动规】or【普通递归】or【递推】
- 编程之路
- jquery validate使用Demo实例说明
- 深入理解CSS盒子模型
- 10002---全文检索引擎Solr系列——入门篇
- QT学习笔记——容器类总结
- 偶尔回顾一下C99先于C11(末)
- 第一博客,行走在编程道路上的菜鸟
- shell运算符之 关系运算符,算数运算符,布尔运算符,字符串运算符和文件测试运算符
- ARP协议-地址解析的过程
- windows客户端开发--通过ShellExecute函数打开浏览器
- 排序算法(五)——简单选择排序
- windows客户端开发--通过ShellExecute函数打开浏览器
- BZOJ 1305 CQOI2009 dance跳舞 二分答案+最大流
- [BZOJ 2180]最小直径生成树
- json
- ThinkPHP框架总结之安全及使用