http://poj.org/problem?id=2513

73348K        1438MS        C++        1614B
解题思路:欧拉路的应用 要点 :1、判断连通性  2、欧拉路的判断(所有的节点的度为偶数或者只有两个奇数节点)

连通性的判断: 并查集-----由于本题的节点是字符串,,并不好处理,所以用Trie树来获得id。。然后 find 、unin 和普通并查集一样,。
连通性判断:并查集的祖先节点 ,,只有一个,若有多个即 不是连通图,也就不是欧拉路。。

 #include <iostream>
#include<cstring>
#include<cstdio>
#define maxn 500005
using namespace std; int f[maxn];
int degree[maxn];
int num;
struct node{
int id;
struct node *next[];
node(){
id =;
memset(next,,sizeof(next));
}
};
node *root = NULL; int maketrie(char *s){//用trie树获得id,,,其实就是给每个节点一个编号。。。用一般的方法不好解决,,就只能用trie树了
node *p = root;
node *temp = NULL;
for(int i=;i<strlen(s);i++){
if(p->next[s[i]-'a']==NULL){
temp = new node;
p->next[s[i]-'a']=temp;
}
p = p->next[s[i]-'a'];
}
if(p->id==)
p->id = ++num;
return p->id;
} int find(int x){
if(x!=f[x])
f[x] = find(f[x]);
return f[x];
} void unin(int x, int y){
int fx = find (x);
int fy = find(y);
if(fx==fy)
return ;
else{
f[fy] = fx;
}
}
int main()
{
for(int i=;i<maxn;i++)//注意此处。。。已经好几次。。i<=maxn了。。。。。runtime error。。。悲催啊。。
f[i] = i;
char s1[],s2[];
int id1,id2;
root = new node;
num =;
while(scanf("%s%s",s1,s2)==){
id1 = maketrie(s1);
id2 = maketrie(s2);
degree[id1]++;//记录其节点的度数
degree[id2]++;
unin(id1,id2);
}
int s = find();
int cnt =;
for(int i=;i<=num;i++){
if(degree[i]%==)
cnt++;
if(cnt>){
printf("Impossible\n");
return ;
}
if(find(i)!=s){//仅只有一个祖先节点,要不然就不是连通图。。就更不是欧拉路了。。
printf("Impossible\n");
return ;
}
}
if(cnt==){//cnt 有可能为1.。。所以特殊说明。。
printf("Impossible\n");
}
else{
printf("Possible\n");
} return ;
}

最新文章

  1. android官方下拉刷新控件SwipeRefreshLayout的使用
  2. uiautomator-----UiWatcher监听器
  3. android 利用View自身的setAnimation来实现动画
  4. 李洪强iOS开发之OC[009] -OC无参方法的声明实现和调用
  5. cocos2d-x触摸事件优先级的探究与实践
  6. cookie笔记(一)
  7. Quartz源码——QuartzSchedulerThread.run() 源码分析(三)
  8. UWP Flyout浮动控件
  9. [Kaggle] dogs-vs-cats之建立模型
  10. springboot中自定义根路径的配置
  11. .NET ORM框架 SqlSugar4.0 功能快速预览【开源】
  12. js 事件模型详解
  13. Python脱产8期 Day10 2019/4/24
  14. Boost 序列化
  15. 【MySql】join操作
  16. bind函数详解(转)
  17. Python连接MySQL的实例代码
  18. asp adodb.stream读取文件和写文件
  19. PRINCE2是什么?
  20. Scrum立会报告+燃尽图(Final阶段第三次)

热门文章

  1. ThinkPHP 3 的输出
  2. highlight a DOM element on mouse over, like inspect does
  3. Java中byte转int的方法
  4. ceph启动脚本
  5. 深入解读JavaScript面向对象编程实践
  6. java web分享ppt大纲 -- servlet容器简介
  7. 更改mysql 数据库名称
  8. connot find one or more components. please reinstall the application
  9. sql update 触发器 可获得被update的行的信息
  10. WINFORM Tootip使用小结