POJ 2513 欧拉路
来源:互联网 发布:广州淘宝拍摄基地地址 编辑:程序博客网 时间:2024/04/30 01:47
//今天下午,学一道欧拉,一道哈密尔顿。。再学一个生成树或最短路难题。。////欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次,//称这条回路为欧拉回路。具有欧拉回路的图成为欧拉图。//判断欧拉路是否存在的方法//有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。//无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的。//判断欧拉回路是否存在的方法//有向图:图连通,所有的顶点出度=入度。//无向图:图连通,所有顶点都是偶数度。//程序实现一般是如下过程:////1.利用并查集判断图是否连通,即判断可以作为起点的点的个数,如果大于1,说明不连通。//2.根据出度入度个数,判断是否满足要求。//3.利用dfs输出路径。//他还要用字典树过了。。还是要去写难题呀。。#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<algorithm>#include<vector>#define maxn 600000using namespace std;int color;int fa[maxn];class Tree_node{ public: Tree_node *next[27]; bool flag; int id; Tree_node() { memset(next,0,sizeof(next)); flag=0; id=0; }}root;int Hash(char *s){ int len=0; Tree_node *p=&root; while(s[len]!='\0') { int index=s[len++]-'a'; if(!p->next[index]) { p->next[index]=new Tree_node; } p=p->next[index]; } if(p->flag==false) { p->flag=true; p->id=color++; } return p->id;}int Find(int x){ if(fa[x]==-1) return x; else return fa[x]=Find(fa[x]);}int Union(int a,int b){ int ra=Find(a); int rb=Find(b); if(ra!=rb) { fa[ra]=rb; return true; } return false;}int du[maxn];int main(){// freopen("input.txt","r",stdin); color=0; memset(du,0,sizeof(du)); memset(fa,-1,sizeof(fa)); char a[100],b[100]; while(scanf("%s%s",&a,&b)!=EOF) {// cout<<"miao"<<endl; int ida=Hash(a);int idb=Hash(b); du[ida]++; du[idb]++;// cout<<ida<<" "<<idb<<endl; Union(ida,idb); }// for(int i=0;i<color;i++)// cout<<Find(i)<<endl; int ans=Find(0); bool flag=true; for(int i=1;i<color;i++) { if(Find(i)!=ans) { flag=false;break; } }// cout<<flag<<endl; int res=0; for(int i=0;i<color;i++) { if(du[i]%2==1) res++; } if(!(res==2 || res==0)) flag=false; if(flag) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl;}
0 0
- POJ 2513 欧拉路判定
- POJ 2513 欧拉路
- poj 2513 Colored Sticks 欧拉路
- POJ 2513
- poj 2513
- poj-2513
- POJ 2513
- POJ 2513
- poj 2513
- POJ 2513
- poj 2513
- POJ 2513
- POJ 2513
- poj 2513
- POJ 2513 trie+并查集+欧拉路
- POJ 2513 Colored Sticks 欧拉路+字典树
- [poj 2513] Colored Sticks (trie+欧拉路)
- POJ 2513 Colored Sticks
- HDU 3639 强连通加缩点
- Create groups 与 Create folder references
- Linux服务器性能评估
- 【转帖】易飞ERP 9.05版本 包含EasyFlow工作流注册机
- js公司测试题(公司做手游)
- POJ 2513 欧拉路
- SAP hybris企业级电商开发,Java程序员能做些什么?
- STL之string
- DFS Unique Binary Search Trees II
- 怎样从10亿查询词找出出现频率最高的10个
- C++作业4.21
- 简单返回顶部代码及注释说明
- matlab 与c++混合编码 (matlab .m文件转为c++的dll文件)
- POJ 3207 2_sat