POJ 1386 Play on Words [欧拉路]
来源:互联网 发布:嘉兴菜鸟网络食堂 编辑:程序博客网 时间:2024/05/16 00:52
题意:字母若一首一尾相同,可相互连接,问是否能连成一串。
思路:若可连接,每个点的入度和出度相同,则为欧拉回路,仅有两个点(一个入度比出度多一,一个出度比入度多一),则为欧拉路。两者皆成立。
#include <iostream>#include <string.h>using namespace std;int in[30],out[30],father[30];bool is[30];int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x];}int main(){ int t,n,a,b; char c[1002]; bool f; cin>>t; while(t--){ int x=0,y=0; f=true; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(is,0,sizeof(is)); for(int i=0;i<26;i++) father[i]=i; cin>>n; while(n--){ cin>>c; a=c[0]-'a'; b=c[strlen(c)-1]-'a'; in[a]++; out[b]++; is[a]=1; is[b]=1; a=find(a); b=find(b); if(a!=b) father[b]=a; } int ii; for(int i=0;i<26;i++) if(is[i]){ ii=find(i); break; } for(int i=0;i<26;i++){ if(is[i]&&ii!=find(i)){ f=false; break; } } if(f){ for(int i=0;i<26;i++){ if(in[i]!=out[i]){ if(in[i]-out[i]==1) x++; else if(out[i]-in[i]==1) y++; else{ f=false; break; } } } if(f&&((x==1&&y==1)||(x==0&&y==0))) cout<<"Ordering is possible."<<endl; else cout<<"The door cannot be opened."<<endl; } else cout<<"The door cannot be opened."<<endl; } return 0;}/*测试:Sample Input32acmibm3acmmalformmouse2okokSample OutputThe door cannot be opened.Ordering is possible.The door cannot be opened.*/
- POJ 1386 Play on Words [欧拉路]
- poj 1386 Play on Words(欧拉路判断)
- poj 1386 Play on Words 欧拉路
- poj 1386 Play on Words 【图论-欧拉路】
- POJ 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- POJ 1386 Play on Words
- POJ 1386 Play on Words
- POJ-1386-Play on Words
- POJ 1386 Play on Words
- poj 1386 Play on Words
- poj 1386 Play on Words
- POJ 1386 Play on Words
- poj Play on Words(1386)
- APUE 笔记 守护进程
- java中的线程池
- strace用来监视“系统调用“
- Snmp Package API 常被人忽视的Agent开发
- 关于前++和后++的讨论
- POJ 1386 Play on Words [欧拉路]
- AES 加密解密 c#
- linux内核设计与实现摘录及 Linux的内核源代码 的阅读
- MAC OS 关于内存使用状态的问题
- vs 2005 Updating Intellisense...占用cpu资源过高的问题解决参考方案
- 一些库在各个项目经常重用.如jpeg,freetype,iconv 等.每次大型项目均会反复重用.因此按照原来项目规范,一般都放在项目libs目录下,但后来发现,每个项目都做重复操作.也完全没有必要.
- linux查看内核版本、系统版本、系统位数(32or64)
- win7申请高级证书时遇到正在下载ActiveX控件的问题解决
- 打开.dsw文件而不会覆盖先前文件?