//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!!

题意:给出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的更多相关文章

  1. 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 ...

  2. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  3. POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater

    水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algor ...

  4. 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 ...

  5. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  6. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  7. 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 ...

  8. 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 ...

  9. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

随机推荐

  1. Scala基础入门-代码碎片

    import scala.util.control._ import java.util.Date object Test { def main(args: Array[String]) { // v ...

  2. AsyncTask 不能与Thread.sleep()同时使用解决方案

    public class MainActivity extends Activity { private ImageView iv_ads; String urrstrString = "h ...

  3. Android05-UI02布局,自定义控件,ListView

    1.布局 布局的内部除了放置控件外,也可以放置布局,通过多层布局的嵌套,我们就能够完成一些 比较复杂的界面实现 ¨四种基本布局 LinearLayout RelativeLayout FrameLay ...

  4. java如何在一个有序的数组类插入一个数!

    第一种:依次与有序数组中的每个数进行比较,然后找到位置之后,定义一个新的数组,该信数组的长度加一,再使用system.arraycopy将于数组copy到新数组!import java.util.Ar ...

  5. block,inline,inline-block

    block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度 <div>, <p>, <h1>, ...

  6. Jdt Javax

    http://www.javablogging.com/dynamic-in-memory-compilation/ http://www.java2s.com/Code/Java/JDK-6/Com ...

  7. 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new

    引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...

  8. 可爱的 Python : Python中的函数式编程,第三部分

    英文原文:Charming Python: Functional programming in Python, Part 3,翻译:开源中国 摘要:  作者David Mertz在其文章<可爱的 ...

  9. Balls Rearrangement(HDU)

    Problem Description Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the ...

  10. 习题3.4 & 3.5: 求两链表的交集和并集

    #include<stdio.h> #include<stdlib.h> struct Node; typedef struct Node *PtrToNode; typede ...