POJ 2513 Colored Sticks (字典树,并查集,欧拉路径组合题)
2014-08-08 08:56
387 查看
DescriptionYou 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?InputInput 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 250000sticks.OutputIf 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 cyanSample Output
Possible
火柴有两个端点,只要颜色一样就可以连在一起,要判断能否把所有火柴首尾相连
把每个火柴当作一条边,所有火柴连接起来当作一个无向图,只要无向图能形成欧拉路径则输出“Possible”
连通的无向图 有欧拉路径的充要条件是: 中奇顶点(连接的边数量为奇数的顶点)的数目等于0或者2
那我们记录每个节点的边数即可,至于节点可以把不同颜色当作不同的节点,同样的颜色当作同样的节点,用字典树辅助判断,每次只要是新的颜色就映射为一个新的数字即可
至于整体是否连通用并查集记录即可
#include<stdio.h>#include<string.h>#include<memory.h>struct Node{Node *next[26];int cnt;//记录单词末尾标记,标记单词映射的数字Node(){for (int i = 0; i<26; i++) next[i] = NULL;cnt = -1;}};Node root;int color[500001];int father[500001];void insert(char *str, int num){Node *p = &root, *q;int len = strlen(str);int id;for (int i = 0; i<len; i++){id = str[i] - 'a';if (p->next[id] == NULL){q = new Node;p->next[id] = q;p = q;}else{p = p->next[id];}if (i == len - 1) p->cnt = num;}}int find(char *str){int len = strlen(str);Node *p = &root;int id;for (int i = 0; i<len; i++){id = str[i] - 'a';p = p->next[id];if (p == NULL) return -1;}return p->cnt;}int findfa(int a){return father[a] == a ? a : findfa(father[a]);}int main(){char str1[11], str2[11];int num = 0;int endnum1, endnum2;memset(color, 0, sizeof(color));while (scanf("%s%s", str1, str2) != EOF){if (strcmp(str1, str2) == 0) continue;//首尾颜色一样可以当作不存在endnum1 = find(str1);if (endnum1 == -1){insert(str1, num);father[num] = num;endnum1 = num;num++;}color[endnum1]++;endnum2 = find(str2);if (endnum2 == -1){insert(str2, num);father[num] = num;endnum2 = num;num++;}color[endnum2]++;father[findfa(endnum1)] = father[findfa(endnum2)];}if (num == 0){printf("Possible\n");return 0;}int jdflag = 0, ltflag = 1;int i;for (i = 0; i<num - 1; i++){if (findfa(i) != findfa(i + 1)){ltflag = 0;break;}if (color[i] % 2 == 1) jdflag++;}if (color[i] % 2 == 1) jdflag++;if (ltflag == 1 && (jdflag == 0 || jdflag == 2)) printf("Possible\n");else printf("Impossible\n");return 0;}
相关文章推荐
- POJ 2513 Colored Sticks 字典树hash 欧拉路径
- poj 2513 Colored Sticks(字典树+欧拉路径+并查集)
- POJ 2513 Colored Sticks(字典树+欧拉路径)
- POJ 2513 Colored Sticks 字典树+欧拉通路判定+并查集
- poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)
- 欧拉路径 之 poj 2513 Colored Sticks
- POJ训练计划2513_Colored Sticks(字典树+判断欧拉通路)
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
- poj 2513 Colored Sticks (字典树,并查集,欧拉回路)
- POJ 2513 Colored Sticks 字典树 + 并查集 + 欧拉通路
- (字典树+欧拉通路) poj 2513 Colored Sticks
- poj 2513(欧拉路径+字典树映射)
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- poj 2513(欧拉路径+字典树映射)
- POJ 2513 Colored Sticks (Trie字典树+欧拉通路+并查集) 解题报告
- POJ 2513 - Colored Sticks 判断无向图哈密顿通路转化为判断无向图欧拉通路
- PKU2513 - Colored Sticks --一道很不错的题(结合了并查集的应用,字典树的应用,欧拉通路的原理)
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
- POJ 2513 Colored Sticks 并查集 + 字典树 + 欧拉回路