HDU 1116 Play on Words(欧拉路径(有向图)+并查集)
来源:互联网 发布:分类信息群发软件 编辑:程序博客网 时间:2024/04/29 14:42
题目大意:给定若干个单词,问他们能不能连成为一个联通量(连接的方式是当前的单词第一个字母和上一个单词的最后一个字母相同),并且符合欧拉路
思路:(有向图的欧拉路问题)由题目大意得,每个单词的首尾才有用并且在'a'-'z'之间,所以可以由每个单词的两端来建图。并查集构造。
然后判断联通分量问题;之后再判断欧拉路,我们可以统计出入度和出度不相同的点的个数,若没有则符合要求。
若为2的话不一定满足分情况了,如ok ok不满足条件,若为 ac cs sd 则是满足条件的。
所以可以判定当某个点的 入度-出度=1,另一个出度-入度=1,才符合。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;typedef long long ll;bool vis[30];char s[1000010];int in[30],out[30],f[30];int fi(int x){ return x==f[x]?x:f[x]=fi(f[x]);}void mer(int x,int y){ int a=fi(x); int b=fi(y); if(a>b) f[a]=b; else f[b]=a;}int main(){ int n,m,k,i; int cla,x,y; scanf("%d",&cla); while(cla--) { for(i=0;i<30;i++) { vis[i]=false; in[i]=out[i]=0; f[i]=i; } scanf("%d",&n); getchar(); while(n--) { scanf("%s",s); int l=strlen(s); x=s[0]-'a'; y=s[l-1]-'a'; mer(x,y); in[x]++; out[y]++; vis[x]=vis[y]=true; } int tmp=0; for(i=0;i<30;i++) { if(f[i]==i&&vis[i]) { tmp++; if(tmp>1) break; } } if(tmp>1) { printf("The door cannot be opened.\n");continue; } int cnt=0; int p[30]; for(i=0;i<30;i++) { if(vis[i]&&in[i]!=out[i]) p[cnt++]=i; } if(cnt==0) { printf("Ordering is possible.\n"); } else if(cnt==2) { if(( out[p[0] ]-in[p[0] ]==1&&in[p[1]]-out[p[1] ]==1 )||(in[p[0] ]-out[p[0] ]==1&&out[p[1]]-in[p[1] ]==1) ) { printf("Ordering is possible.\n"); } else printf("The door cannot be opened.\n"); } else printf("The door cannot be opened.\n"); } return 0;}
0 0
- HDU 1116 Play on Words(欧拉路径(有向图)+并查集)
- [HDU 1116]Play on Words(欧拉回路/欧拉路径+并查集)
- HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)
- hdu oj 1116 Play on Words(并查集+欧拉路径)
- hdu 1116 Play on Words(并查集+欧拉回路|| 欧拉路径)
- 【并查集 欧拉路径】hdu 1116 Play on Words
- Play on Words(欧拉回路+并查集)
- HDu1116——Play on Words(并查集+欧拉路径)
- hdu 1116 Play on Words (欧拉路径)
- HDU Play on Words(并查集——欧拉通路)
- HDOJ 1116-Play on Words【欧拉路径+欧拉回路+并查集】
- HDU 1116 Play on Words (歐拉迴路 + 并查集)
- hdoj 1116 Play on Words 【并查集+欧拉】
- HDU 1116 Play on Words(并查集,欧拉回路)
- hdu 1116 Play on Words 并查集+欧拉通路
- POJ 1386 Play on Words(有向图欧拉路径并查集判定)
- HDU 1116 Play on Words(有向图欧拉路)
- 【欧拉路径(有向图)】poj 1386 play on words
- 程序设计基石与实践系列之C中的继承和多态
- JAVA爬虫Nutch、WebCollector的正则约束
- 网络管理Network
- 【leetcode】Best Time to Buy and Sell Stock II
- Anaconda python各模块简介
- HDU 1116 Play on Words(欧拉路径(有向图)+并查集)
- linux中对象文件的符号表symbols查看工具nm简介
- 程序设计基石与实践系列之编写高效的C程序与C代码优化
- 快速幂取模算法
- Java中读取图片和配置文件的写法
- SCU 3082 windy's cake II(容斥)
- Java中堆内存和栈内存详解
- javaSE多线程下载
- Swift-循环控制新特性