POJ2513 Colored Sticks

Time Limit: 5000MS   Memory Limit: 128000K
Total Submissions: 36032   Accepted: 9427

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 red
red violet
cyan blue
blue magenta
magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

Source

The UofA Local 2000.10.14

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=2400;
10 const int mxm=250010;
11 int read(){
12     int x=0,f=1;char ch=getchar();
13     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
14     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
15     return x*f;
16 }
17 int id=0;
18 struct trie{
19     int a[mxn][26];
20     int end[mxn];
21     int cnt;
22     int hash(char s[]){
23         int len=strlen(s);
24         int now=1;
25         for(int i=0;i<len;i++){
26             if(!a[now][s[i]-‘a‘])a[now][s[i]-‘a‘]=++cnt;
27             now=a[now][s[i]-‘a‘];
28         }
29         if(!end[now])end[now]=++id;
30         return end[now];
31     }
32 };
33 trie t;
34 //
35 int fa[mxm];
36 void init(int n){for(int i=1;i<=n;i++)fa[i]=i;}
37 int find(int x){
38     if(fa[x]==x)return x;
39     return fa[x]=find(fa[x]);
40 }
41 char s[mxm][mxn];
42 char c1[mxn],c2[mxn];
43 int deg[mxm];
44 int main(){
45     int i,j;
46     init(mxm-1);t.cnt=1;
47     while(scanf("%s%s",c1,c2)!=EOF){
48         int x=t.hash(c1);
49         int y=t.hash(c2);
50         deg[x]++;deg[y]++;
51         x=find(x);y=find(y);
52         if(x!=y)fa[x]=y;
53     }
54     int c=0;
55     for(i=1;i<=id;i++){
56         if(deg[i]&1)c++;
57     }
58     bool flag=1;
59     if(c>2)flag=0;
60     else{
61         int x=find(1);
62         for(i=2;i<=id;i++){
63             int y=find(i);
64             if(x!=y){
65                 flag=0;
66                 break;
67             }
68         }
69     }
70     if(flag)printf("Possible\n");
71     else printf("Impossible\n");
72     return 0;
73 }
时间: 2024-10-12 04:20:08

POJ2513 Colored Sticks的相关文章

POJ-2513 Colored Sticks(字典树+并查集+欧拉)

题目链接:Colored Sticks 一道3个知识点结合的题目,可以说单个知识点的题目,都会做,一旦知识点结合起来,题目就不简单了 思路:这个题开始看就知道是并查集,但是不好处理的不同种单词的统计,所以理所应当联想到字典树,上次做字典树的题目啸爷出的是统计相同单词数,这个题目和那个一样,把flag加个编号即可,再利用并查集. 1750ms  水过 #include <iostream> #include <cstdio> #include <cstdlib> #inc

POJ2513 Colored Sticks (并查集+trie)

传送门 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K       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

poj2513 Colored Sticks (欧拉通路+Trie树+并查集)

D - Colored Sticks Crawling in process... Crawling failed Time Limit:5000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2513 Appoint description: System Crawler (2016-05-05) Description You are given a bunch

POJ-2513 Colored Sticks 【欧拉通路+Trie】

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

POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

http://poj.org/problem?id=2513 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 t

POJ2513 Colored Sticks(Trie+欧拉回路)

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

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树. 欧拉通路,每一个节点的统计度,度为奇数的点不能超过2个.并查集,推断节点 是否全然联通. 字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #i

poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 32545   Accepted: 8585 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 st