poj 1386 Play on Words(欧拉回路&&并查集)(中等)

Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10624   Accepted: 3603

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.

题意:

给你一些字符串,这些字符串可以首位相接(末位置如果和另一个字符串的首位置相同的话就可以相连) 。然后问你是否可以全部连起来。

思路:

取出每个字符串的首尾字母-‘a'并保存在数组里,然后求出数组每个点的出度和入度,根据有向欧拉通路的性质,可以求出是否可以组成欧拉通路 。

重点还得考虑一下这个图是否是连通图,这里可以用并查集记录边的集合。最后判断是否是一个连通图。

*我在并查集的合并那里wa,这里要比较两个数大小,或则加权法则。

代码:

//932K	313MS
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

char word[1005];
int bused[26];
int od[26],id[26];
int father[26];
struct edge
{
    int u,v;
}e[100005];
int n;

int Find(int x)
{
    int s;
    for(s=x;father[s]>=0;s=father[s]);
    while(s!=x)
    {
        int tmp=father[x];
        father[x]=s;
        x=tmp;
    }
    return s;
}

void Union(int R1,int R2)
{
    int r1=Find(R1),r2=Find(R2);
    int tmp=father[r1]+father[r2];
    if(father[r1]>father[r2])
    {
        father[r1]=r2;
        father[r2]=tmp;
    }
    else
    {
        father[r2]=r1;
        father[r1]=tmp;
    }
}

int connect()
{
    int u,v;
    for(int i=0;i<26;i++)
        father[i]=-1;
    for(int i=0;i<n;i++)
    {
        u=e[i].u;v=e[i].v;
        if(u!=v && Find(u)!=Find(v))
            Union(u,v);
    }
    int first=-1,i;
    for(i=0;i<26;i++)
    {
        if(!bused[i]) continue;
        if(first==-1) first=i;
        else if(Find(i)!=Find(first)) break;
    }
    if(i<26)
        return 0;
    else
        return 1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<26;i++)
        {
            od[i]=0,id[i]=0,bused[i]=0;
        }
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",word);
            int l=strlen(word);

            int a=word[0]-'a',b=word[l-1]-'a';
            e[i].u=a,e[i].v=b;
            od[a]++,id[b]++;
            bused[a]=bused[b]=1;
        }
        int flag=1;
        int sum1=0,sum2=0;
        for(int i=0;i<26;i++)
        {
            if(!bused[i]) continue;
            if(od[i]-id[i]>=2 || id[i]-od[i]>=2){flag=0;break;}
            if(od[i]-id[i]==1)
            {
                sum1++;
                if(sum1>1){flag=0;break;}
            }
            if(id[i]-od[i]==1)
            {
                sum2++;
                if(sum2>1){flag=0;break;}
            }
        }
        if(sum1!=sum2) flag=0;
        if(!connect()) flag=0;
        if(flag)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}

或者:

//932K	313MS
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

char word[1005];
int bused[26];
int od[26],id[26];
int father[26];
struct edge
{
    int u,v;
}e[100005];
int n;

int Find(int x)
{
    return x==father[x]?x:father[x]=Find(father[x]);
}

void Union(int a,int b)
{
    a=Find(a);
    b = Find(b) ;
    if(a==b)return ;
    if(a<b)father[b]=a;
    else father[a]=b;
}

int connect()
{
    int u,v;
    for(int i=0;i<26;i++)
        father[i]=i;
    for(int i=0;i<n;i++)
    {
        u=e[i].u;v=e[i].v;
        if(u!=v && Find(u)!=Find(v))
            Union(u,v);
    }
    int first=-1,i;
    for(i=0;i<26;i++)
    {
        if(!bused[i]) continue;
        if(first==-1) first=i;
        else if(Find(i)!=Find(first)) break;
    }
    if(i<26)
        return 0;
    else
        return 1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<26;i++)
        {
            od[i]=0,id[i]=0,bused[i]=0;
        }
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",word);
            int l=strlen(word);

            int a=word[0]-'a',b=word[l-1]-'a';
            e[i].u=a,e[i].v=b;
            od[a]++,id[b]++;
            bused[a]=bused[b]=1;
        }
        int flag=1;
        int sum1=0,sum2=0;
        for(int i=0;i<26;i++)
        {
            if(!bused[i]) continue;
            if(od[i]-id[i]>=2 || id[i]-od[i]>=2){flag=0;break;}
            if(od[i]-id[i]==1)
            {
                sum1++;
                if(sum1>1){flag=0;break;}
            }
            if(id[i]-od[i]==1)
            {
                sum2++;
                if(sum2>1){flag=0;break;}
            }
        }
        if(sum1!=sum2) flag=0;
        if(!connect()) flag=0;
        if(flag)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}





猜你喜欢

转载自blog.csdn.net/kaisa158/article/details/47317997
今日推荐