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

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.");
    }
}

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

时间: 2024-10-10 21:59:06

Play on Words HDU - 1116 (并查集 + 欧拉通路)的相关文章

一笔画问题 南阳oj42 【并查集+欧拉通路的判断】

描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整数N(N<=10)表示测试数据的组数. 每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线.(点的编号从1到P) 随后的Q行,每行有两个正整数A,B(0<A,B<P),表示编号为A和B的两点之间有连线. 输出 如果存在符合条件的连线

FZU2112 Tickets (并查集+欧拉通路)经典

Problem 2112 Tickets Accept: 309    Submit: 526 Time Limit: 3000 mSec    Memory Limit : 32768 KB Problem Description You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction betwe

hdu 1116(并查集+欧拉路径)

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7080    Accepted Submission(s): 2398 Problem Description Some of the secret doors contain a very interesting word puzzle. The team

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU 1512 并查集+左偏树

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3105    Accepted Submission(s): 1330 Problem Description Once in a forest, there lived N aggressive monkeys. At the beginning, they e

hdu 1829 并查集(食物链的弱化版)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 Problem Description Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

hdu 1856 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 13672    Accepted Submission(s): 5008 Problem Description Mr Wang wants some boys

HDU 1181 并查集 or SPFA

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4733 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规