您的位置:首页 > 理论基础 > 计算机网络

poj 1386 Play on Words(单向欧拉函数+并查集)

2015-09-24 21:02 645 查看
题目链接:
http://poj.org/problem?id=1386
Play on Words

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10871 Accepted: 3709
Description

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. 

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. 

Input

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

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

题目大意就是给你n个单词问你这n个单词能否首尾连接成一串,有点像成语接龙的感觉。。
思路就是用并查集判断是否联通,欧拉回路判断出度入度就好

AC代码:

v#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<utility>
using namespace std;
vector<pair<int ,int> >edge;
int in[30],out[30];
int set[30];
int used[30];
int n;
void init()
{
memset(used,0,sizeof(used));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<=26;i++)
set[i]=i;
edge.clear();
}
int find(int a)
{
int root=a;
int temp;
while(set[root]!=root)
root=set[root];
while(set[a]!=root)
{
temp=a;
a=set[a];
set[temp]=root;
}
return root;
}
bool merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)
return false;
set[x]=y;
return true;
}
bool eluer()
{
int ort,st,ed;
st=ed=0;
ort=-1;
for(int i=0;i<edge.size();i++)
merge(find(edge[i].first),find(edge[i].second));
for(int i=0;i<26;i++)
if(used[i])
{
if(ort==-1) ort=i;
if(abs(in[i]-out[i])>=2)
return false;
if(find(i)!=find(ort))
return false;
if(in[i]-out[i]==1)
{
ed++;
if(ed>1)
return false;

}
if(out[i]-in[i]==1)
{
st++;
if(st>1)
return false;
}
}
return true;
}
int main()
{
char s[1100];
int t;
scanf("%d",&t);
for(int kk=0;kk<t;kk++)
{
scanf("%d",&n);
init();
for(int i=0;i<n;i++)
{
scanf("%s",s);
int u=s[0]-'a';
int v=s[strlen(s)-1]-'a';
in[v]++,out[u]++;
used[v]=used[u]=1;
edge.push_back(make_pair(v,u));
}
if(!eluer())
printf("The door cannot be opened.\n");
else
printf("Ordering is possible.\n");

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 网络流