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

http://poj.org/problem?id=2513

题意:

给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的。

思路:

题目很明显的是欧拉道路的问题。

欧拉道路的关键是:

①图是连通的。

②最多只能有两个奇点。(不能只存在一个奇点)

本来是想用map映射的,但是太多了,比较费时,这里用字典树的话会比较省时,判断图是否连通可以用并查集来完成。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<cstring>
 5 using namespace std;
 6
 7 const int maxn = 500000 + 5;
 8 int p[maxn], deg[maxn];
 9 int cnt;
10
11 struct Trie
12 {
13     int ch[maxn][30];
14     int vis[maxn];
15     int sz;
16
17     void init()
18     {
19         sz = 1;
20         memset(ch[0], 0, sizeof(ch[0]));
21     }
22
23     int insert(char *s, int& v)
24     {
25         int u = 0, n = strlen(s);
26         for (int i = 0; i < n; i++)
27         {
28             int c = s[i]-‘a‘;
29             if (!ch[u][c])
30             {
31                 memset(ch[sz], 0, sizeof(ch[sz]));
32                 vis[sz] = 0;
33                 ch[u][c] = sz++;
34             }
35             u = ch[u][c];
36         }
37         if (!vis[u])   vis[u] = ++v;
38         return vis[u];
39     }
40 }t;
41
42 int find(int x)
43 {
44     return x == p[x] ? x : find(p[x]);
45 }
46
47 void merge(int x, int y)
48 {
49     int fx = find(x);
50     int fy = find(y);
51     if (fx != fy)
52         p[fx] = fy;
53 }
54
55 int main()
56 {
57     //freopen("D:\\txt.txt", "r", stdin);
58     char a[20], b[20];
59     t.init();
60     memset(deg, 0, sizeof(deg));
61     for (int i = 0; i <= maxn; i++)   p[i] = i;
62     cnt = 0;
63     while (~scanf("%s %s", a, b))
64     {
65         int id1 = t.insert(a,cnt);
66         int id2 = t.insert(b,cnt);
67         deg[id1]++;
68         deg[id2]++;
69         merge(id1, id2);
70     }
71     int ans = 0;
72     for (int i = 1; i <= cnt; i++)
73     {
74         if (deg[i] % 2 == 1)   ans++;
75         if (ans > 2 || find(1) != find(i))
76         {
77             printf("Impossible\n");
78             return 0;
79         }
80     }
81     if (ans == 1)
82         printf("Impossible\n");
83     else
84         printf("Possible\n");
85     return 0;
86 }
时间: 2024-11-25 09:48:29

POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)的相关文章

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 无向欧拉通路+字典树+并查集

题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧拉通路的判断,但是map效率太低,超时了 网上看了一遍发现必须得用效率更高的字典树对每个不同的颜色进行赋值 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace st

POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)

题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 转:kuangbing 无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节点的度为偶数,或者有且只有两个度为奇数的节点. 图的连通可以利用并查集去判断. 度数的统计比较容易. view code//第一次用指针写trie,本来是用二维数组,发现数组开不下,只好删删改改,改成指针 //做这道题,知道了欧拉回路判定,还有用指针写trie #include

poj2513Colored Sticks(欧拉通路+字典树+并查集)

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 题目大意:有N个木棍,每根木棍两端被涂上颜色,现在给定每个木棍两端的颜色,不同木棍之间拼接需要颜色相同的 端才可以,问最后能否将N个木棍拼接在一起. 解题思路:欧拉通路+并查集+字典树.欧拉通路,每个节点的统计度,度为奇数的点不能超过2个.并查集,判断节点 是否完全联通.字典树,映射颜色. #include <cstdio> #include <cstring> #include <string> #includ

POJ 2513 Colored Sticks 欧拉路的判断+字典树

题目链接: poj2513 题意: 给定一捆木棍.每根木棍的每个端点涂有某种颜色.问:是否能将这些棍子首尾相连,排成 一条直线,且相邻两根棍子的连接处端点的颜色一样. 输入描述: 输入文件中包含若干行,每行为两个单词,用空格隔开,表示一根棍子两个端点的颜色.表 示颜色的单词由小写字母组成,长度不超过10 个字符.木棍的数目不超过250000. 输出描述: 如果木棍能按照题目的要求排成一条直线,输出"Possible",否则输出"Impossible". 解题思路:

[欧拉] poj 2513 Colored Sticks

主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30955   Accepted: 8159 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is i

[欧拉回路] poj 2513 Colored Sticks

题目链接: http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30955   Accepted: 8159 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

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