//用map映射TLE,字典树就AC了
#include"cstdio"
#include"set"
using namespace std;
const int MAXN=;
const int N=;//26个小写英文字母
struct node{
int val;//存放字符串的hash值
node* next[N];
};
node memory[MAXN];
int ant;
node* root;
node* create_tree()
{
node* p=&memory[ant++];
for(int i=;i<N;i++)
{
p->next[i]=NULL;
}
return p;
} void insert(char *s,int x)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=create_tree();
p=p->next[k];
}
p->val=x;
} int search(char *s)
{
node* p=root;
for(int i=;s[i];i++)
{
int k=s[i]-'a';
if(p->next[k]==NULL) return -;
p=p->next[k];
}
return p->val;
} int par[MAXN];
int rnk[MAXN];
void init()
{
for(int i=;i<=MAXN;i++)
{
par[i]=i;
rnk[i]=;
}
} int fnd(int x)
{
if(par[x]==x)
{
return x;
}
return par[x]=fnd(par[x]);
} void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
if(a==b) return; if(rnk[a]<rnk[b])
{
par[a]=b;
}
else
{
par[b]=a;
if(rnk[a]==rnk[b]) rnk[a]++;
} } int deg[MAXN];
int cnt;
/*
bool judge()
{
int odd=0;
for(int i=0;i<cnt;i++)
{
if(deg[i]%2==1) odd++;
}
if(odd!=0&&odd!=2) return false;
int f=fnd(0);
for(int i=1;i<cnt;i++)
{
if(f!=fnd(i)) return false;
}
return true;
}
*/
int main()
{
root=create_tree();
init();
char a[];
char b[];
int numer=;
while(scanf("%s%s",a,b)!=EOF)
{
if(a[]=='') break;
int y1,y2;
y1=search(a);
y2=search(b);
if(y1==-)
{
y1=cnt;
insert(a,y1);
cnt++;
}
if(y2==-)
{
y2=cnt;
insert(b,y2);
cnt++;
}
deg[y1]++;
deg[y2]++;
unite(y1,y2);
} int l=;
set<int> vec;
for(int i=;i<cnt;i++)
{
if(deg[i]%==) l++;
}
if((l==)||l==) //满足欧拉回路
{
for(int i=;i<cnt;i++)
{
int x=fnd(i);
vec.insert(x);
}
if(vec.size()<=) //满足图的连通性
{
printf("Possible\n");
return ;
}
}
printf("Impossible\n"); return ;
}

POJ2513(字典树+图的连通性判断)的更多相关文章

  1. poj2513字典树+欧拉图判断+并查集断连通

    题意:俩头带有颜色的木棒,要求按颜色同的首尾相连,可能否? 思路:棒子本身是一条边,以俩端为顶点(同颜色共点),即求是否有无向图欧拉路(每条棒子只有一根, 边只能用一次,用一次边即选一次棒子). 先判 ...

  2. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  3. HDU-2072-单词数(字典树)

    链接: https://vjudge.net/problem/HDU-2072 题意: lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面 ...

  4. leetcode之820. 单词的压缩编码 | python极简实现字典树

    题目 给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A. 例如,如果这个列表是 ["time", "me", "bell& ...

  5. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

  6. POJ-2513 Colored Sticks---欧拉回路+并查集+字典树

    题目链接: https://vjudge.net/problem/POJ-2513 题目大意: 给一些木棍,两端都有颜色,只有两根对应的端点颜色相同才能相接,问能不能把它们接成一根木棍 解题思路: 题 ...

  7. POJ2513:Colored Sticks(字典树+欧拉路径+并查集)

    http://poj.org/problem?id=2513 Description You are given a bunch of wooden sticks. Each endpoint of ...

  8. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  9. POJ2513 欧拉 + 字典树

    POJ 2513 有N根木棒,一根木棒有2头,我们把每头涂色(相同或不同),如果2根木棒有相同颜色的一端就可以连接,颜色全部不同就不能连接,现在给你N根木棒以及它们的颜色,问最后能不能链接成1条链. ...

随机推荐

  1. python 列表中字符串排序故事一则

    a = ["bca","cab","abc"] 有时候需要对列表排序 如果是对列表中整个元素 直接用sort()排序 如果想按元素的某一段排 ...

  2. WiX 中XML引用变量说明

    WiX 安装工程中的XML 文件所引用变量说明: The WiX project supports the following project reference variables: Variabl ...

  3. anaconda + opencv3

    直接运行 pip install opencv-python 或者 pip install opencv-contrib-python 参照如下网页 https://blog.csdn.net/sin ...

  4. 读a paper of ICCV 2017 : Areas of Attention for Image Captioning

    前言废话,作者说把代码公布在gitub上,但是迟迟没有公布,我发邮件询问代码情况,邮件也迟迟不回,表示很尴尬..虽然种种这些,但是工作还是好工作,这个没的黑,那我们今天就来详细的介绍这篇文章. 导论: ...

  5. rails数据验证

    @user1 = :name => "zhou" 与 @user2 = :name=> "ZHOU" 在为保存之前都有可能通过第一关validate ...

  6. 分布式数据库对比评测(Es,mongodb,redis)基础知识篇

    前言 我建议大家看下这个,否则后面你不知道我在说什么. 1.ES数据库相关概念 啥是Es,说白了就是支持文档搜索的分布式数据库,专门方便搜索的,GITHUB京东现在都在用. 1.ES的数据库存放在哪里 ...

  7. mysql管理工具之pt-heartbeat

    之前我一直用Seconds_behind_master来衡量主从的延迟,今天看到文档,才觉得多么不可靠!以下是官方文档的描述: In essence, this field measures the ...

  8. 【Flask】Column常用参数

    ### Column常用参数:1. primary_key:设置某个字段为主键.2. autoincrement:设置这个字段为自动增长的.3. default:设置某个字段的默认值.在发表时间这些字 ...

  9. POJ 1068 Parencodings (类似括号的处理问题)

                                                                                                    Pare ...

  10. HDU 之 I Hate It

                                                                                    I Hate It Time Limit ...