HDU1116 Play on words(欧拉回路加并查集)

来源:互联网 发布:道德经足不出户知天下 编辑:程序博客网 时间:2024/06/10 01:41
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

32acmibm3acmmalformmouse2okok

Sample Output

The door cannot be opened.Ordering is possible.The door cannot be opened.

思路:

1.并查集判断连通
2.将每个单词取出首字母和尾字母,转换为一条边,然后加入对应的连通分量中。如果这个字母出现过,vis标记为1。同时起点出度加1,终点入度加1.

3.判断一下:

1)这个图必须是连通的,即根结点只有一个。如果不是,直接结束本次算法。

2)如果这个图是连通的,判断每个结点的入度和出度情况。

如果这个图是欧拉路,则每个顶点的出度等于入度。即out[i] = in[i]

如果这个图是半欧拉图,则起点的出度比入度大1,终点的入度比出度大1.其余顶点的出度等于入度。

如果满足上述条件,就可以将所有单词链接起来,否则不能。

当然,在判断出度入度的时候还有一点需要注意,那就是除了起点终点以外的顶点,出度必须等于入度(出度入度可以同时为2,即环),但是起点和终点必须保证出度和入度之差为1。

ps:以上思路是我借鉴网上大佬的,万分感谢网上大佬们的代码和解析~膜一下

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <set>#include <string>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>#include<cstdlib>using namespace std;#define M 30int pre[M];bool vis[M];int in[M], out[M];int find(int x){    int r = x;      //用r来传递信息    while(pre[r] != r)  //如果r的上级bushir自身        r = pre[r];     //r就接着找上级,直到找到老大    return r;           //告诉你x的老大是谁}void join(int x, int y)     //x,y成为朋友,于是他们两个的联盟合并了{    int fx = find(x);       //找x的老大    int fy = find(y);       //找y的老大    if(fx != fy){        pre[fx] = fy;       //x的老大成了y老大的小弟    }}int main(){    int n, m, a, b, i, j;    string s;    bool flag;      //判断连通性    bool flag1;     //判断入读和出度是否为1或0    int x, y;       //记录入度出度不相等顶点的个数    int root;       //记录根结点个数    cin >> n;    while(n--)    {        x = y = root = 0;        flag = flag1 = 1;        memset(vis, 0, sizeof(vis));        memset(in, 0, sizeof(in));        memset(out, 0, sizeof(out));        for(j=1; j<M; j++){            pre[j] = j;        }        cin >> m;        for(j=1; j<=m; j++){            cin >> s;            a = s[0] - 'a' + 1;            b = s[s.length()-1] - 'a' + 1;            vis[a] = 1;            vis[b] = 1;            out[a]++;            in[b]++;            join(a, b);        }        for(j = 1; j<M; j++){            if(vis[j]){                if(pre[j] == j)                    root++;                if(in[j] != out[j]){                    if(in[j] - out[j] == 1)                        x++;                    else if(out[j] - in[j] == 1)                        y++;                    else                        flag1 = 0;                }            }            if(root > 1){                flag = 0;                break;            }        }        if((flag && x == 0 && y == 0 && flag1) || (flag && x == 1 && y == 1 && flag1))            printf("Ordering is possible.\n");        else            printf("The door cannot be opened.\n");    }    return 0;}

我要再因为==出错,我就可以去死了!!!!

原创粉丝点击