HDU 1116 && UVA 10129 && POJ 1386 ------ Play on Words(欧拉路径 + 并查集)

题意:词语接龙,看看所有单词能不嫩首尾相连, 其实就是一笔画问题,欧拉路径。

解题思路:首位字母映射为数字。变成从首至尾的一条有向边,加入并查集,使用并查集进行判断连通性。再根据欧拉路径的判定乱搞就好了。

转:关于欧拉回路和欧拉路径

定义:
欧拉回路:每条边恰好只走一次,并能回到出发点的路径
欧拉路径:经过每一条边一次,但是不要求回到起始点

①首先看欧拉回路存在性的判定:

一、无向图
每个顶点的度数都是偶数,则存在欧拉回路。

二、有向图(所有边都是单向的)
每个节顶点的入度都等于出度,则存在欧拉回路。

 三.混合图欧拉回路
  混合图欧拉回路用的是网络流。
  把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
  好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以2,得x。也就是说,对于每一个点,只要将x条边改变方向(入>出就是变入,出>入就是变出),就能保证出 = 入。如果每个点都是出 = 入,那么很明显,该图就存在欧拉回路。
  现在的问题就变成了:我该改变哪些边,可以让每个点出 = 入?构造网络流模型。首先,有向边是不能改变方向的,要之无用,删。一开始不是把无向边定向了吗?定的是什么向,就把网络构建成什么样,边长容量上限1。另新建s和t。对于入 > 出的点u,连接边(u, t)、容量为x,对于出 > 入的点v,连接边(s, v),容量为x(注意对不同的点x不同)。之后,察看是否有满流的分配。有就是能有欧拉回路,没有就是没有。欧拉回路是哪个?查看流值分配,将所有流量非 0(上限是1,流值不是0就是1)的边反向,就能得到每点入度 = 出度的欧拉图。
  由于是满流,所以每个入 > 出的点,都有x条边进来,将这些进来的边反向,OK,入 = 出了。对于出 > 入的点亦然。那么,没和s、t连接的点怎么办?和s连接的条件是出 > 入,和t连接的条件是入 > 出,那么这个既没和s也没和t连接的点,自然早在开始就已经满足入 = 出了。那么在网络流过程中,这些点属于“中间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反向之后,自然仍保持平衡。
  所以,就这样,混合图欧拉回路问题,解了。


②.欧拉路径存在性的判定

一。无向图
一个无向图存在欧拉路径,当且仅当   该图所有顶点的度数为偶数   或者  除了两个度数为奇数外其余的全是偶数。

二。有向图
一个有向图存在欧拉路径,当且仅当 该图所有顶点的度数为零     或者 一个顶点的度数为1,另一个度数为-1,其他顶点的度数为0。

三。混合图欧拉路径
其实整篇文章只有这部分是我写的哈,灰常不好意思,只是网上的同志们写的太好了,实在没有必要重复劳动,不知道大家有没有发现,求欧拉路径的第一步一定是求欧拉回路,在混合图上也不例外,如何判断混合图欧拉回路问题的存在性呢?首先,我们用上文所说的方法判断该图是否存在欧拉回路,如果存在,欧拉路径一定存在。如果欧拉回路不存在,那么我们枚举欧拉路径的起点和终点,连接一条无向边,然后再用最大流判断是否存在欧拉回路即可。


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.


#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)

int buf[10];
inline long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void writenum(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
//#define MAX_N
const int INF = 0x3f3f3f3f;
int parent[30];
int indegree[30];
int outdegree[30];
bool vis[30];


inline void init()
{
    memset(vis, false, sizeof(vis));
    memset(indegree, 0, sizeof(indegree));
    memset(outdegree, 0, sizeof(outdegree));
    for(int i = 0 ; i < 30 ; i++)
    {
        parent[i] = i;
    }
}

int find(int a)
{
    if(parent[a] != a)
    {
        parent[a] = find(parent[a]);
    }
    return parent[a];
}

void unite(int a, int b)
{
    int fa = find(a);
    int fb = find(b);
    if(fa != fb)
    {
        parent[fb] = fa;
    }
}

int main()
{
    int t = read();
    while(t--)
    {
        char str[1005];
        init();
        int n = read();
        for(int i = 0 ;i < n ; i++)
        {
            scanf("%s", &str);
            int len = strlen(str);
            int a = str[0] - 'a';
            int b = str[len - 1] - 'a';
//            cout<<a<<" "<<b<<endl;
            outdegree[a]++;
            indegree[b]++;
            vis[a] = true;
            vis[b] = true;
            unite(a, b);
        }
        int cnt = 0;
        int flag = 0;
        for(int i = 0 ; i < 30 ; i++)
        {
            if(vis[i] && find(i) == i)
            {
                cnt++;
            }
            if(cnt > 1)
            {
                flag = 1;
                break;
            }
        }
        if(flag)
        {
//            cout<<1<<endl;
            printf("The door cannot be opened.\n");
            continue;
        }
        int in = 0;
        int out = 0;
        for(int i = 0 ; i < 30 ; i++)
        {
            if(vis[i] && outdegree[i] != indegree[i])
            {
                if(outdegree[i] - indegree[i] == 1)
                {
                    if(out)
                    {
                        flag = 1;
                        break;
                    }
                    out = 1;
                }
                else if(indegree[i] - outdegree[i] == 1)
                {
                    if(in)
                    {
                        flag = 1;
                        break;
                    }
                    in = 1;
                }
                else
                {
                    flag = 1;
                    break;
                }
            }
        }
        if(flag)
        {
            printf("The door cannot be opened.\n");
        }
        else
        {
            printf("Ordering is possible.\n");
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值