POJ2513 【并查集+欧拉路径+trie树】

题目链接:http://poj.org/problem?id=2513

Colored Sticks

Time Limit: 5000MS Memory Limit: 128000K
Total Submissions:40949 Accepted: 10611

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.
 
题目大意:给出以下字符串,代表每根棍子的两端颜色,只能将颜色相同的端点连接起来。问是否能连成一欧拉路径。
思路:
1.棍子两端无方向要求,故是 无向图的欧拉路径,无向图判断欧拉路径的充要条件是:图连通( 无向图的连通性可以用并查集来判断,但有向图不可以)+ 点的度全为偶/当且仅当只有2个点的度为奇
2.关键在于将颜色字符串映射成整型编号,有两种思路:用map直接映射(TLE), 用trie树给字符串上编号。
3.具体实现在代码中, 值得注意的一点是 G++才能AC。
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define mem(a, b) memset(a, b, sizeof(a))
 4 const int MAXN = 250000 * 2 + 10;//最多MAXN种颜色 即最多MAXN种不同的点 
 5 
 6 char s1[12], s2[12];
 7 int deg[MAXN];
 8 int trie[MAXN][27], cnt, tot;
 9 int id[MAXN], pre[MAXN];
10 
11 int insert(char s[12])
12 {
13     int flag = 1;
14     int len = strlen(s);
15     int root = 0;
16     for(int i = 0; i < len; i ++)
17     {
18         int id = s[i] - 'a';
19         if(!trie[root][id])
20         {
21             flag = 0;  //单词之前没出现过 
22             trie[root][id] = ++ cnt;
23         }
24         root = trie[root][id];
25     }
26     if(!flag)
27     {
28         tot ++;
29         id[root] = tot;
30         return id[root];
31     }
32     else
33         return id[root];
34 }
35 
36 int find(int x)
37 {
38     if(pre[x] == x)
39         return x;
40     else
41     {
42         int root = find(pre[x]);
43         pre[x] = root;
44         return pre[x];
45     }
46 }
47 
48 int main()
49 {
50     for(int i = 1; i <= MAXN + 1; i ++)
51         pre[i] = i;
52     while(scanf("%s%s", s1, s2) != EOF)
53     {
54         int a = insert(s1), b = insert(s2);//返回的是颜色所对应的序号
55         deg[a] ++, deg[b] ++; //点的度数 判断是否存在欧拉路径 
56         int x = find(a), y = find(b); //查找根节点 
57         if(x != y)
58             pre[y] = x;
59     }
60     int flag = 1;
61     for(int i = 1; i < tot; i ++) //总共有tot个不同的点 
62         if(find(i) != find(i + 1))
63         {
64             flag = 0;
65             break;
66         }
67     if(flag == 0)
68         printf("Impossible\n");
69     else
70     {
71         int xx = 0;
72         for(int i = 1; i <= tot; i ++)
73             if(deg[i] % 2)
74                 xx ++;
75         if(xx == 0 || xx == 2)
76             printf("Possible\n");
77         else
78             printf("Impossible\n");
79     }
80     return 0;
81 }
View Code

 

转载于:https://www.cnblogs.com/yuanweidao/p/11296325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值