Play on Words HDU - 1116
There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
InputThe input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.
OutputYour program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened. 题意:给你一些英文字母,不同的英文字母之间可以首位连接,只要字母一样,问可不可以构成一个完整的一条链
题解:一开始想把每一个单词都看作是一个点来跑,但是很难实现(有点像哈密顿图),之后我就把英文的26个字母当作是点,然后按照单词来建边,当作欧拉通路来跑。
有向的欧拉通路:只有两个节点的出入度不一样(一个出度比入度大一,一个入度比出度大一),其余所有的点的出入度都是一样的。同时还需要判断是不是一个连通图,那就是看是不是在同一个集合(根节点是不是同一个就可以了)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e3+;
const int mod = 1e9+;
int par[];
void init()
{
for(int i=;i<;i++)
par[i] = i;
}
int find(int x)
{
return par[x]!=x ? find(par[x]) : x;
}
void combine(int a,int b)
{
a = find(a);
b = find(b);
if(a != b)
par[a] = b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int st[],en[];
memset(st,,sizeof st);
memset(en,,sizeof en);
int n;
scanf("%d",&n);
init();
for(int i=;i<n;i++)
{
char str[];
scanf("%s",str);
int a=str[]-'a';
int b=str[strlen(str)-]-'a';
st[a]++;
en[b]++;
combine(a,b);
}
int start;
for(int i=;i<;i++)
{
if((st[i] || en[i]) && i == find(i))
{
//cout<<i<<endl;
start = i;
break;
}
}
int ans = ;
bool connect = ;
for(int i=;i<;i++)
{
ans += abs(st[i]-en[i]);
if((st[i] || en[i]) && start != find(i))
connect = ;
}
if(ans > || connect == )
puts("The door cannot be opened.");
else
puts("Ordering is possible.");
}
}