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 streets form no cycles.
With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.
no_of_intersections
no_of_streets
S1 E1
S2 E2
......
Sno_of_streets Eno_of_streets
The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.
There are no blank lines between consecutive sets of data. Input data are correct.
2 4 3 3 4 1 3 2 3 3 3 1 3 1 2 2 3
2 1
题解:
题意:
有n个节点,给m个数据,x,y表示点x,y有一条有向边,然后可以从任意位置空降伞,伞兵只会走之前没有伞兵到过的地方,问最少多少个伞兵可以覆盖全点
思路:
这题如果不是二分匹配的专题我恐怕就用搜索做了。。。想了一会不知道如何用二分匹配。。然后就搜博客,以下是我的理解:问题可以转化为求最多解有多少个每两个点之间只有一个入度一个出度(也就是伞兵一路走下来不走回头路或者别人走过的路)然后用总点数减去能建成多少个这样的路的数目就是答案,感觉二分匹配就是建模很难,代码以一下就出来了,这里用匈牙利算法邻接表写法0ms,邻接矩阵写法是15ms
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
using namespace std;
#define lson k*2
#define rson k*2+1
#define M (t[k].l+t[k].r)/2
#define INF 1008611111
int used[155];
int vis[155];
int num[155];
int p[155][155],n,m;
int find(int u)//匈牙利算法邻接表写法
{
int i,v;
for(i=0;i<num[u];i++)
{
v=p[u][i];
if(!vis[v])
{
vis[v]=1;
if(!used[v]||find(used[v]))
{
used[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,k,test,x,y;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
memset(num,0,sizeof(num));
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
p[x][num[x]]=y;
num[x]++;
}
int ans=0;
memset(used,0,sizeof(used));
for(i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))
ans++;
}
printf("%d\n",n-ans);
}
return 0;
}