LightOJ 1403 - Air Raid【二分匹配】

题目链接: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;
}

版权声明:转载请注明出处。

时间: 2024-11-02 14:52:15

LightOJ 1403 - Air Raid【二分匹配】的相关文章

poj 1422 Air Raid (二分匹配)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6520   Accepted: 3877 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

hdu 1151 Air Raid - 二分匹配

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's

hdu1151Air Raid (二分匹配,最小路径覆盖)

Problem Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersec

hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 题目大意: 城镇之间互相有边,但都是单向的,并且不会构成环,现在派伞兵降落去遍历城镇,问最少最少派多少人去 思路: 转化题意,求用最少的有向边覆盖点        -------->      最小边覆盖数目=顶点数-最大二分匹配数目 注意:这道题目中说得是有向无环的DAG,所以顶点数目不能按照2倍来计算 在DAG中我们假设点集为 -----> Ni     然后建立每个点对应的虚点 ---

hdoj 1151 Air Raid【匈牙利算法+二分最小顶点覆盖】

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3861    Accepted Submission(s): 2552 Problem Description Consider a town where all the streets are one-way and each street leads from one

POJ 1422 Air Raid(二分图匹配最小路径覆盖)

POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,可以使伞兵可以走到图上所有的点.且每个点只被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径覆盖,每个点拆成两个点,然后根据有向边连边,然后答案为n - 最大匹配数 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace

kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图. 1.一个二分图中的最大匹配数等于这个图中的最小点覆盖数 König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知道什么是最小点覆盖,我也在这里说一下:假如选

hdu1151 poj1422 最小路径覆盖.最大二分匹配

Air RaidTime Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 1422 Appoint description: Description Consider a town where all the streets are one-way and each street leads from one intersection to another.

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T