HDU1116 Play on Words 并查集+欧拉通路

Play on Words

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

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.
 

题意:给你一些英文单词,判断所有单词能不能连成一串。
题解:把单词开头结尾看做是一个点,也就是问存不存在一条通路,使得恰好经过每个边一次
可以发现这就是有向图欧拉通路的定义(首尾相连的是欧拉回路)
有向图的欧拉通路存在的充要条件是

1.D的基图(无向图)为连通图

2.每个点的入度等于出度(欧拉回路)或者除起点和终点每个点出入度相等且(终点出度-终点入度)*(起点出度-起点入度)== -1(欧拉通路)

出入度可以用in,out数组保存,判断连通可以用并查集或者BFS着色可以解决

#include<bits/stdc++.h>
#define N 45000
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
const long long mod = 1e9+7;
const int MAX = 0x7ffffff;
using namespace std;
int fa[100], in[100], out[100], dir[100];
char s[1200];
void init(int n){for(int i=1;i<=n;i++) fa[i] = i;}
int find(int n){return fa[n] == n?n:fa[n] = find(fa[n]);}
void add(int u,int v){
    int fau = find(u);
    int fav = find(v);
    if(fau != fav) fa[v] = fau;
}
int bcontinue(){
    int i, t, num=0;
    for(i=1;i<=26;i++)
        if(dir[i] && fa[i]==i)
            num++;
    if(num==1) return 1;
    return 0;
}
int term(){
    int i, num=0, t[30], e[30];
    for(i=1;i<=26;i++)
        t[i] = in[i]-out[i];
    for(i=1;i<=26;i++) 
        if(t[i] == 1||t[i] == -1) e[num++] = t[i];
        else if(t[i] != 0) return 0;
    if(num==0) return 1;
    if(num==1||num>2) return 0;
    if(e[0]*e[1] == -1) return 1;
    else return 0; 
}
int main()
{
    int T, t, i, n, l;
    scanf("%d", &T);
    while(T--){
    init(190);
    mes(in);mes(out);mes(dir);
    scanf("%d", &n);
    while(n--){
        
        scanf("%s", s);
        l = strlen(s);
        in[s[l-1]-'a'+1]++;
        out[s[0]-'a'+1]++;
        dir[s[l-1]-'a'+1] = dir[s[0]-'a'+1] = 1;
        add(s[0]-'a'+1, s[l-1]-'a'+1);
    }
    if(term()&&bcontinue()) printf("Ordering is possible.
");
    else printf("The door cannot be opened.
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Noevon/p/6281818.html