Hdu1151 air raid (directed graph least path overwrite)

Source: Internet
Author: User

Question:

Directed Graph with m edges at N points

It means to ask the minimum coverage

Ideas:

A directed graph is used to create a unidirectional edge, and Hungary calculates the maximum matching number.

Use the n-Max match.

/* ***********************************************Author        :devilCreated Time  :2016/5/17 11:55:14************************************************ */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <stdlib.h>using namespace std;const int N=125;int link[N];bool vis[N];vector<int>eg[N];bool dfs(int u){    for(int i=0;i<eg[u].size();i++)    {        int v=eg[u][i];        if(!vis[v])        {            vis[v]=1;            if(link[v]==-1||dfs(link[v]))            {                link[v]=u;                return 1;            }        }    }    return 0;}int main(){    //freopen("in.txt","r",stdin);    int t,n,m,x,y;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        memset(link,-1,sizeof(link));        for(int i=1;i<=n;i++)            eg[i].clear();        while(m--)        {            scanf("%d%d",&x,&y);            eg[x].push_back(y);        }        int ans=0;        for(int i=1;i<=n;i++)        {            memset(vis,0,sizeof(vis));            ans+=dfs(i);        }        printf("%d\n",n-ans);    }    return 0;}

 

Hdu1151 air raid (directed graph least path overwrite)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.