Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
The input file contains several data sets in text format. Each data set represents a tree with the following description:
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:
the solution is one soldier ( at the node 1).
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:
Input
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)
Output
1
2
Sample Input
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)
Sample Output
1
2
给出一颗树,在一个顶点上放守卫可以保护所有与该点相连的边,现在求最少在几个点上放守卫就可以防御所有边。
根据题意,不难得出这是要求一个最小定点覆盖,而对于最小顶点覆盖,除了二分图可以算,其他的基本没戏。那我们不妨考虑一下,给出的图是一个二分图么?
给出的树首先确定了这个图里没有环,而且题目中给出的是一个无向图。我们可以通过对顶点归纳证明。
首先,将根节点放在一侧,假设放在左侧,再将与根节点相连的所有节点放在另一侧,也就是右侧,之后再将与新加入节点相连的点放在其对侧,不断重复,直到所有点都加入这两个集合。之后,因为树中不可能有环存在,所以对于除于根节点直接相连的顶点外的任意顶点来说,不存在一条边会与根节点相连(否侧将会产生环),进而可以得到,与根节点在同一集合中的点没有边相连,而于根节点直接相连的顶点又在其对侧的集合中,其他顶点于根节点的分析类似,以此类推,就可以证明,树中的点集,可以分为两互不相交的子集,故树是二分图。
既然树是二分图,那么根据二分图的性质
|最大匹配| == |最小顶点覆盖|
就将原问题转化为了求二分图的最大匹配,再套用匈牙利算法就可以做出来了。
#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
int match[1504];
bool used[1504];
vector<int> adj[1504];
bool dfs(int v);
int main(){
ios::sync_with_stdio(false);
int n;
char c;
while(cin>>n){
rep(i,0,n){
int s,len;
cin>>s>>c>>c>>len>>c;
rep(j,0,len){
int f;
cin>>f;
adj[s].Push(f);
adj[f].Push(s);
}
}
int ans=0;
memset(match,-1,sizeof(match));
rep(i,0,n){
if(match[i]<0){
memset(used,false,sizeof(used));
if(dfs(i))
ans++;
}
}
cout<<ans<<endl;
rep(i,0,n)
adj[i].clear();
}
re 0;
}
bool dfs(int v){
used[v]=true;
rep(i,0,adj[v].size()){
int u=adj[v][i],matched=match[u];
if(matched<0 || !used[matched] && dfs(matched)){
match[u]=v;
match[v]=u;
return true;
}
}
return false;
}