HDU 4687 Boke and Tsukkomi 一般圖匹配

來源:互聯網
上載者:User
題目:

http://acm.hdu.edu.cn/showproblem.php?pid=4687 題意:

有n個女孩,她們之間有k對可能的組合,但參賽時每個人只能參加一個組合,在組合數量最大化時有些組合是多餘的,找出這些多餘的組合 思路:

剛開始我的思路是首先求出最大匹配,然後枚舉每條邊是不是多餘的,用去掉枚舉邊之後的圖求出最大匹配,和最初的最大匹配比較,若相等說明當前枚舉邊是多餘的。然而WA了,有資料如下:
5 4
1 2
2 3
3 4
4 5
此時跑出的答案即多餘邊是全部的邊,明顯是不對的。。。
換種思路,可以枚舉每條邊是不是在最大匹配中,把當前枚舉邊的兩個點刪除(即強制匹配這兩個點),用新圖求出最大匹配,若等於最初的最大匹配-1,則說明枚舉邊一定在最大匹配中,否則就是多餘邊。
帶花樹開花演算法蒟蒻很難理解,權當模板

#include <bits/stdc++.h>using namespace std;const int N = 210;struct edge{    int to, next;}g[N*N*2];int cnt, head[N];int que[N], rear;int match[N], mark[N], nxt[N], par[N], vis[N];int n, m, a[N], b[N];bool h[N][N];void add_edge(int v, int u){    g[cnt].to = u, g[cnt].next = head[v], head[v] = cnt++;}int ser(int x){    int r = x, i = x, j;    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;}int lca(int x, int y){    static int t = 0;    t++;    while(true)    {        if(x != -1)        {            x = ser(x); //點要對應到所在的花上去            if(vis[x] == t) return x;            vis[x] = t;            if(match[x] != -1) x = nxt[match[x]];            else x = -1;        }        swap(x, y); //交換,使兩點輪流走    }}void group(int x, int r){    while(x != r)    {        int y = match[x], z = nxt[y];        // next數組是用來標記花朵中的路徑的,綜合match數組來用,實際上形成了        // 雙向鏈表,如(x, y)是匹配的,next[x]和next[y]就可以指兩個方向了。        if(ser(z) != r) nxt[z] = y;        /* 奇環中的點都有機會向環外找到匹配,所以都要標記成S型點加到隊列中去,         因環內的匹配數已飽和,因此這些點最多隻允許匹配成功一個點,在aug中         每次匹配到一個點就break終止了當前階段的搜尋,並且下階段的標記是重         新來過的,這樣做就是為了保證這一點。*/        if(mark[y] == 2) mark[que[rear++] = y] = 1;        if(mark[z] == 2) mark[que[rear++] = z] = 1;        unite(x, y), unite(y, z);        x = z;    }}void augmented(int s){    for(int i = 1; i <= n; i++) //每次都要重設標記        nxt[i] = -1, par[i] = i, mark[i] = 0, vis[i] = -1;    mark[s] = 1; //1代表S型,2代表T型    que[0] = s;    rear = 1;    for(int i = 0; match[s] == -1 && i < rear; i++)    {        int v = que[i];        for(int j = head[v]; j != -1; j = g[j].next)        {            int u = g[j].to;            if(match[v] == u) continue; //v,u已經匹配,跳過            if(ser(v) == ser(u)) continue; //在同一朵花中,跳過            if(mark[u] == 2) continue; //T型,跳過            if(mark[u] == 1) //S型,奇環縮點            {                int r = lca(v, u); // r為從v和u到s的路徑上的第一個公用節點                if(ser(v) != r) nxt[v] = u; // r和v不在同一個花朵,nxt標記花朵內路徑                if(ser(u) != r) nxt[u] = v; // r和u不在同一個花朵,nxt標記花朵內路徑                //將整個r-x-y-r的奇環縮成點,r作為這個環的標記節點                group(v, r), group(u, r);// 縮路徑r-v和r-u為點            }            else if(match[u] == -1) //u自由,可以增廣            {                nxt[u] = v;                for(int k = u; k != -1;) //交錯路取反                {                    int x = nxt[k];                    int tm = match[x];                    match[x] = k, match[k] = x;                    k = tm;                }                break; // 搜尋成功,退出迴圈將進入下一階段            }            else            { // 當前搜尋的交叉鏈+u+match[u]形成新的交錯路,將match[u]排入佇列作為待搜節點                nxt[u] = v;                mark[que[rear++] = match[u]] = 1;                mark[u] = 2;            }        }    }}int work(){    memset(match, -1, sizeof match);    for(int i = 1; i <= n; i++)        if(match[i] == -1) augmented(i);    int num = 0;    for(int i = 1; i <= n; i++)        if(match[i] != -1) num++;    return num/2;}int main(){    while(~ scanf("%d%d", &n, &m))    {        cnt = 0;        memset(head, -1, sizeof head);        for(int i = 0; i < m; i++)        {            scanf("%d%d", &a[i], &b[i]);            add_edge(a[i], b[i]), add_edge(b[i], a[i]);        }        int res = work(), num = 0;        int arr[N], k = 0;        for(int i = 0; i < m; i++)        {            cnt = 0;            memset(head, -1, sizeof head);            for(int j = 0; j < m; j++)            {                if(i == j) continue;                if(a[j] == a[i] || a[j] == b[i] || b[j] == a[i] || b[j] == b[i]) continue;//刪除枚舉邊的兩端點                add_edge(a[j], b[j]), add_edge(b[j], a[j]);            }            int tm = work();            printf("%d %d %d\n", i, tm, res);            if(tm != res-1) num++, arr[k++] = i;        }        printf("%d\n", num);        for(int i = 0; i < k; i++) printf("%d%c", arr[i]+1, i == k-1 ? '\n' : ' ');        if(! num) printf("\n");//沒有多餘邊時輸出空行,否則會PE    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.