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 ...
随机推荐
- tyvj1305 最大子序和 【单调队列优化dp】
描述 输入一个长度为n的整数序列,从中找出一段不超过M的连续子序列,使得整个序列的和最大. 例如 1,-3,5,1,-2,3 当m=4时,S=5+1-2+3=7 当m=2或m=3时,S=5+1=6 输 ...
- [ACM][2018南京预赛]Lpl and Energy-saving Lamps
一.题面 样例输入: 5 4 3 10 5 2 7 10 5 1 4 8 7 2 3 6 4 7 样例输出: 4 0 1 1 3 6 5 1 5 1 2 0 3 2 4 4 3 6 5 1 二.思路 ...
- C++之正则表达式20171121
准确来说,不论在C++或C中,只要在Linux系统中都可以使用本文讲诉的正则表达式使用方式. 一.Linux中正则表达式的使用步骤: 编译正则表达式 regcomp() 匹配正则表达式 regexec ...
- rename table table1 to table2;
1. MYSQL rename table table1 to table2;
- C语言 两个小知识点
strlen 函数原型 extern unsigned int strlen(char *s); 在Visual C++ 6.0中,原型为size_t strlen(const char *strin ...
- HTML+CSS基础小笔记再整理
1. font的两个必须要写的:font-size 和 font-family text-indent 首行缩进(em)1em=一个文字大小 text-algin 对齐方式:left.center.r ...
- Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令
Oracle中查询当前数据库中的所有表空间和对应的数据文件语句命令 ------------------------------------------------------------------ ...
- c# 计算星座
public string xz(DateTime birthday) { float birthdayF = 0.00F; if (birthday.Month == 1 && bi ...
- 2017 国庆湖南 Day4
期望得分:20+40+100=160 实际得分:20+20+100=140 破题关键: f(i)=i 证明:设[1,i]中与i互质的数分别为a1,a2……aφ(i) 那么 i-a1,i-a2,…… i ...
- Redis实战(五)CentOS 7上搭建Redis集群
高可用Redis(十二):Redis Cluster https://www.cnblogs.com/renpingsheng/p/9862485.html https://www.cnblogs.c ...