POJ 2513 Colored Sticks (字典树,并查集,欧拉路径组合题)
来源:互联网 发布:java thread wait 编辑:程序博客网 时间:2024/05/17 01:32
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 violetcyan blueblue magentamagenta cyan
Sample 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;}
0 0
- POJ 2513 Colored Sticks (字典树,并查集,欧拉路径组合题)
- poj 2513 Colored Sticks(字典树+欧拉路径+并查集)
- poj 2513 Colored Sticks(欧拉回路 并查集 路径压缩 字典树)(困难)
- 文章标题 POJ 2523 : 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 Colored Sticks(欧拉回路判断+字典树Trie+并查集)
- POJ 2513 Colored Sticks(字典树,并查集,欧拉回路)
- POJ-2513 Colored Sticks(字典树+并查集+欧拉)
- poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断
- 并查集+欧拉回路+字典树 Colored Sticks POJ 2513
- 代码里写了ARC,而不使用ARC编译
- closest jquery
- hdu 3336 Count the string(KMP)
- 15个受用一生的学习习惯
- PHP学习:字符串操作和正则表达式
- POJ 2513 Colored Sticks (字典树,并查集,欧拉路径组合题)
- javamail 收邮件并解析附件
- java各种流读写文件
- linux下搭建汇编环境
- POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)
- 龙永图助理,龙永图联系电话,龙永图简介,
- OAuth的学习
- POJ 1062 昂贵的聘礼
- Qt 的学习