hdu - 4687 Boke and Tsukkomi(一般图带花树匹配)

Boke and Tsukkomi
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 846 Accepted Submission(s): 249

Problem Description
A new season of Touhou M-1 Grand Prix is approaching. Girls in Gensokyo cannot wait for participating it. Before the registration, they have to decide which combination they are going to compete as. Every girl in Gensokyo is both a boke (funny girl) and a tsukkomi (straight girl). Every candidate combination is made up of two girls, a boke and a tsukkomi. A girl may belong to zero or more candidate combinations, but one can only register as a member of one formal combination. The host of Touhou M-1 Grand Prix hopes that as many formal combinations as possible can participate in this year. Under these constraints, some candidate combinations are actually redundant as it\’s impossible to register it as a formal one as long as the number of formal combinations has to be maximized. So they want to figure out these redundant combinations and stop considering about them.

Input
There are multiple test cases. Process to the End of File.
The first line of each test case contains two integers: 1 ≤ N ≤ 40 and 1 ≤ M ≤ 123, where N is the number of girls in Gensokyo, and M is the number of candidate combinations. The following M lines are M candidate combinations, one by each line. Each combination is represented by two integers, the index of the boke girl 1 ≤ Bi ≤ N and the index of the tsukkomi girl 1 ≤ Ti ≤ N, where Bi != Ti.

Output
For each test case, output the number of redundant combinations in the first line. Then output the space-separated indexes of the redundant combinations in ascending order in the second line.

Sample Input

4 4
1 3
2 3
2 4
3 1
6 6
1 2
3 2
3 4
5 2
5 4
5 6

Sample Output

1
2
3
2 4 5

题意:N个人,每个人有两种身份,然后进行匹配,文最大匹配情况下,哪些边是多余的
思路:求一边一般图最大匹配,然后枚举每条边强制将那条边上的两个点匹配(即删除),然后求最大匹配,如果是tot-1,那么这条边就可以存在于最大匹配中。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn = 50;
int N; //点的个数,点的编号从1到N
bool Graph[maxn][maxn];
int Match[maxn];
bool InQueue[maxn],InPath[maxn],InBlossom[maxn];
int Head,Tail;
int Queue[maxn];
int Start,Finish;
int NewBase;
int Father[maxn],Base[maxn];
int Count;
void Push(int u){
    Queue[Tail++] = u;
    InQueue[u] = true;
}
int Pop(){
    int res = Queue[Head++];
    return res;
}
int FindCommonAncestor(int u,int v){
    memset(InPath,false,sizeof(InPath));
    while(true){
        u = Base[u];
        InPath[u] = true;
        if(u == Start) break;
        u = Father[Match[u]];
    }
    while(true){
        v = Base[v];
        if(InPath[v])break;
        v = Father[Match[v]];
    }
    return v;
}
void ResetTrace(int u){
    int v;
    while(Base[u] != NewBase){
        v = Match[u];
        InBlossom[Base[u]] = InBlossom[Base[v]] = true;
        u = Father[v];
        if(Base[u] != NewBase) Father[u] = v;
    }
}
void BloosomContract(int u,int v){
    NewBase = FindCommonAncestor(u,v);
    memset(InBlossom,false,sizeof(InBlossom));
    ResetTrace(u);
    ResetTrace(v);
    if(Base[u] != NewBase) Father[u] = v;
    if(Base[v] != NewBase) Father[v] = u;
    for(int tu = 1; tu <= N; tu++){
        if(InBlossom[Base[tu]]){
            Base[tu] = NewBase;
            if(!InQueue[tu]) Push(tu);
        }
    }
}
void FindAugmentingPath(){
    memset(InQueue,false,sizeof(InQueue));
    memset(Father,0,sizeof(Father));
    for(int i = 1;i <= N;i++)
        Base[i] = i;
    Head = Tail = 1;
    Push(Start);
    Finish = 0;
    while(Head < Tail){
        int u = Pop();
        for(int v = 1; v <= N; v++){
            if(Graph[u][v] && (Base[u] != Base[v]) && (Match[u] != v)){
                if((v == Start) || ((Match[v] > 0) && Father[Match[v]] > 0)){
                    BloosomContract(u,v);
                } else if(Father[v] == 0){
                    Father[v] = u;
                    if(Match[v] > 0){
                        Push(Match[v]);
                    } else {
                        Finish = v;
                        return;
                    }
                }
            }
        }
    }
}
void AugmentPath(){
    int u,v,w;
    u = Finish;
    while(u > 0){
        v = Father[u];
        w = Match[v];
        Match[v] = u;
        Match[u] = v;
        u = w;
    }
}
void Edmonds(){
    memset(Match,0,sizeof(Match));
    for(int u = 1; u <= N; u++){
        if(Match[u] == 0){
            Start = u;
            FindAugmentingPath();
            if(Finish > 0)AugmentPath();
        }
    }
}
int getMatch(){
    Edmonds();
    Count = 0;
    for(int u = 1; u <= N;u++){
        if(Match[u] > 0)
            Count++;
    }
    return Count/2;
}

bool g[maxn][maxn];
pair<int,int>p[150];
int main(){
    int m;
    while(scanf("%d%d",&N,&m)!=EOF){
        memset(g,false,sizeof(g));
        memset(Graph,false,sizeof(Graph));
        int u,v;
        for(int i = 1;i <= m;i++){
            scanf("%d%d",&u,&v);
            p[i] = make_pair(u,v);
            g[u][v] = true;
            g[v][u] = true;
            Graph[u][v] = true;
            Graph[v][u] = true;
        }
        int cnt0 = getMatch();
        vector<int>ans;
        for(int i = 1;i <= m;i++){
            u = p[i].first;
            v = p[i].second;
            memcpy(Graph,g,sizeof(g));
            for(int j = 1;j <= N;j++)
                Graph[j][u] = Graph[u][j] = Graph[j][v] = Graph[v][j] = false;
            int cnt = getMatch();
            if(cnt < cnt0-1)
                ans.push_back(i);
        }
        int sz = ans.size();
        printf("%d\n",sz);
        for(int i = 0;i < sz;i++){
            printf("%d",ans[i]);
            if(i < sz-1)printf(" ");
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值