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


题解:把每种颜色看成顶点,棍子看成边,即转化为求是否存在欧拉通路问题,这题数据量大,Map超时,用hash恰当的mod值取不来(discuss里神tm1000过了一脸懵逼),用Trie可以过。

代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 #include<map>
 7 using namespace std;
 8 const int N = 250000 * 2 + 5;
 9 int deg[N], p[N];
10 struct Edge {
11     int u, v;
12 }edge[N];
13
14 int ch[N][26], val[N], num = 0, tot = 0;
15 int insert(char *s) {
16     int now = 0, len = strlen(s);
17     for (int i = 0; i < len; ++i) {
18         int id = s[i] - ‘a‘;
19         if (!ch[now][id]) ch[now][id] = ++num;
20         now = ch[now][id];
21     }
22     val[now] = ++tot;
23     return val[now];
24 }
25
26 int find(char *s) {
27     int now = 0, len = strlen(s);
28     for (int i = 0; i < len; ++i) {
29         int id = s[i] - ‘a‘;
30         if (!ch[now][id]) return 0;
31         now = ch[now][id];
32     }
33     return val[now];
34 }
35
36 int Find(int x) {return x == p[x] ? x : p[x] = Find(p[x]);}
37
38 void Union(int x, int y) {
39     x = Find(x); y = Find(y);
40     if (x != y) p[x] = y;
41 }
42
43 bool bconnect(int n, int m) {
44     for (int i = 1; i <= m; ++i) p[i] = i;
45     for (int i = 0; i < n; ++i) {
46         int u = edge[i].u, v = edge[i].v;
47         if (u != v) Union(u, v);
48     }
49     for (int i = 2; i <= m; ++i)
50         if (Find(i) != Find(1)) return false;
51     return true;
52 }
53
54 int main() {
55     char s1[15], s2[15];
56     int edgenum = 0, u, v;
57     while (~scanf("%s%s", s1, s2)) {
58         u = find(s1); v = find(s2);
59         if (!u) u = insert(s1);
60         if (!v) v = insert(s2);
61         edge[edgenum].u = u;
62         edge[edgenum].v = v;
63         ++edgenum;
64         deg[u]++; deg[v]++;
65     }
66     bool eular = true;
67     int odd = 0;
68     for (int i = 1; i <= tot; ++i) {
69         if (deg[i] % 2 == 1) ++odd;
70         if (odd > 2) {eular = false; break;}
71     }
72     if (!bconnect(edgenum, tot)) eular = false;
73     if (eular) printf("Possible\n");
74     else printf("Impossible\n");
75
76     return 0;
77 }
时间: 2024-12-20 22:02:57

POJ-2513 Colored Sticks 【欧拉通路+Trie】的相关文章

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

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

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

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

[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

poj 2513 Colored Sticks 彩色棒

poj  2513  Colored Sticks http://poj.org/problem?id=2513 题意:现在有几个木棒,每个木棒端点都着色,问:能否将它们排成一排,同时要满足相邻的的两端点颜色是一样的. trie+并查集+欧拉通路 方法:要想排成一排,可以变向的理解为从一个图里找到一个欧拉通路(一定要想到):如果只是孤零零的一个个小棒,这道题很难实现. 首先:要先要对所有颜色进行编号,由于事先又不知道有几种颜色,有哪几种颜色.故有一个笨方法,用map<int,string>容器

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

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

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

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

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 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)

题目地址:POJ 2513 刚开始没想到字典树,用的map函数一直TLE,由于上一次的签到题由于没想到字典树而卡了好长时间的深刻教训,于是过了不久就想起来用字典树了,(为什么是在TLE了5次之后..T^T)然后把map改成了字典树,然后就过了. 这题居然不知不觉的用上了欧拉回路..其实当时我是这样想的..因为相互接触的必须要相同,所以除了两端外,其他的都是两两相同的,所以除了两端的颜色外其他的的个数必须为偶数.然后两端的可能相同可能不相同,相同的话,说明所有的都是偶数个数了,不相同的话那就只有这

[欧拉] 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