POJ2513-Colored Sticks
/*
思路:类似图论中“一笔画”问题,两根木棒的相连接的端点是一样的颜色,(a,b)--(b,c)--(c, d)....
方法:trie树+并查集, 利用trie树建立字符串和某一个节点的映射,并将这些和字符串构成映射的节点建成图, 用并查集判断图的连通
*/
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #define N 2500005*2
5 using namespace std;
6 int f[N];
7 int indgr[N];
8 int trie[N][26];
9 int nodeNum, pre, cnt, oddDgr, root;
10 int getFather(int x)//并查集寻找父亲节点,压缩路径
11 {
12 return x == f[x] ? x : f[x]=getFather(f[x]);
13 }
14
15 void Union(int a, int b)//并查集的合并
16 {
17 int fa=getFather(a), fb=getFather(b);
18 if(fa!=fb)
19 f[fa]=fb;
20 }
21
22 int main()
23 {
24 char color[15];
25 int i;
26 for(i=0; i<N; ++i)
27 f[i]=i;
28 while(scanf("%s", color)!=EOF)
29 {
30 cnt++;
31 int cur=0, L=strlen(color);
32 for(i=0; i<L; ++i)
33 {
34 int k=color[i]-'a';
35 if(!trie[cur][k])
36 trie[cur][k]=++nodeNum;
37 cur=trie[cur][k];
38 }
39 ++indgr[cur];
40 if(cnt%2) pre=cur;
41 else
42 Union(pre, cur);
43 }
44 for(i=0; i<=nodeNum; ++i)
45 {
46 if(indgr[i]&1) ++oddDgr;
47 if(indgr[i] && f[i]==i) ++root;
48 if(oddDgr>2 || root>1) break;
49 }
50 if((oddDgr==0 || oddDgr==2) && root==1 || oddDgr==0 && root==0)//注意空树的情况下是输出Impossible, 开始就是错在了这里
51 printf("Possible\n");
52 else printf("Impossible\n");
53 return 0;
54 }
POJ2513-Colored Sticks的更多相关文章
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- POJ2513 Colored Sticks(Trie+欧拉回路)
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...
- POJ2513:Colored Sticks(字典树+欧拉路径+并查集)
http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...
- POJ2513 Colored Sticks(欧拉)
题目链接. 题目大意: 给很多木棍,两端被涂了颜色.任意两根木棍的相同颜色处可以拼接在一起,问有没有可能将所有的木棍都连起来,成一条直线? 分析: 考点,欧拉道路. 将一根木棍看成一条边,两端的颜色看 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
随机推荐
- javascript 全局对象--w3school
JavaScript全局对象 1. decodeURI()解析某个编码的URI. 2.decodeURInComponent()解析一个编码的URI组件. 3.encodeURI()把字符串编码为U ...
- asp.net mvc3登录验证
1,在web.config中 <system.web>节点下面增加: <authentication mode="Forms"> <forms na ...
- Win8.1开机黑屏一段时间才能登录
最近发现开机后有一段时间黑屏过后才能进人登录界面,并且时间越来越长,网上查询了很多方法都没有效果,只能自己找了. 网上有一种方法提到用msconfig诊断判断或者安全启动来查看是否有黑屏,于是试了一下 ...
- scrapy 和 scrapy_redis 安装
安装sqlslte,scrapy需要这个模块 yum install sqlite-devel python3.5 下载包自己编译安装 ./configure make make install 自带 ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
- 更加精确的定时器:dispatch_source_t
在使用定时器时,我们经常使用NSTimer,但是由于NSTimer会受RunLoop影响,当RunLoop处理的任务很多时,就会导致NSTimer的精度降低,所以在一些对定时器精度要求很高的情况下,应 ...
- Swift库运行崩溃
报错如下: 解决方法: 退出 Xcode 找到 DerivedData 文件夹 删除 (路径: ~/Library/Developer/Xcode/DerivedData) 删除 com.apple. ...
- C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误。
本人今天遇到的错误,C#使用HttpWebRequest 进行请求,提示 基础连接已经关闭: 发送时发生错误. 测试了很久,才发现,是安全协议问题,把安全协议加上就可以了
- SDOI 2016 排列计数
题目大意:一个数列A,n个元素,其中m个元素不动,其他元素均不在相应位置,问有多少种排列 保证m个元素不动,组合数学直接计算,剩余元素错位排列一下即可 #include<bits/stdc++. ...
- poj1200-Crazy Search(hash入门经典)
Hash:一般是一个整数.就是说通过某种算法,可以把一个字符串"压缩" 成一个整数.一,题意: 给出两个数n,nc,并给出一个由nc种字符组成的字符串.求这个字符串中长度为n的不同 ...