ACM: 图论题 poj 1…

来源:互联网 发布:java酒店管理系统程序 编辑:程序博客网 时间:2024/05/17 19:16
Air Raid
 
Description
Consider a town where all thestreets are one-way and each street leads from one intersection toanother. It is also known that starting from an intersection andwalking through town's streets you can never reach the sameintersection i.e. the town's streets form no cycles.

With these assumptions your task is to write a program that findsthe minimum number of paratroopers that can descend on the town andvisit all the intersections of this town in such a way that morethan one paratrooper visits no intersection. Each paratrooper landsat an intersection and can visit other intersections following thetown streets. There are no restrictions about the startingintersection for each paratrooper.

Input

Your program should read sets ofdata. The first line of the input file contains the number of thedata sets. Each data set specifies the structure of a town and hasthe 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 integerno_of_intersections (greater than 0 and less or equal to 120),which is the number of intersections in the town. The second linecontains a positive integer no_of_streets, which is the number ofstreets in the town. The next no_of_streets lines, one for eachstreet in the town, are randomly ordered and represent the town'sstreets. The line corresponding to street k (k <=no_of_streets) consists of two positive integers, separated by oneblank: Sk (1 <= Sk <=no_of_intersections) - the number of the intersection that is thestart of the street, and Ek (1 <= Ek<= no_of_intersections) - the number of theintersection that is the end of the street. Intersections arerepresented by integers from 1 to no_of_intersections.

There are no blank lines between consecutive sets of data. Inputdata are correct.

Output

The result of the program is onstandard output. For each input data set the program prints on asingle line, starting from the beginning of the line, one integer:the minimum number of paratroopers required to visit all theintersections 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

 

题意: 进行空袭, 要求用最少的伞兵, 可以走完全部的点.

 

解题思路: (简单题)

     1. 现在我们假设全部的点都放下伞兵, 当然初始状态全部覆盖了所有的点. 现在要选出一些

        边, 使得每个节点的出度和入度都不大于1. 因此问题可以转化为min = 节点-匹配边.

     2. 从公式可以得出当匹配边最大时, min最小. 即a->b连边, 经行最大匹配即可.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 128

int n, m;
int g[MAX][MAX];
int y[MAX];
bool vis[MAX];

bool find(int u)
{
 for(int v = 1; v <= n; ++v)
 {
  if( g[u][v]&& !vis[v] )
  {
   vis[v] =true;
   if( y[v] ==-1 || find(y[v]) )
   {
    y[v]= u;
    returntrue;
   }
  }
 }
 return false;
}

int Edmonds()
{
 int result = 0;
 memset(y, -1, sizeof(y));
 for(int u = 1; u <= n; ++u)
 {
  memset(vis, false,sizeof(vis));
  if( find(u) ) result++;
 }
 return result;
}

int main()
{
// freopen("input.txt", "r", stdin);
 int caseNum;
 scanf("%d", &caseNum);
 while( caseNum-- )
 {
  scanf("%d %d",&n, &m);
  memset(g, 0, sizeof(g));
  int u, v;
  for(int i = 1; i<= m; ++i)
  {
   scanf("%d%d", &u, &v);
   g[u][v] =1;
  }

  printf("%d\n",n-Edmonds());
 }
 return 0;
}

0 0
原创粉丝点击