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 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

回顾了一下欧拉图的知识。。整个图的奇度点个数如果>=3或只有1个 就不能一笔画画完

中间re了几次。。因为trie里我开了n*30,但是外面数组只开了n...

 1 #include<set>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 const int N = 250010;
10 #define For(i,n) for(int i=1;i<=n;i++)
11 #define For0(i,n) for(int i=0;i<n;i++)
12 #define Rep(i,l,r) for(int i=l;i<=r;i++)
13
14 char s1[11],s2[11];
15 int n,in[N*30],fa[N*30],ans;
16 struct Trie{
17     int sz,ch[N*30][26],v[N*30],Loc[N*30];
18     Trie(){sz=1;}
19     int insert(char st[]){
20         int now=1;v[1]++;int len=strlen(st);
21         For0(i,len){
22             int id=st[i]-‘a‘;
23             if(!ch[now][id]) ch[now][id]=++sz;
24             v[now=ch[now][id]]++;
25         }
26         if(Loc[now]) return Loc[now];
27         else         return Loc[now]=++n;
28     }
29 }trie;
30
31 int find(int i){
32     if(fa[i]==i) return i;
33     else         return fa[i]=find(fa[i]);
34 }
35
36 int main(){
37     #ifndef ONLINE_JUDGE
38         freopen("trie.in","r",stdin);
39     #endif // ONLINE_JUDGE
40     For0(i,N*30) fa[i]=i;
41     while(scanf("%s %s",&s1,&s2)!=EOF){
42         int x=trie.insert(s1),y=trie.insert(s2);
43         in[x]++;in[y]++;
44         int fx=find(x),fy=find(y);
45         if(fx!=fy) fa[fx]=fy;
46     }
47     int checks=find(1);
48     Rep(i,2,n) if(find(i)!=checks){
49         puts("Impossible\n");
50         return 0;
51     }
52     For(i,n) {
53         in[i]=in[i]%2;
54         if(in[i]) ans++;
55     }
56     if(ans==1||ans>=3) puts("Impossible");
57     else               puts("Possible");
58     return 0;
59 }
时间: 2024-10-14 22:40:22

POJ2513 Colored Sticks (并查集+trie)的相关文章

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30273   Accepted: 8002 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 sti

POJ - Colored Sticks - 并查集+字典树

这道题主要还是要判断是不是欧拉图 说白了就是能不能这幅图能不能用一笔画下来,那么就可以知道了,如果是一个环状的,说明奇数度就不存在,否则就只能用两个奇数度(起点终点)//我的理解这是 只需要用字典树将单词变为对应的一个数字,然后并查集操作就可以,需要维护一个度变量 #include<stdio.h> #include<string.h> int du[500010],p[500010]; int tot=1; struct tree { tree *next[30]; int id

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(字典树+并查集+欧拉)

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

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

POJ2513(欧拉回路+并查集+字典树)

Colored Sticks 题目链接:https://vjudge.net/problem/POJ-2513 题目大意: candidate19有好多根棍子,这些棍子的两端分都别涂了一种颜色. candidate19突然有了一个疑问,就是他手里的这些棍子能否互相拼接,从而形成一条直线呢? 两根棍子只有在颜色相同的时候才能拼接.比如有两根棍子,第一根棍子的两端的颜色分别为blue green,第二根两端的颜色为blue red,那么他们就可以拼接成green blue blue red或者red

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

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 suc