码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1386Play on Words(欧拉回路)

时间:2016-08-15 10:20:16      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

                                                         Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11748   Accepted: 4018

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.
【题意】这个就相当于成语接龙,需满足前一个单词的尾字母与后一个的首字母相同。然后就是问是否 存在欧拉通路。
【分析】用两个数组存每个字母的入度与出度,再用vis[]判断字母是否出现,然后并查集判断是否联通。
技术分享
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=100005;
const int M=150005;
ll power(ll a,int b,ll c){ll ans=1;while(b){if(b%2==1){ans=(ans*a)%c;b--;}b/=2;a=a*a%c;}return ans;}
char str[N];
int n,m;
int vis[27],pre[27];
int out[27],in[27];
struct man
{
    int u,v;
}edg[N];
void init()
{
    memset(vis,0,sizeof(vis));
    memset(out,0,sizeof(out));
    memset(in,0,sizeof(in));
    for(int i=0;i<26;i++)pre[i]=i;
}
int Find(int x) {
    if(pre[x] != x) pre[x] = Find(pre[x]);
    return pre[x];
}

void Union(int x,int y) {
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    pre[y] = x;
}
bool beconnect()
{
    for(int i=0;i<n;i++){
        int u=edg[i].u,v=edg[i].v;
        if(u!=v&&Find(u)!=Find(v))Union(u,v);
    }
    int f=-1,k;
    for(k=0;k<26;k++){
        if(!vis[k])continue;
        if(f==-1)f=k;
        else if(Find(k)!=Find(f))break;
    }
    if(k<26)return false;
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",str);
            int u=str[0]-a,v=str[strlen(str)-1]-a;
            edg[i].u=u;edg[i].v=v;
            vis[u]=vis[v]=1;
            out[u]++;in[v]++;
        }
        bool flag=true;
        int cnt1=0,cnt2=0;
        for(int i=0;i<26;i++){
            if(!vis[i])continue;
            if(abs(out[i]-in[i])>1){
                flag=false;
                break;
            }
            if(out[i]-in[i]==1){
                cnt1++;
                if(cnt1>1){
                    flag=false;
                    break;
                }
            }
            if(out[i]-in[i]==-1){
                cnt2++;
                if(cnt2>1){
                    flag=false;
                    break;
                }
            }
        }
        if(cnt1!=cnt2)flag=false;
        if(!beconnect())flag=false;
        if(flag)puts("Ordering is possible.");
        else puts("The door cannot be opened.");
    }
    return 0;
}
View Code

 

POJ1386Play on Words(欧拉回路)

标签:

原文地址:http://www.cnblogs.com/jianrenfang/p/5771871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!