您的位置:首页 > 其它

poj1386 Play on Words (欧拉回路或欧拉路的判定)

2016-07-21 10:54 330 查看
Play on Words

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 11639Accepted: 3982
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.

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 Input3

2

acm

ibm

3

acm

malform

mouse

2

ok

ok

Sample OutputThe door cannot be opened.

Ordering is possible.

The door cannot be opened.

这道题判断是不是欧拉路,或者是欧拉通路

首先,这道题是有向图

关于有向图的判断:

欧拉回路:全部必须是偶数度节点才可以

欧拉通路(欧拉路):有2个节点,其中一个入度比出度多1,一个出度比入度多1,其余的

全部是偶数度节点。

当然,这个图必须联通才行。

#include<iostream>
using namespace std;
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>

const int maxn=1e5+5;
const int maxm=1e3+5;
#define read int Tcase;scanf("%d",&Tcase);while(Tcase--)
#define cls(x,y) memset((x),(y),sizeof((x)));
#define si(x) scanf("%d",&(x))
#define pb(x) push_back((x))

int n;
char in[maxm];
vector<int> vec[26];

int ind[26];
int outd[26];
int all,tr;

void dfs(int now){
int len=vec[now].size();
for(int j=0;j<len;++j){
int i=vec[now][j];
if(((all>>i)&1)&&(!((tr>>i)&1))){
tr|=(1<<i);
dfs(i);
}
}
}
int main() {
read{
si(n);
cls(ind,0)
cls(outd,0)
for(int i=0;i<26;++i){
vec[i].clear();
}
all=0;
int s=-1;
for(int i=0; i<n; ++i) {
scanf("%s",in);
int len=strlen(in);
all|=(1<<(in[0]-'a')),all|=(1<<(in[len-1]-'a'));
++outd[in[0]-'a'],++ind[in[len-1]-'a'];
vec[in[0]-'a'].pb(in[len-1]-'a');
vec[in[len-1]-'a'].pb(in[0]-'a');
s=(s==-1?(s=in[0]-'a'):s);
}
bool L=true;
tr=0;
dfs(s);
for(int i=0;i<26;++i){
if(((all>>i)&1)&&(!((tr>>i)&1))){
L=false;break;
}
}
int J=0,big=0,small=0;
for(int i=0; i<26&&L; ++i) {
if(ind[i]^outd[i]) {
++J;
if(ind[i]==outd[i]-1)++big;
else if(outd[i]==ind[i]-1)++small;
}
}
if(L&&((J==2&&big==1&&small==1)||(!J))) {
puts("Ordering is possible.");
} else{
puts("The door cannot be opened.");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: