[poj2513] 彩色木条
来源:互联网 发布:淘宝cf抽奖是真的吗 编辑:程序博客网 时间:2024/04/28 20:35
题目描述
你有一些木条,木条的两头都涂有颜色,那么能不能把这些木条拼成一条直线,要求相邻两根木条相互接触的一头是相同的颜色?
输入格式
输入有若干行,每行两个空格间隔的单词,表示一根木条两段的颜色
单词用小写字母表示,并且长度不超过10,木条的数量不超过300000根
输出格式
如果能行,输出”Possible”, 否则输出”Impossible”
样例数据
样例输入
blue red
red violet
cyan blue
blue magenta
magenta cyan
样例输出
Possible
说明
Huge input,scanf is recommended.
题目分析
仔细分析,如果每个颜色出现了偶数次,那么就可以把他们连起来,如果出现了奇数次,其中一个肯定是开头或结尾。
故丢进trie树统计个数,如果颜色是奇数的>2,impossible
还需判断图是否联通,用并查集,如果最后不是同一个父亲,impossible
还需判断图是否是欧拉图,统计度,若度为奇数的点个数不是0或2,impossible
源代码
#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj;}const int maxn=500000;string s1,s2;int father[300005],Degree[300005],papa;int Get_Father(int x) { if(father[x]==x)return x; else return father[x]=Get_Father(father[x]);}void Union(int a,int b) { int fa=Get_Father(a),fb=Get_Father(b); if(fa!=fb)father[fa]=fb;}struct Tree { int child[26],bj,cnt; void clear() { memset(child,0,sizeof(child)); bj=0; cnt=0; }};struct Trie { Tree tree[maxn]; int cnt,f,tot,former; void init() { cnt=0; f=0; tot=0; memset(tree,0,sizeof(tree)); } void insert(string s) { //插入字符串s int x=0; for(int i=0; i<s.length(); i++) { int y=s[i]-'a'; if(tree[x].child[y]==0) { //没有此结点,新建结点 tree[x].child[y]=++cnt; tree[cnt].clear(); x=cnt; } else x=tree[x].child[y]; } tree[x].bj=1; tree[x].cnt++; } int Find(string s) { //返回字符串s出现的下标,没有返回-1 int x=0; for(int i=0; i<s.length(); i++) { int y=s[i]-'a'; if(tree[x].child[y]==0)return -1; //不在树中 x=tree[x].child[y]; } if(tree[x].bj)return x; //有标记域才能返回 else return -1; } void Dfs(int Now) { f+=tree[Now].cnt%2; if(tree[Now].bj) { tot++; if(tot>=2&&Get_Father(Now)!=former) { puts("Impossible"); exit(0); } former=Get_Father(Now); } papa=Get_Father(Now); for(int i=0; i<26; i++) if(tree[Now].child[i])Dfs(tree[Now].child[i]); }};Trie trie;int main() { ios::sync_with_stdio(false); for(int i=0; i<=300000; i++)father[i]=i; while(cin>>s1>>s2) { trie.insert(s1); trie.insert(s2); int num1=trie.Find(s1),num2=trie.Find(s2); Union(num1,num2); Degree[num1]++; Degree[num2]++; } if(trie.cnt==0) { puts("Possible"); return 0; } int cnt=0; for(int i=0; i<=trie.cnt; i++) if(Degree[i]&1)cnt++; if(cnt!=0&&cnt!=2) { puts("Impossible"); return 0; } trie.Dfs(0); if(trie.f>2)puts("Impossible"); else puts("Possible"); return 0;}
0 0
- [poj2513] 彩色木条
- poj2513
- POJ2513
- poj2513
- poj2513
- poj2513
- poj2513
- 木条加工
- poj2513 Colored Sticks / nyoj230 彩色棒(字典树,并查集,欧拉通路)
- zzu oj1223(切割木条)
- POJ2513 欧拉图
- POJ2513解题报告
- poj2513解题报告
- POJ2513解题报告
- POJ2513 欧拉图入门
- POJ2513--Colored Sticks
- poj2513(字典树)
- poj2513 Colored Sticks
- MFC控件使用大全
- MySQL大表优化方案
- 上下界网络流
- MFC 模态对话框和非模态对话框
- Android中bindService的细节之二:从进程的角度分析绑定Service的流程【Service所在进程已存在】
- [poj2513] 彩色木条
- Kafka 集群启动脚本
- Targan
- [leetcode]46. Permutations
- 0123程序练习
- MFC数据类型转化笔记
- HDU1061Rightmost Digit
- HDU1312 Red and Black(DFS水)
- hdu5762 Teacher Bo