【luogu P2319 [HNOI2006]超级英雄】 题解

题目链接:https://www.luogu.org/problemnew/show/P2319

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2001;
int vis[maxn], a[maxn][maxn], link[maxn], linkk[maxn];
int n, m, u, v, ans;
bool dfs(int x)
{
    for(int i = 1; i <= n; i++)
    if(a[x][i]&&!vis[i])
    {
        vis[i] = 1;
        if(!link[i]||dfs(link[i]))
        {
            link[i] = x; linkk[x] = i;//link意义不变 linkk因为题目要求按原问题顺序输出,所以再单独记录 
            return 1;
        }
    }
    return 0;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d",&u,&v);
        a[i][u+1] = 1;
        a[i][v+1] = 1;
    }
    for(int i = 1; i <= m; i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ans++;
        else break;
    }
    printf("%d\n",ans);
    for(int i = 1; i <= m; i++)
    { 
        if(linkk[i]!=0)
        printf("%d\n",linkk[i]-1);
    }
    return 0;
}
posted @ 2018-06-14 19:37  Misaka_Azusa  阅读(172)  评论(0编辑  收藏  举报
Live2D