HDU 1116 Play on Words

BabbleDay posted @ 2013年8月31日 00:50 in 刷题防身 , 879 阅读

Play on Words

并查集+欧拉路

以下摘自http://blog.csdn.net/niushuai666/article/details/6917777

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

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

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

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

 三.混合图欧拉回路
  混合图欧拉回路用的是网络流。
  把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。
  好了,现在每个点入度和出度之差均为偶数。那么将这个偶数除以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

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

 

 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int degree[26], root[26];

int find(int x)
{
    int t=x;
    while(root[t]!=t)
        t = root[t];
    root[x] = t;
    return t;
}

void join(int x, int y)
{
    int tx = find(x); 
    int ty = find(y);
    if(tx!=ty) root[tx] = ty; // 这里注意了,不要写成root[x] = ty 。。
}

int main()
{
    int t, n, c, s, e, r;
    char word[100005];
    bool possible;
    bool vis[26];
    cin >> t;
    while(t--)
    {
        cin >> n;
        memset(degree, 0, sizeof(degree));
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<26; i++)
            root[i] = i;
        while(n--)
        {
            cin >> word;
            s = word[0]-'a';
            e = word[strlen(word)-1]-'a';
            degree[e]++;
            degree[s]--;
            join(s, e);
            vis[s] = true;
            vis[e] = true;
        }
        int n1=0,  p1=0;
        r = 0;
        possible = true;
        for(int i=0; i<26; i++)
        {
            if(vis[i])
            {
                if(root[i] == i) 
                {
                    r++;
                }
                if(degree[i] != 0)
                {
                    if(degree[i] == -1) n1++;
                    else if(degree[i] == 1) p1++;
                    else
                    {
                        possible = false;
                        break;
                    }
                }
            }
            if(r>1)
            {
                possible = false;
                break;
            }
        }
        if(possible && n1<2 && p1<2) cout << "Ordering is possible.\n";
        else cout << "The door cannot be opened.\n";
    }
    return 0;
}
Avatar_small
tenda 说:
2022年8月08日 23:10

Tenda is a popular brand that supplies various wireless routers that are well known for easy configuration of the WiFi network, and once a new router has been bought, one should make it ready to get it configured such that their LAN connection does move to WiFi to be used by multiple devices. tenda Tenda is the most preferred brand in customer premises wireless equipment due to its good range and stable connection, and their routers are found in 150Mbps and 300Mbps form which can be bought based on customer visibility.

Avatar_small
GSEB STD-4 Model Pap 说:
2022年9月11日 22:34

GSEB STD-4 Model Paper 2023 Pdf Download for Gujarat State Elementary Level Primary School 4th Class Question Paper Pdf with Answers for Gujarati Medium, Hindi Medium, English Medium & Urdu Medium Students of Gandhinagar Board at GSEB STD-4 Model Paper. Subject experts of the state and teaching staff of private schools have prepared and suggested the GSEB STD-4 Model Paper 2023 Pdf for Part-A, Part-B, Part-C and Part-D exams. Set wide solved question paper suggested as Gujarat Board 4th Class Model Paper 2023.

Avatar_small
Axis Bank Corporate 说:
2022年10月27日 18:59

Axis bank is a leading financial and banking service private sector bank in India. The bank serves millions of customers in the country, offering services such as net banking, loan services, transaction, etc.. Axis Bank Corporate Net Banking Login The axis bank corporate net banking services serve great purposes as clients don’t have to visit the bank for any transaction. Today we are focusing on the Axis corporate net banking a facility introduced for the corporate user. The bank ranks among the best SME in India, thus giving it a solid corporate base.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter