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

题目大意:你有好多根棍子,这些棍子的两端分都别涂了一种颜色。请问你手中的这些棍子能否互相拼接,从而形成一条直线呢?

两根棍子只有在颜色相同的时候才能拼接。比如有两根棍子,第一根棍子的两端的颜色分别为blue green,第二根两端的颜色为blue red,那么他们就可以拼接成green blue blue red或者red blue blue green。
解题思路:跟之前写的POJ1386很像,都是首尾连通。但是这里需要用字典树(不能用map会超时)对这些颜色单词做处理分配编号,然后这些颜色就是一个个节点了,就可以建图了,然后判断一下是不是欧拉回(通)路即可。注意,这里的木棍可以反转,所以是无向图,还有输入的字符串数目可能为0要特判一下输出“Possible”。
代码:
 #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+欧拉回(通)路判断)的更多相关文章

  1. POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  2. POJ - 2513 Colored Sticks(欧拉通路+并查集+字典树)

    https://vjudge.net/problem/POJ-2513 题解转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1304742541 题 ...

  3. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  4. [欧拉] poj 2513 Colored Sticks

    主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Tota ...

  5. poj 2513 Colored Sticks (trie树+并查集+欧拉路)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 40043   Accepted: 10406 ...

  6. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

  7. poj 2513 Colored Sticks (trie 树)

    链接:poj 2513 题意:给定一些木棒.木棒两端都涂上颜色,不同木棒相接的一边必须是 同样的颜色.求能否将木棒首尾相接.连成一条直线. 分析:能够用欧拉路的思想来解,将木棒的每一端都看成一个结点 ...

  8. POJ 2513 Colored Sticks (欧拉回路 + 字典树 +并查集)

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27097   Accepted: 7175 ...

  9. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

随机推荐

  1. 【bzoj4195】【NOI2015】程序自动分析

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3470  Solved: 1626[Submit][Sta ...

  2. profit

    Portal --> broken qwq Description 大M成立的怪兽电力公司最近刚建立了一个网站.这个季度共有\(n\)天,第\(i\)天共有\(v[i]\)个用户点击该网站,总的 ...

  3. 【树链剖分换根】P3979 遥远的国度

    Description zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcw ...

  4. tomcat8 的 websocket 支持

    使用 tomcat8 开发 WebSocket 服务端非常简单,大致有如下两种方式. 1.使用注解方式开发,被 @ServerEndpoint 修饰的 Java 类即可作为 WebSocket 服务端 ...

  5. mongodb replica set 和 nodejs中使用mongoose连接replica

    一.mongodb replication 介绍 官网上的第一句话就是Replication is the process of synchronizing data across multiple ...

  6. vue使用插件 使用库

    用插件1.引用import VueResource from 'vue-resource'2.使用Vue.use(VueResource); 用库(bootstrap alertify )1.引入: ...

  7. centos7下安装配置jenkins+git+maven+jdk

    环境 centos7 jdk1.8 maven3 git 在安装jenkins之前,先安装jdk1.8.maven.git 一. 安装jdk1.8 第一步:下载 jdk-8u131-linux-x64 ...

  8. extjs6需要引入文件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 无废话JavaScript(下)

    五.函数式 这个可不是JavaScript的发明,它的发明人已经死了,而他的这个发明还在困扰着我们……如同爱迪生的灯泡还在照耀着我们. 其实函数式语言很简单,它就是一种与命令式语言同样“完备”的语言实 ...

  10. javascript中各类的prototype属性

    prototype 作用:获取调用对象的对象原型引用 应用:可以为某对象原型添加方法 例: function getMax() { var max = this[0]; for(var x=0; x& ...