HDU 3472 HS BDC 混合图的欧拉路径

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3472

题意:给定n个单词,有些单词可以倒置,如果一个单词的尾部和另一个单词的首部一样,则可以把两个单词连在一起,问能不能把n个单词连在一起

思路:把每个单词看做一条边,单词的首尾看做两个点,可以得到一个图,于是问题就变成遍历这个图当且仅当经过每条边一次,就是一个欧拉路径问题,可以倒置的单词视为双向边,否则是单向边,就是混合图的欧拉路径问题。混合图的欧拉路径问题和欧拉回路问题很相似,于是可以转换成欧拉回路来做,首先找到欧拉路径的起点和终点(起点出度比入度大1,终点入度比出度大1),从终点到起点连一条容量为1的边,然后就和欧拉回路一样了

#include 
#include 
#include 
#include 
#include 
#define debug() puts("here");
using namespace std;

const int N = 50;
const int INF = 0x3f3f3f3f;
struct edge
{
    int to, cap, next;
} g[N*N*2];
bool vis[N];
int cnt, nv, ss, tt, head[N], level[N], gap[N], cur[N], pre[N];
int cas, out[N], in[N], par[N];
void init()
{
    for(int i = 0; i < N; i++) par[i] = i;
}
int ser(int x)
{
    int i = x, j, r = x;
    while(r != par[r]) r = par[r];
    while(i != r) j = par[i], par[i] = r, i = j;
    return r;
}
void unite(int x, int y)
{
    x = ser(x), y = ser(y);
    if(x == y) return;
    par[x] = y;
}
void add_edge(int v, int u, int cap)
{
    g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++;
    g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;
}
int sap(int s, int t)
{
    memset(level, 0, sizeof level);
    memset(gap, 0, sizeof gap);
    memcpy(cur, head, sizeof head);
    gap[0] = nv;
    int v = pre[s] = s, flow = 0, aug = INF;
    while(level[s] < nv)
    {
        bool flag = false;
        for(int &i = cur[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && level[v] == level[u] + 1)
            {
                flag = true;
                pre[u] = v;
                v = u;
                aug = min(aug, g[i].cap);
                if(v == t)
                {
                    flow += aug;
                    while(v != s)
                    {
                        v = pre[v];
                        g[cur[v]].cap -= aug;
                        g[cur[v]^1].cap += aug;
                    }
                    aug = INF;
                }
                break;
            }
        }
        if(flag) continue;
        int minlevel = nv;
        for(int i = head[v]; i != -1; i = g[i].next)
        {
            int u = g[i].to;
            if(g[i].cap > 0 && minlevel > level[u])
                minlevel = level[u], cur[v] = i;
        }
        if(--gap[level[v]] == 0) break;
        level[v] = minlevel + 1;
        gap[level[v]]++;
        v = pre[v];
    }
    return flow;
}
bool work()
{
    for(int i = 0; i < 26; i++)
        for(int j = i + 1; j < 26; j++)
            if(vis[i] && vis[j])
            {
                if(ser(i) != ser(j)) return false; //判断图的连通性
            }
    int st = -1, en = -1, cnt = 0;
    for(int i = 0; i < 26; i++)
        if(vis[i])
        {
            int tmp = (in[i] - out[i]) % 2;
            if(tmp == 1 || tmp == -1) //寻找起点终点,并统计入度 - 出度为奇数的点个数,若有欧拉路径,应该为0或2
            {
                cnt++;
                if(tmp == 1) st = i;
                else en = i;
            }
        }
    if(cnt == 0 || (cnt == 2 && st != -1 && en != -1)) //入度 - 出度为奇数的点个数为0时可能存在欧拉回路,为2可能存在欧拉路径
    {
        if(cnt == 2) add_edge(en, st, 1); //欧拉路径,从终点向起点连边使之变成欧拉回路
    }
    else return false;
    ss = 26, tt = 27;
    int sum = 0;
    for(int i = 0; i < 26; i++)
        if(vis[i])
        {
            if(in[i] > out[i]) add_edge(i, tt, (in[i] - out[i]) / 2), sum += (in[i] - out[i]) / 2;
            else add_edge(ss, i, (out[i] - in[i]) / 2);
        }
    nv = tt + 1;
    if(sap(ss, tt) == sum) return true; //判断满流
    else return false;
}
int main()
{
    int t, n, k;
    char s[110];
    scanf("%d", &t);
    while(t--)
    {
        init();
        cnt = 0;
        memset(head, -1, sizeof head);
        memset(in, 0, sizeof in);
        memset(out, 0, sizeof out);
        memset(vis, 0, sizeof vis);
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%s%d", s, &k);
            int c1 = s[0] - 'a', c2 = s[strlen(s)-1] - 'a';
            out[c1]++, in[c2]++;
            vis[c1] = vis[c2] = true;
            unite(c1, c2);
            if(k == 1) add_edge(c1, c2, 1);
        }
        if(! work()) printf("Case %d: Poor boy!\n", ++cas);
        else printf("Case %d: Well done!\n", ++cas);
    }
    return 0;
}


你可能感兴趣的:(最大流-最小割)