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”
连通的无向图 G 有欧拉路径的充要条件是:G中奇顶点(连接的边数量为奇数的顶点)的数目等于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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 美容院转让给别人客人要退钱怎么办 卖家毁约中介费担保费怎么办 天猫买东西店家不开增值税票怎么办 天猫专卖店品牌不授权了怎么办 临时京东账号被冻结买的东西怎么办 天猫强行退款给买家商家怎么办 淘宝店铺没交保证金被释放了怎么办 沭阳县地段生过了报名时间怎么办 淘宝买东西地址和收件人填错怎么办 才装修的房子马上要住怎么办 淘宝发货显示无效的发货人怎么办 微信位置和所在地位置不一样怎么办 qq号被冻结了短信发不出去怎么办 商户刷自己信息卡被冻结怎么办 天猫超市买的东西坏了怎么办 天猫超市买东西地址填错了怎么办 天猫超市地址填错了怎么办 天猫超市退货达不到包邮条件怎么办 保千里达令手机锁屏密码忘记怎么办 淘宝买的东西给的发票不见了怎么办 在天猫超市买到发霉怎么办 支付宝红包券金额消费不完怎么办 支付宝向对方发起收款不付怎么办 支付宝收款别人少付了怎么办 红牛领到50元加油优惠券怎么办用 苹果淘宝看评价图片看不清楚怎么办 淘宝账号账户体验中心有违规怎么办 淘宝买的东西需要寄回去维修怎么办 淘宝闪电退款了卖家不肯退了怎么办 云视听会员账号账号密码忘了怎么办 移动卡异地补卡忘记服务密码怎么办 欠我钱的人不接电话怎么办 苹果手机换屏升级系统了黑屏怎么办 苹果手机黑屏时接不到电话怎么办 手机上打电话的图标没了怎么办啊 阿巴町手表上的二维码不生成怎么办 速卖通几个月来搜索曝光很差怎么办 你我贷逾期3个月怎么办 手机放声音的地方进水了怎么办 手机放卡的地方坏了怎么办 京东购物申请退款卖家不同意怎么办