首页 > 搜索 > DFS搜索 > HDU 4687-Boke and Tsukkomi-图-[解题报告]HOJ
2015
09-17

HDU 4687-Boke and Tsukkomi-图-[解题报告]HOJ

Boke and Tsukkomi

Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 152    Accepted Submission(s): 51



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.
Boke and Tsukkomi
 


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
思路:一般图匹配,用带花树匹配算法。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int MAXN=50;
deque<int> Q;
//g[i][j]存放关系图:i,j是否有边,match[i]存放i所匹配的点
bool g[MAXN][MAXN],inque[MAXN],inblossom[MAXN],vis[150];
int match[MAXN],pre[MAXN],base[MAXN];
int n,m,mmg;
vector<int>res;
struct node
{
    int u,v;
}com[150];
//找公共祖先
int findancestor(int u,int v){
    bool inpath[MAXN]={false};
    while(1){
        u=base[u];
        inpath[u]=true;
        if(match[u]==-1)break;
        u=pre[match[u]];
    }
    while(1){
        v=base[v];
        if(inpath[v])return v;
        v=pre[match[v]];
    }
}

//压缩花
void reset(int u,int anc){
    while(u!=anc){
        int v=match[u];
        inblossom[base[u]]=1;
        inblossom[base[v]]=1;
        v=pre[v];
        if(base[v]!=anc)pre[v]=match[u];
        u=v;
    }
}

void contract(int u,int v,int n){
    int anc=findancestor(u,v);
    memset(inblossom,0,sizeof(inblossom));
    reset(u,anc);reset(v,anc);
    if(base[u]!=anc)pre[u]=v;
    if(base[v]!=anc)pre[v]=u;
    for(int i=1;i<=n;i++)
        if(inblossom[base[i]]){
            base[i]=anc;
            if(!inque[i]){
                Q.push_back(i);
                inque[i]=1;
            }
        }
}

bool dfs(int S,int n){
    for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;
    Q.clear();Q.push_back(S);inque[S]=1;
    while(!Q.empty()){
        int u=Q.front();Q.pop_front();
        for(int v=1;v<=n;v++){
            if(g[u][v]&&base[v]!=base[u]&&match[u]!=v){
                if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);
                else if(pre[v]==-1){
                    pre[v]=u;
                    if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1;
                    else{
                        u=v;
                        while(u!=-1){
                            v=pre[u];
                            int w=match[v];
                            match[u]=v;
                            match[v]=u;
                            u=w;
                        }
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
int MMG()
{
    int ans=0;
    memset(match,-1,sizeof(match));
    for(int i=1;i<=n;i++)
        if(match[i]==-1&&dfs(i,n)) ans++;
    return ans;
}
int main()
{
    int a,b;
    while(~scanf("%d%d",&n,&m))
    {
        memset(g,false,sizeof(g));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            g[a][b]=g[b][a]=true;
            com[i].u=a;
            com[i].v=b;
        }
        mmg=MMG();
        res.clear();
        for(int i=1;i<=m;i++)
        {
            a=com[i].u;
            b=com[i].v;
            memset(g,false,sizeof(g));
            for(int j=1;j<=m;j++)
            if(j!=i)
            {
                int x=com[j].u,y=com[j].v;
                if(x==a||x==b||y==a||y==b) continue;
                g[x][y]=g[y][x]=true;
            }
            int t=MMG();
            if(t!=mmg-1) res.push_back(i);
        }
        printf("%d\n",res.size());
        if(res.size())
        printf("%d",res[0]);
        for(int i=1;i<res.size();i++)
        printf(" %d",res[i]);
        printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/weyuli/article/details/10141881


, ,