POJ-1386-Play on Words
来源:互联网 发布:钢琴键盘软件 编辑:程序博客网 时间:2024/05/20 19:32
回家轻松了很久很久了~~T_T,做了一个判断欧拉路径的题,因为可能存在自己连自己的情况,所以用并查集进行特殊处理~
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=1e5+100;int e,n,cntin[maxn],cntout[maxn],head[27],nxt[maxn],pnt[maxn],p[maxn];char str[1001];int find(int x){ if(p[x]==-1)return x; return p[x]=find(p[x]);}void AddEdge(int u,int v){ pnt[e]=v;nxt[e]=head[u];head[u]=e++;}void solve(){ int cnta=0,cntb=0; for(int i=0;i<26;i++) {if(cntin[i]==cntout[i]) continue;if(cntin[i]==cntout[i]+1){ if(++cnta>1) {puts("The door cannot be opened.");return; } continue;}if(cntin[i]==cntout[i]-1){ if(++cntb>1) {puts("The door cannot be opened.");return; } continue;}puts("The door cannot be opened.");return; } for(int j=0;j<26;j++) {int x=find(j);for(int i=head[j];i!=-1;i=nxt[i]){ int y=find(pnt[i]); if(x!=y)p[y]=x;} } int last; for(int i=0;i<26;i++)if(cntin[i]||cntout[i]){ last=find(i); break;} for(int i=0;i<26;i++) {if(!cntin[i]&&!cntout[i]) continue;int x=find(i);if(x!=last){ puts("The door cannot be opened."); return;} } puts("Ordering is possible.");}void read(){ int T; scanf("%d",&T); while(T--) { e=0; memset(p,-1,sizeof(p)); memset(head,-1,sizeof(head)); memset(cntin,0,sizeof(cntin)); memset(cntout,0,sizeof(cntout)); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",str); int u=str[0]-'a'; int v=str[strlen(str)-1]-'a'; cntin[u]++; cntout[v]++; AddEdge(u,v); } solve(); }}int main(){ read(); return 0;}
- 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)
- POJ-1386 Play on Words
- poj 1386 Play on Words
- POJ 1386 Play on Words
- POJ 1386 Play on Words
- IPC介绍——10个ipcs例子
- java代码收藏:获取HttpServletRequest中某一前缀的参数
- java转码问题解决---收集
- NSThread创建CFRunLoop控制动画 高级使用技巧(翻译)
- 给ecshop后台增加管理功能页面
- POJ-1386-Play on Words
- 土改悲剧——地主被消灭使中国农业产业化至今未果
- 算法学习笔记之计数排序
- java web 开发中各种注意点总结(转)-后期可能会出原创版
- uva 10057 A mid-summer night's dream.
- Spring中Quartz的配置
- 九度 题目1531 货币面值 (组合数学,母函数应用)
- tomcat 开机linux自动启动
- javascript核心