POJ 2513 Colored Sticks(字典树+并查集)
2016-11-07 14:30
435 查看
原题链接:http://poj.org/problem?id=2513
Time Limit: 5000MS Memory Limit: 128000K
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.
red violet
cyan blue
blue magenta
magenta cyan
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 redred violet
cyan blue
blue magenta
magenta cyan
Sample Output
PossibleHint
Huge input,scanf is recommended.Source
The UofA Local 2000.10.14解题思路
使用字典树存储各个颜色的英文,并用数字一一对应。木棍两边的颜色用并查集维护,最后如果发现有两个及以上连通分量,那一定是不符合题意的。本题接下来转化成欧拉图问题,图中所有的节点中,度数为奇数的节点只能有0个或2个。AC代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<string> #include<queue> #include<sstream> #include<list> #include<stack> #define ll long long #define ull unsigned long long #define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++) #define rrep(i,a,b) for(int i=(a),_ed=(b);i>=_ed;i--) #define fil(a,b) memset((a),(b),sizeof(a)) #define cl(a) fil(a,0) #define PI 3.1415927 #define inf 0x3f3f3f3f #define MAXN 500005 #define MAX 26 template<typename N>N gcd(N a, N b) { return b ? gcd(b, a%b) : a; } using namespace std; typedef struct TrieNode //字典树节点 { bool isword; //当前节点是否为颜色单词的结尾 TrieNode * next[MAX]; int id; }Trie; int f[MAXN]; int degree[MAXN]; int color; int find(int a) { if (f[a] == -1) return a; else return f[a] = find(f[a]); } void uni(int a, int b) { int x, y; x = find(a); y = find(b); if (x != y) f[b] = a; return; } int insert(Trie * root,char *word) //插入单词 { Trie * p = root; int i = 0; while(word[i]!='\0') { if (p->next[word[i] - 'a'] == NULL) { Trie *temp = new Trie; temp->id = 0; temp->isword = false; rep(j, 0, MAX - 1) temp->next[j] = NULL; p->next[word[i] - 'a'] = temp; } p = p->next[word[i] - 'a']; i++; } if (!p->isword) { p->isword = true; p->id = color++; } return p->id; } int main() { char str1[15]; char str2[15]; int color1, color2; int link = 0, oddDegree = 0; Trie *root = new Trie; fil(f, -1); cl(degree); root->id = 0; root->isword = false; rep(i, 0, MAX - 1) root->next[i] = NULL; while (scanf("%s%s", str1, str2) != EOF) { color1 = insert(root, str1); color2 = insert(root, str2); degree[color1]++; degree[color2]++; uni(color1, color2); } rep(i, 0, color - 1) { if (f[i] == -1) link++; if (degree[i] & 1) oddDegree++; if (link > 1 || oddDegree > 2) break; } if ((link == 0 || link == 1) && (oddDegree == 0 || oddDegree == 2)) printf("Possible\n"); else printf("Impossible\n"); return 0; }
相关文章推荐
- POJ 2513 Colored Sticks 字典树+并查集
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
- POJ2513 Colored Sticks
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路
- POJ 2513 Colored Sticks(trie 欧拉通路 并查集)
- POJ 2513-Colored Sticks (字典树)
- poj 2513 Colored Sticks (字典树+并查集+无向图的欧拉回路。)
- poj 2513 Colored Sticks (无向欧拉路+字典树)
- POJ 2513Colored Sticks(????)
- POJ 2513 Colored Sticks - from lanshui_Yang
- POJ-2513 Colored Sticks
- poj 2513 Colored Sticks
- [欧拉] poj 2513 Colored Sticks
- POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks 【Trie树】
- POJ 2513 Colored Sticks (Trie + 并查集 + 欧拉通路)
- POJ 2513 Colored Sticks
- POJ 2513 Colored Sticks(hash + 欧拉道路)
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- poj2513——Colored Sticks