Trie树+并查集+欧拉回路poj2513
来源:互联网 发布:铁路局通信段 知乎 编辑:程序博客网 时间:2024/06/02 15:02
Language:
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 30307 Accepted: 8007
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
题意:有很多木棒,每根木棒两端有颜色,颜色相同的可以接起来,问可不可以一笔画
思路:就跟单词接龙一样,每种颜色看成一个节点,每根木棒那个看成一条边,问题就转化成了是否存在欧拉回路。对单词进行编号时用Trie进行优化,并查集判断是不是联通
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=250010;char s1[100],s2[100];int in[maxn*2];int pre[maxn*2],id,num;struct node{ node *next[30]; int val; node(){memset(next,0,sizeof(next));val=0;}};struct TREE{ node *root; void clear(){root=new node();} int idx(char x){return x-'a';} int insert(char *s) { int n=strlen(s); node *p=root; for(int i=0;i<n;i++) { int c=idx(s[i]); if(!p->next[c]) p->next[c]=new node(); p=p->next[c]; } if(!p->val)p->val=num++; return p->val; }}tree;int find(int x){ if(x==pre[x])return x; return pre[x]=find(pre[x]);}void unite(int a,int b){ int x=find(a),y=find(b); if(x==y)return; pre[y]=x;}int main(){ memset(in,0,sizeof(in)); num=1; tree.clear(); for(int i=0;i<maxn*2;i++)pre[i]=i; while(scanf("%s%s",s1,s2)!=EOF) { int x=tree.insert(s1); int y=tree.insert(s2); in[x]++,in[y]++; unite(x,y); } int cnt=0; for(int i=1;i<num;i++) if(pre[i]==i)cnt++; if(cnt>1){printf("Impossible\n");return 0;} cnt=0; for(int i=1;i<num;i++) if(in[i]%2)cnt++; if(cnt==2||cnt==0)printf("Possible\n"); else printf("Impossible\n"); return 0;}
0 0
- Trie树+并查集+欧拉回路poj2513
- POJ2513 Trie+并查集+欧拉回路
- poj2513 Colored Sticks —— trie树 + 并查集 + 欧拉回路
- poj2513字典树+并查集+欧拉回路
- poj2513—并查集+欧拉回路+线段树
- [poj2513]Colored Sticks Trie +并查集 + 欧拉回路判断
- POJ2513 并查集+hash+欧拉回路
- POJ2513 Hash+并查集+欧拉回路
- POJ2513-Colored Sticks(并查集 欧拉回路)
- POJ2513 Colored Sticks(字典树+并查集+欧拉回路)
- poj 2513 trie树+并查集+欧拉回路
- poj2513 Colored Sticks (欧拉通路+Trie树+并查集)
- poj2513(trie树+欧拉路+并查集)
- POJ-2513(trie+并查集+欧拉回路)
- POJ2513-并查集+欧拉通路+字典树
- POJ2513【并查集+字典树+欧拉】
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- poj 2513 欧拉回路+并查集判断是否联通+Trie树
- 页面置换算法
- 详解OD脚本的写法续之进阶篇
- 实习之旅
- Python polymorphism
- Python学习笔记
- Trie树+并查集+欧拉回路poj2513
- python 简单实现 图片百叶窗效果
- (转载)JAVA MVC初了解
- 进步,从CSDN博客开始
- 深入理解计算机系统之虚拟存储器
- 一元多项式的表示及相加(抽象数据类型Polynomial的实现)
- 140815暑期培训.txt
- 实现算法2.20、2.21的程序
- poj 1647Sorting by Swapping