HDU1116(欧拉回路+并查集)
来源:互联网 发布:淘宝双十一抽奖在哪里 编辑:程序博客网 时间:2024/05/16 06:51
先用并查集来判断图是否连通,然后再根据欧拉回路的出度和入度的性质来判断是否为欧拉回路。
关键是建边,我们可以把字符串看成是一条边,首字母为出发点,尾字母为目的点,建边。
#include <stdio.h>#include <string.h>#include <string>#include <iostream>#include <algorithm>#include <vector>#include <math.h>#include <map>#include <queue>#include <stack>#include <set>#define M 100000+5#define LL long long#define Ld __int64#define eps 0.00001#define INF 999999999#define MOD 112233#define MAX 26#define PI acos(-1.0)using namespace std;int father[M];int into[M],out[M];set<int> s;int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x];}void merge(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) father[fx]=fy;}void init(){ for(int i=0;i<M;i++) { into[i]=0; out[i]=0; father[i]=i; } s.clear();}int main(){ int t; char ch[1005]; scanf("%d",&t); while(t--) { int n; init(); scanf("%d",&n); getchar(); for(int i=1;i<=n;i++) { scanf("%s",ch); int a=ch[0]-'a'+1; int l=strlen(ch); int b=ch[l-1]-'a'+1; into[b]++; out[a]++; int x=find(a); int y=find(b); if(x!=y) merge(x,y); s.insert(a); s.insert(b); } int num=0; set<int>::iterator it; for(it=s.begin();it!=s.end();it++) { if(father[*it]==*it && (into[*it] || out[*it])) num++; } if(num!=1) printf("The door cannot be opened.\n"); else { int f1=0,f2=0,f3=0; for(it=s.begin();it!=s.end();it++) { int p=*it; if((into[p]-out[p])==1) f1++; if((out[p]-into[p])==1) f2++; if(into[p]==out[p]) f3++; } if(f1==1 && f2==1 && (f1+f2+f3)==(s.size())) printf("Ordering is possible.\n"); else if(f1==0 && f2==0 && f3==s.size()) printf("Ordering is possible.\n"); else printf("The door cannot be opened.\n"); } } return 0;}
0 0
- HDU1116(欧拉回路+并查集)
- HDU1116-Play On Words 并查集,欧拉回路
- HDU1116-并查集&&欧拉路径
- UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words【欧拉回路+并查集】
- HDU1116(欧拉回路)
- HDU1116-欧拉回路
- hdu1116欧拉通路 回路
- hdu1116&&Uva10129 欧拉回路
- hdu1116(欧拉回路)
- hdu1878欧拉回路【并查集】
- 并查集+欧拉回路
- hdu3018并查集+欧拉回路
- 欧拉回路-并查集
- 并查集与欧拉回路
- UVa10129(欧拉回路,并查集)
- 欧拉路径,欧拉回路,并查集
- HDU1116 欧拉回路 解题报告
- HDu1116——Play on Words(并查集+欧拉路径)
- 为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?
- JDK环境变量配置—Ubuntu
- 编译 ioftpd v7.7.3
- C#.net web开发,母版页和内容页加载的顺序
- 2.8.等待多个并发完成
- HDU1116(欧拉回路+并查集)
- 第十一周项目1
- HDU 1850 Being a Good Boy in Spring Festival (Nim游戏变形)
- android.os.NetworkOnMainThreadException 异常
- [编程挑战]交替字符串,仅供参考
- virtual circuit wait等待事件
- python爬虫学习(一)
- Java基础部分笔记
- 谷歌招聘新员工五大标准:文凭一边玩去