POJ 2513 Colored Sticks(Tire+欧拉回(通)路判断)
题目链接:http://poj.org/problem?id=2513
题目大意:你有好多根棍子,这些棍子的两端分都别涂了一种颜色。请问你手中的这些棍子能否互相拼接,从而形成一条直线呢?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define CLR(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
const int M=5e5+; struct Tire{
Tire *next[];
int flag;
Tire(){
for(int i=;i<;i++)
next[i]=NULL;
flag=-;
}
};
int degree[M],fa[M],color; int Insert(char *str,Tire *&root){//要插入的字符串str
Tire *p=root;
for(int i=;str[i]!='\0';i++){
int k=str[i]-'a';
if(p->next[k]==NULL)
p->next[k]=new Tire;
p=p->next[k];
}
if(p->flag==-)
p->flag=++color;
return p->flag;
} void del(Tire *root){ //动态建树后要释放内存
for(int i=;i<;i++)
if(root->next[i])
del(root->next[i]);
delete(root);
} int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} int main(){
char str1[],str2[];
Tire *root=new Tire;
for(int i=;i<M;i++){
fa[i]=i;
}
while(~scanf("%s%s",str1,str2)){
int u,v;
u=Insert(str1,root);
v=Insert(str2,root);
degree[u]++;
degree[v]++;
if(find(u)!=find(v))
fa[find(u)]=find(v);
}
del(root);
bool flag=true;
int cnt1=,cnt2=,chu,ru;
for(int i=;i<=color;i++){
if(find(i)==i)
cnt1++;
if(degree[i]%==)
cnt2++;
}
//注意有字符串为0的情况,还有这是无向图
if((cnt1==||cnt1==)&&(cnt2==||cnt2==))
puts("Possible");
else
puts("Impossible");
return ;
}
POJ 2513 Colored Sticks(Tire+欧拉回(通)路判断)的更多相关文章
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)
https://vjudge.net/problem/POJ-2513 题解转载自:優YoU http://user.qzone.qq.com/289065406/blog/1304742541 题 ...
- poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)
题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- poj 2513 Colored Sticks (trie树+并查集+欧拉路)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 40043 Accepted: 10406 ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- poj 2513 Colored Sticks (trie 树)
链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...
- POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27097 Accepted: 7175 ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
随机推荐
- c语言中,指针加1的情况.指针变量详细介绍
指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址. 要搞清一个指针需要搞清指针的四方面的内容: 指针的类型, 指针所指向的 类型, 指针的值或者叫指针所指向的内存区, 还有指针本身所占 ...
- 【Nowcoder】玩游戏
Portal --> Nowcoder197D Solution 所以说这是一道==纯粹的人类智慧题是这样吗qwq 一开始的时候想sg函数qwq然后发现..好像根本不能拆成独立的子游戏嘛qw ...
- IT(然而其实是。。hdu5244?)
Time Limit: 3000 ms Memory Limit: 256 MB Description IT = Inverse Transform 两个长度为 \(2^n\) 的序列 \(a,b\ ...
- python基础----常用模块
一 time模块(时间模块)★★★★ 时间表现形式 在Python中,通常有这三种方式来表示时 ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C 并查集
C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 折腾到死:matlab7.0 安装
matlab7.0应该是2004年的东西了吧,装起来相当费劲!为什么不用更高的版本呢?其实我也想,之前安装的2013a安装包就5个多G,安装完之后就十多个G了.我习惯将软件安装到C盘,可怜我那100G ...
- C#中调用Dll动态链接库
C#中调用Dll动态链接库 起始 受限于语言的不同,我们有的时候可能会用别人提供的函数及方法 或者其他的什么原因.反正就是要调!!! 恰巧别人所使用的的语言跟自己又不是一样的 这个时候想要调用别人的函 ...
- 新式类 VS 经典类
一.概述 Python中支持多继承,也就是一个子类可以继承多个父类/基类.当一个调用一个自身没有定义的属性时,它是按照何种顺序去父类中寻找的呢?尤其是当众多父类中都包含有同名的属性,这就涉及到新式类 ...
- JS中encodeURI、encodeURIComponent、decodeURI、decodeURIComponent
js 对文字进行编码涉及2个函数:encodeURI,encodeURIComponent,相应2个解码函数:decodeURI,decodeURIComponent 1.用来编码和解码URI的 统一 ...
- 816D.Karen and Test 杨辉三角 规律 组合
LINK 题意:给出n个数,每个数对间进行加或减,结果作为下一层的数,问最后的值为多少 思路:首先我们发现很像杨辉三角,然后考虑如何计算每个数对结果的贡献值,找规律可以发现当数的个数为偶数时,其所在层 ...