uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!!
题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同
解:欧拉通路存在=底图联通+初度!=入度的点至多只有两个(分别为入点和出点)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<string> using namespace std; int T,n;
int in[],out[];
int f[];
char s[]; int gf(int x){
if (x==f[x]) return f[x];
f[x]=gf(f[x]);
return f[x];
} bool solve(){
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for (int i=;i<;i++){
f[i]=i;
}
scanf("%d",&n);
for (int i=;i<n;i++){
scanf("%s",s);
int f1=gf(s[]-'a');
int f2=gf(s[strlen(s)-]-'a');
if (f1!=f2){
f[f1]=f2;
}
in[s[]-'a']++;
out[s[strlen(s)-]-'a']++;
}
int pre=-;
for (int i=;i<;i++){
if (in[i]!= || out[i]!=){
if (pre==-)
pre=gf(i);
else{
if (pre!=gf(i)) return false;
pre=gf(i);
}
}
}
for (int i=;i<;i++){
in[i]=in[i]-out[i];
}
sort(in,in+);
for (int i=;i<;i++) if (in[i]!=) return false;
if (abs(in[])> || abs(in[])> || in[]+in[]!=) return false;
if (in[]+in[]!=) return false;
return true;
} int main(){
scanf("%d",&T);
for (int cas=;cas<=T;cas++){
if (solve())
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return ;
}
/*
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
*/
uva 10129 poj 1386 hdu 1116 zoj 2016 play on words的更多相关文章
- POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words Some of the secret doors contain a very interesting word puzzle. The team of archaeolo ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater
水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algor ...
- POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- POJ 1386 Play on Words(欧拉图的判断)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11838 Accepted: 4048 De ...
- POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)
POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
随机推荐
- WinCmd
Q1:tracert Tracert (跟踪路由)是路由跟踪实用程序,用于确定 IP 数据报访问目标所采取的路径. Tracert 命令用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确定 ...
- java版括号匹配检测
做一个空栈,读入字符直到结尾.如果读入一个封闭符号,空栈时报错;非空时弹出栈尾字符,如果不匹配则报错.否则读入为开放字符,压入栈中.最后如果栈空,返回true. 其中用到MyStack类,详情请见 p ...
- React 从0开始 消息传递
React笔记 React 数据决定DOM 以往的做法是通过JS去操作DOM 将数据填充 JSX Jsx javascript xml HTML的结构组装到js中 jsx使用style的时候 不能直接 ...
- windows下使用mingw编译python扩展模块
环境: 1.python2.7.2 2.mingw20120426 说明: PYTHON_HOME:不做特殊说明的话,默认代表python的安装路径. MINGW_HOME:不做特殊说明的话,默认代表 ...
- collection set
http://blog.csdn.net/humingfiy/article/details/7946408 Collection:List.SetMap:HashMap.HashTable 如何在它 ...
- Android——仿QQ聊天撒花特效
实现这样的效果,你要知道贝塞尔曲线,何谓贝塞尔曲线?其实就是曲线,嘿嘿,关于曲线的概念大家可以去 Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和 ...
- uva10934 Dropping water balloons
//好久没做题 一直没状态 然后刷了个水题玩玩 //寒假集训和校赛都做到了类似的题目 然而当时并不会 (其实现在也不会 题意:有k个气球和一个n层高的楼,气球有硬度,在某曾会恰好摔碎,问至少多少次实验 ...
- cf-公式专场
A. Again Twenty Five! time limit per test 0.5 seconds memory limit per test 64 megabytes input stand ...
- Android读写JSON格式的数据之JsonWriter和JsonReader
近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...
- scrapy爬虫初体验
scrapy是一个python的爬虫框架,用于提取结构性数据.在这次宝贝计划1的项目中要用到一些数据.但四处联系后各方可能因为一些隐私问题不愿提供数据信息.这样我们只能自己爬取,存入数据库,再进行调用 ...