hdu 1116 Play on Word【并查集+欧拉路判断】

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6831    Accepted Submission(s): 2304


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

Source
Central Europe 1999

看着图论题大全找的并查集专题找到的这么个题,刚看到的时候真的没想过并查集要怎么来实现这么个东西,这么个字符串处理问题还能用上并查集?后来看到了N的数据范围、明显的暴力不能过,才慢慢开始研究起来路径关系,我们可以模拟一下这样一组样例得到的结果:

ab、bc、cd、de、

连接起来是:

a-b-b-c-c-c-d-e、我假设连接的节点是a、b。b、c。c、d和d、e。这个时候观察到有一个度的问题、因为是无向图,这里我们不能只考虑度,要区分入度和出度、这样我们就能判断的了起点和终点,以及连接问题。

所以这里我们这样判断就行:

1、所有节点的入度==出度,那么成环、可行

2、如果不是所有节点的入度==出度 ,那么我们就是不成环 ,如果可行的话,我们是知道是起点和终点的入度!=出度、并且这个时候要满足起点的出度-入度==1;并且终点的入度-出度==1;

如果不满足上述两个条件,那么就是不可行、

AC代码:
 

#include<stdio.h>
#include<string.h>
using namespace std;
//char str[100000][1000];
char str[1000];
int chu[100];//出度
int ru[100];//入度
int f[27];//father【】
int vis[28];//出现过的点记录、
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    {
        f[B]=A;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(degree,0,sizeof(degree));
        memset(ru,0,sizeof(ru));
        memset(chu,0,sizeof(chu));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<27;i++)
        {
            f[i]=i;
        }//初始化切记不要丢、
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            int u=str[0]-'a';
            int v=str[strlen(str)-1]-'a';
            merge(u,v);
            chu[u]++;
            ru[v]++;
            vis[u]=1;
            vis[v]=1;
        }
        int cont=0;
        for(int i=0;i<27;i++)
        {
            if(vis[i]==1)
            {
                if(f[i]==i)
                cont++;
            }
        }
        if(cont==1)
        {
            int qi=0;
            int zhong=0;
            int judge=0;
            for(int i=0;i<27;i++)
            {
                if(vis[i]==1)
                {
                    if(ru[i]!=chu[i])
                    judge++;
                    if(ru[i]-chu[i]==1)
                    zhong++;
                    if(chu[i]-ru[i]==1)
                    qi++;
                }
            }
            if(judge==0)
            {
                printf("Ordering is possible.\n");
            }
            else
            {
                if(qi==1&&zhong==1&&judge==2)
                {
                    printf("Ordering is possible.\n");
                }
                else
                {
                    printf("The door cannot be opened.\n");
                }
            }
        }
        else
        {
            printf("The door cannot be opened.\n");
        }
    }
}








你可能感兴趣的:(HDU,杭电,1116,1116)