LightOJ 1403 - Air Raid【二分匹配】

来源:互联网 发布:双色球绝密算法 编辑:程序博客网 时间:2024/06/06 04:00

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1403

代码:

#include <iostream>  #include <algorithm>  #include <set>  #include <map>  #include <string.h>  #include <queue>  #include <sstream>  #include <stdio.h>  #include <math.h>  #include <stdlib.h>  using namespace std;int n, m;int p[1010][1010];int book[1010];int match[1010];vector<int> g[1010];int dfs(int u){    int i;    for (i = 0; i < g[u].size(); i++)    {        int v = g[u][i];        if (book[v] == 0 && p[u][v] == 1)        {            book[v] = 1;            if (match[v] == 0 || dfs(match[v]))            {                match[v] = u;                return 1;            }        }    }    return 0;}int main(){    int t;    int cases = 1;    scanf("%d", &t);    while (t--)    {        scanf("%d%d", &n, &m);        for (int i = 0; i <= n; i++) g[i].clear();        int ans = 0;        memset(match, 0, sizeof(match));        memset(p, 0, sizeof(p));        int a, b;        while (m--)        {            scanf("%d%d", &a, &b);            p[a][b] = 1;            g[a].push_back(b);        }        for (int i = 1; i <= n; i++)        {            memset(book, 0, sizeof(book));            if (dfs(i))                ans++;        }        printf("Case %d: %d\n", cases++, n - ans);    }    return 0;}
0 0
原创粉丝点击