POJ 2513 Colored Sticks(字典树+并查集+欧拉回路)
来源:互联网 发布:js中select标签 编辑:程序博客网 时间:2024/05/17 23:37
Colored Sticks
Time Limit: 5000MS Memory Limit: 128000KTotal Submissions: 32466 Accepted: 8570
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
给你n个木棒,两端都有颜色,问相同颜色连在一起,所有木棒能否连成一条直线
map试了许多次,一直TLE,还是字典树吧,最后还有空数据,又送了几遍WA。。。
先用并查集判一下图是否连通,然后一个条件是全部点的度都为偶数 或 只有两个点的度为奇数
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <stdlib.h>#include <algorithm>#include <cmath>#include <map>#define inf 0x3f3f3f3f#define N 500010using namespace std;struct node{ int s[27]; int id;} q[N*2];int n,m;int a[N],v[N];int bin[N];int findx(int x){ int j; int r=x; while(r!=bin[r]) { r=bin[r]; } int i=x; while(i!=r) { j=bin[i]; bin[i]=r; i=j; } return r;}void merge(int x,int y){ int fx,fy; fx=findx(x); fy=findx(y); if(fx!=fy) bin[fx]=fy;}int Trie(char a[]){ int len=strlen(a); int x=0; for(int i=0; i<len; i++) { if(q[x].s[a[i]-'a']==0) { q[x].s[a[i]-'a']=++m; for(int j=0; j<26; j++) q[m].s[j]=0; x=m; q[x].id=0; } else x=q[x].s[a[i]-'a']; } if(q[x].id==0) { q[x].id=++n; bin[n]=n; } return q[x].id;}int main(){ char s1[20],s2[20]; memset(a,0,sizeof(a)); memset(v,0,sizeof(v)); for(int i=0; i<26; i++) q[0].s[i]=0; bool flag1=false; n=0; m=0; while(~scanf("%s%s",s1,s2)) { int xx=Trie(s1); int yy=Trie(s2); a[xx]++; a[yy]++; v[xx]=1; v[yy]=1; merge(xx,yy); flag1=true; } if(!flag1) { printf("Possible\n"); return 0; } bool flag2; for(int i=0; i<n; i++) { flag2=false; if(v[i]) { for(int j=i+1; j<n; j++) { if(v[j]) { if(findx(i) != findx(j)) { flag2=true;; break; } } } break; } } if(flag2) { printf("Impossible\n"); return 0; } int k1=0; bool flag3=false; for(int i=0; i<n; i++) { if(v[i]) { if(a[i]%2==1) { k1++; if(k1>2) { flag3=true; break; } } } } if(flag3 || k1==1) printf("Impossible\n"); else printf("Possible\n"); return 0;}
0 0
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- 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(字典树+并查集+欧拉回路)
- 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 / hash +并查集 欧拉回路
- POJ Colored Sticks 2513(字典树+并查集+欧拉回路)
- POJ 2513 Colored Sticks——字典树+并查集+欧拉回路
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- Factstone Benchmark
- 基于MFC的串口通信
- Android egl和opengl
- UVA 1103
- 开源应用架构之asterisk
- POJ 2513 Colored Sticks(字典树+并查集+欧拉回路)
- hihoCoder 1082 然而沼跃鱼早就看穿了一切
- zoj 2016 poj 1386 Play on Words 欧拉回路+DFS
- Java删除ArrayList中的重复元素的2种方法
- Binary Tree Inorder Traversal
- easyui toolbar 动态显示与隐藏
- 南阳OJ~~AS||码排序
- 南邮 OJ 1044 连通 OR 不连通
- 致加西亚的信