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

Play on Words HDU - 1116 (并查集 + 欧拉通路)

时间:2019-01-28 19:12:58      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:must   div   imp   %s   ret   arch   important   不同的   math   

 Play on Words HDU - 1116 

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


题意:给你一些英文字母,不同的英文字母之间可以首位连接,只要字母一样,问可不可以构成一个完整的一条链
题解:一开始想把每一个单词都看作是一个点来跑,但是很难实现(有点像哈密顿图),之后我就把英文的26个字母当作是点,然后按照单词来建边,当作欧拉通路来跑。
   有向的欧拉通路:只有两个节点的出入度不一样(一个出度比入度大一,一个入度比出度大一),其余所有的点的出入度都是一样的。同时还需要判断是不是一个连通图,那就是看是不是在同一个集合(根节点是不是同一个就可以了)
技术分享图片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn =  1e3+10;
const int mod = 1e9+7;
int par[30];
void init()
{
    for(int i=0;i<30;i++)
        par[i] = i;
}
int find(int x)
{
    return par[x]!=x ? find(par[x]) : x;
}
void combine(int a,int b)
{
    a = find(a);
    b = find(b);
    if(a != b)
        par[a] = b;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int st[30],en[30];
        memset(st,0,sizeof st);
        memset(en,0,sizeof en);
        int n;
        scanf("%d",&n);
        init();
        for(int i=0;i<n;i++)
        {
            char str[1010];
            scanf("%s",str);
            int a=str[0]-a;
            int b=str[strlen(str)-1]-a;
            st[a]++;
            en[b]++;
            combine(a,b);
        }
        int start;
        for(int i=0;i<26;i++)
        {
            if((st[i] || en[i]) && i == find(i))
            {
                //cout<<i<<endl;
                start = i;
                break;
            }
        }
        int ans = 0;
        bool connect = 1;
        for(int i=0;i<26;i++)
        {
            ans += abs(st[i]-en[i]);
            if((st[i] || en[i]) && start != find(i))
                connect = 0;
        }
        if(ans > 2 || connect == 0)
            puts("The door cannot be opened.");
        else
            puts("Ordering is possible.");
    }
}
View Code

 

Play on Words HDU - 1116 (并查集 + 欧拉通路)

标签:must   div   imp   %s   ret   arch   important   不同的   math   

原文地址:https://www.cnblogs.com/smallhester/p/10331292.html

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