欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

German Collegiate Programming Contest 2015 :

程序员文章站 2022-07-15 16:10:19
...

Spike the bounty hunter is tracking another criminal through space. Luckily for him hyperspace travel has made the task of visiting several planets a lot easier. Each planet has a number of Astral Gates; each gate connects with a gate on another planet. These hyperspace connections are, for obvious safety reasons, one-way only with one gate being the entry point and the other gate being the exit point from hyperspace. Furthermore, the network of hyperspace connections must be loop-free to prevent the Astral Gates from exploding, a tragic lesson learned in the gate accident of 20222022 that destroyed most of the moon.

While looking at his star map Spike wonders how many friends he needs to conduct a search on every planet. Each planet should not be visited by more than one friend otherwise the criminal might get suspicious and flee before he can be captured. While each person can start at a planet of their choosing and travel along the hyperspace connections from planet to planet they are still bound by the limitations of hyperspace travel.

German Collegiate Programming Contest 2015 :

Input Format

The input begins with an integer NN specifying the number of planets (0 < N \le 1000)(0<N1000). The planets are numbered from 00 to N-1N1. The following NN lines specify the hyperspace connections.

The i-th of those lines first contains the count of connections KK (0\le K\le N-1)(0KN1) from planet ii followed by KK integers specifying the destination planets.

Output Format

Output the minimum number of persons needed to visit every planet.

样例输入1

4
1 1
1 2
0
1 1

样例输出1

2

样例输入2

6
0
1 2
2 4 5
1 2
0
0

样例输出2

4

了解下DAG的最小不相交路径覆盖

板子题

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1010;
static int map[maxn][maxn],visit[maxn],match[maxn];
int n;
bool dfs(int v)
{
     int i;
     for(i=0;i<n;i++)
     {
       if(map[v][i]&&!visit[i])
       {
          visit[i]=1;
          if(match[i]==-1||dfs(match[i]))
          {
            match[i]=v;
            return true;
          }
       }
     }
     return false;
}
            
int hungry()
{
    int i,ans=0;
    memset(match,-1,sizeof(match));
    for(i=0;i<n;i++)
    {
       memset(visit,0,sizeof(visit));
       if(dfs(i)) ans++;
    }
    return ans;
}
       
    
int main()
{
    int i,t,j,a,b,ans,m;
    scanf("%d",&n);
    memset(map,0,sizeof(map));
    for(int i=0;i<n;i++)
    {
    	int num;
    	scanf("%d",&num);
    	for(int j=0;j<num;j++){
    		int fz;
    		scanf("%d",&fz);
    		map[i][fz]=1;
		}
	}
    ans=n-hungry();
    printf("%d\n",ans);
    return 0;
}

欢迎大家加入 早起学习群,一起学习一起进步!(群号:642179511)