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:
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:
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)
1 2
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)
1 2
二分图最小点覆盖:选择最少的点覆盖所有边,只要边的两端点之一被选择,那么等说这么边已经被覆盖了
这个题就是个裸的,只要这个图没有环就可以转化成二分图去做。
分好A,B队后就可以直接匈牙利了;
#include<bits/stdc++.h>
using namespace std;
const int M=1600;
struct aa
{
int u,v,next;
} edge[M*M];
int pre[M],team[M],n,cnt,mached[M],used[M];
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void dfs(int now,int fa,int step)
{
team[now]=step%2;
for(int i=pre[now]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(v!=fa)
dfs(v,now,step+1);
}
}
bool findx(int u)
{
for(int i=pre[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(used[v]==-1)
{
used[v]=0;
if(mached[v]==-1||findx(mached[v]))
{
mached[v]=u;
return 1;
}
}
}
return 0;
}
int hungary()
{
memset(mached,-1,sizeof(mached));
int ans=0;
for(int i=0; i<n; i++)
{
if(team[i]==1)
continue;
memset(used,-1,sizeof(used));
if(findx(i))ans++;
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(team,-1,sizeof(team));
cnt=0;
memset(pre,-1,sizeof(pre));
for(int i=0; i<n; i++)
{
int num,nod;
scanf("%d:(%d)",&nod,&num);
while(num--)
{
int temp;
scanf("%d",&temp);
add(nod,temp);
add(temp,nod);
}
}
for(int i=0; i<n; i++)
{
if(team[i]==-1)
dfs(i,i,0);
}
int ans=hungary();
printf("%d\n",ans);
}
}