8.15 E

来源:互联网 发布:mac ubuntu 编辑:程序博客网 时间:2024/06/09 16:26

                                                                           E - Air Raid

 Consider a town where all the streets areone-way and each street leads from one intersection to another. It is alsoknown that starting from an intersection and walking through town's streets youcan 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 minimumnumber of paratroopers that can descend on the town and visit all theintersections of this town in such a way that more than one paratrooper visitsno intersection. Each paratrooper lands at an intersection and can visit otherintersections following the town streets. There are no restrictions about thestarting intersection for each paratrooper. 

Input

Your program should read sets of data. The first line ofthe input file contains the number of the data sets. Each data set specifiesthe structure of a town and has the format: 

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 intersectionsin the town. The second line contains a positive integer no_of_streets, whichis the number of streets in the town. The next no_of_streets lines, one foreach 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 twopositive integers, separated by one blank: Sk (1 <= Sk <=no_of_intersections) - the number of the intersection that is the start of thestreet, and Ek (1 <= Ek <= no_of_intersections) - the number of theintersection that is the end of the street. Intersections are represented byintegers from 1 to no_of_intersections. 

There are no blank lines between consecutive sets of data. Input data arecorrect. 

Output

The result of the program is on standard output. For eachinput data set the program prints on a single line, starting from the beginningof the line, one integer: the minimum number of paratroopers required to visitall the intersections in the town. 

Sample Input

2

4

3

3 4

1 3

2 3

3

3

1 3

1 2

2 3

Sample Output

2

1

 

题意:城镇里的街道从一个交叉口连接到另一个交叉口,街道都是单向的,并且从一个交叉口沿着街道出发不会回到相同的交叉口。伞兵降临在城镇的一个交叉口并可以沿着街道走向另一个没有被其他伞兵走过的交叉口,问城镇中的所有交叉口都被伞兵走过的情况下至少需要多少名伞兵输入一个数字T,表示T组数据,然后每组数据前两行分别为一个数字n,m,n代表n个顶点,m表示m条边,求出在连通情况下的最少路径覆盖。

思路:先将数据读成一张n*n大小的矩阵,0和1表示连通情况,然后用匈牙利算法算出能够连通的点组成的集合有多少组。

#include<stdio.h>#include<string.h>int s,a[150][150],match[150],book[150];int m,n;int dfs(int x){int i;for(i=1;i<=n;i++)//匈牙利算法{if(book[i]==0&&a[x][i]==1){book[i]=1;if(match[i]==-1||dfs(match[i])){match[i]=x;return 1;}}}return 0;}int main(){int i,j,k,t,sum,t1,t2;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);memset(a,0,sizeof(a));memset(match,-1,sizeof(match));for(i=0;i<m;i++){scanf("%d%d",&t1,&t2);a[t1][t2]=1;}/*for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%d ",a[i][j]);printf("\n");}*/sum=0;for(i=1;i<=n;i++){memset(book,0,sizeof(book));if(dfs(i))sum++;}printf("%d\n",n-sum);}return 0;}