POJ 1422【最小路覆盖数】

题意:

背景:

小镇有n个路口,空降兵可以在任意路口降落。有m条通往别的路口的单向边,但是不会出现循环。

问最少空降多少个士兵可以走完所有路口。

数据输入:

测试组数 t

每组有:

路口数 n

边数 m

接下来m组,每组a b代表a到b的单向边。

思路:

这是一个朴素的最小路覆盖数问题。

定理:最小路覆盖数=节点数-最大匹配数。

二分匹配的匈牙利算法不重复。

【菜鸟第一次写最大匹配,代码的错误在于当找到来源是本身的时候继续递归了】

/*************************************************************************
       > File Name: F.cpp
       > Author: ttpond
       > Created Time: 2015-8-17 9:55:53
************************************************************************/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
using namespace std;
vector<int>tmp[130];
bool vis[130];
int from[130];
int ans=0;
void dfs(int n)
{
    vector<int>::iterator it;
    for(it=tmp[n].begin();it!=tmp[n].end();it++)
    {
        if(!vis[*it])
        {
            from[*it]=n;
            vis[*it]=1;
            ans++;
            break;
        }
        else
        {
            if(from[*it]!=n)
                dfs(from[*it]);
        }
    }
}
int main()
{
    int t,tt,v,e;
    scanf("%d",&t);
    for(tt=0;tt<t;tt++)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&v,&e);
        for(int i=1;i<=v;i++)
            tmp[i].clear();
        for(int i=0;i<e;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            tmp[a].push_back(b);
        }
        for(int i=1;i<=v;i++)
            dfs(i);
        printf("%d\n",v-ans);
    }
    return 0;
}
时间: 2024-07-31 07:30:56

POJ 1422【最小路覆盖数】的相关文章

POJ 1422 DAG最小路径覆盖

求无向图中能覆盖每个点的最小覆盖数 单独的点也算一条路径 这个还是可以扯到最大匹配数来,原因跟上面的最大独立集一样,如果某个二分图(注意不是DAG上的)的边是最大匹配边,那说明只要取两个端点只要一条边即可. 故最小覆盖数还是 顶点数-最大匹配数 根据DAG建图的时候,就是DAG有边就给对应的端点建边 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int d[15

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 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

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

poj 1422 Air Raid (最小路径覆盖)

链接:poj 1422 题意:有n个点和m条有向边,现在要在点上放一些伞兵,伞兵可以沿着图走, 直到不能走为止,每条边有且仅有一个伞兵走过,问最少放多少个伞兵 思路:求的最小路径覆盖,用二分图来做 对于这样的一个有向图做最小路径覆盖,首先建图 然后每一条有向边对应左边的点指向右边的点 这样建好图之后求最大匹配数 最小路径覆盖=点数-最大匹配数 [cpp] view plaincopyprint? #include<stdio.h> #include<string.h> int n,

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

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

poj 1422

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6656   Accepted: 3966 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

POJ - 1422 Air Raid(DAG的最小路径覆盖数)

1.一个有向无环图(DAG),M个点,K条有向边,求DAG的最小路径覆盖数 2.DAG的最小路径覆盖数=DAG图中的节点数-相应二分图中的最大匹配数 3. /* 顶点编号从0开始的 邻接矩阵(匈牙利算法) 二分图匹配(匈牙利算法的DFS实现)(邻接矩阵形式) 初始化:g[][]两边顶点的划分情况 建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 g没有边相连则初始化为0 uN是匹配左边的顶点数,vN是匹配右边的顶点数 左边是X集,右边是Y集 调用:res=hungary();输

POJ 1422 Air Raid

题目链接: http://poj.org/problem?id=1422 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

poj 1422 Air Raid 最少路径覆盖

题目链接:http://poj.org/problem?id=1422 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 t