poj 1386 并查集+有向欧拉通路

60 篇文章 0 订阅
32 篇文章 0 订阅

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11890 Accepted: 4061

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.



题意:

给出一系列单词,问是否可以通过重排,使得每一个单词的第一个字母和前一个单词的最后一个字母相同



题解:

使用有向欧拉通路进行判断是否可以得到有向欧拉通路,然后再判断是否是连通图


欧拉通路存在定理:

是连通图,并且每一个顶点的出度和入度都相同

是连通图,只有两个顶点的初度和入度只差分别为-1   1,其余的出度和入度都已相等的


欧拉回路存在定理:

是连通图,并且每一个顶点的出度和入度都相同



并查集判断连通图:

相同的字母之间是连通的,一个单词首尾字母不一样,就需要把这两个字母连通起来




#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 100005
struct Eage{
    int u,v;
}eage[MAXN];

int n;
int od[30],id[30];
bool letter[30];
int father[30];

void init()
{
    memset(od,0,sizeof(od));
    memset(id,0,sizeof(id));
    memset(letter,false,sizeof(letter));
    for(int i=0;i<=26;i++)
        father[i]=i;
}
int find_it(int x)
{
    int tempx=x,t;
    while(tempx!=father[tempx])
        tempx=father[tempx];
    while(father[x]!=x)
    {
        t=father[x];
        father[x]=tempx;
        x=t;
    }
    return tempx;
}

void unit(int num1,int num2)
{
    int tx=find_it(num1);
    int ty=find_it(num2);
    father[tx]=ty;
}

bool isconnect()
{
    int u,v;
    for(int i=0;i<n;i++){
        u=eage[i].u;
        v=eage[i].v;

        if(u!=v&&find_it(u)!=find_it(v))
            unit(u,v);
    }

    int first=-1,flag=1;
    for(int i=0;i<26;i++){
        if(letter[i]==false)
            continue;
        if(first==-1)
            first=i;
        if(find_it(first)!=find_it(i))
            flag=0;
    }

    if(flag)
        return true;
    return false;
}

int main()
{
    int T;
    char str[1010];
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",str);
            int len=strlen(str);
            eage[i].u=str[0]-'a';
            eage[i].v=str[len-1]-'a';

            od[eage[i].u]++;
            id[eage[i].v]++;

            letter[eage[i].u]=true;
            letter[eage[i].v]=true;
        }

        bool flag=true;
        int t1=0,t2=0;
        for(int i=0;i<26;i++){
            if(od[i]==id[i])
                continue;
            else if(od[i]-id[i]==1)
                t1++;
            else if(id[i]-od[i]==1)
                t2++;
            else{
                flag=false;
                break;
            }
        }
        if((t1+t2!=0)&&(t1!=1||t2!=1))
            flag=false;

        if(flag)
            flag=isconnect();

        if(flag)
            puts("Ordering is possible.");
        else
            puts("The door cannot be opened.");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值