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 ...
随机推荐
- 使用Empire自动获取域管理员
使用Empire自动获取域管理员 译:backlion 前言 自从Empire和BloodHound被应用来,对AD渗透已经可以获取到内网环境95%的信息量.作者发现自己一遍又一遍地在做同样重复的事 ...
- Hive(二)hive的基本操作
一.DDL操作(定义操作) 1.创建表 (1)建表语法结构 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name[(col_name data_type ...
- Python3 字典 update() 方法
Python3 字典 描述 Python 字典 update() 函数把字典dict2的键/值对更新到dict里. 语法 update()方法语法: dict.update(dict2) 参数 di ...
- Hdu1542 Atlantis
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 「Django」rest_framework学习系列-路由
自动生成4个url路由:from rest_framework import routersrouter = routers.DefaultRouter()router.register(r'wrx' ...
- vim 常用快捷键(整理版)
最常用: x 删除后面的字符 X 删除前一个字符 删除3个字符就是3x dd:删除一行 D 删除到行尾 J:删除换行符,使下一行并上来. nJ:连接后面的n行 u:撤销上一次操作 ...
- CSS3实现文本垂直排列
最近的一个项目中要使文字垂直排列,也就是运用了CSS的writing-mode属性. writing-mode最初时ie中支持的一个属性,后来在CSS3中增添了这一新的属性,所以在ie中和其他浏览器中 ...
- 2049: [Sdoi2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 7475 Solved: 3499 [Submi ...
- 树链剖分处理+线段树解决问题 HDU 5029
http://acm.split.hdu.edu.cn/showproblem.php?pid=5029 题意:n个点的树,m次操作.每次操作输入L,R,V,表示在[L,R]这个区间加上V这个数字.比 ...
- TabLayout 使用方法 (基础)
此为布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:a ...