您的位置:首页 > 其它

HDU 1054 Strategic Game(二分图最小覆盖集)

2016-02-15 22:00 246 查看
题意:给你一颗具有N个节点的树的所有边信息.现在问你最少需要多少个点放到树的节点上,使得树的任意一条边都至少有一个端点被覆盖.(其实就是最小覆盖集)

思路:明显的最小覆盖集. 且由于树天然就是二分的,所以我们只需要求该树的最小覆盖集点数即可.

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=1500+5;

struct Max_Match
{
int n;
vector<int> g[maxn];
bool vis[maxn];
int left[maxn];

void init(int n)
{
this->n=n;
for(int i=0;i<n;i++) g[i].clear();
memset(left,-1,sizeof(left));
}

bool match(int u)
{
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
{
vis[v]=true;
if(left[v]==-1 || match(left[v]))
{
left[v]=u;
return true;
}
}
}
return false;
}

int solve()
{
int ans=0;
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(match(i)) ++ans;
}
return ans;
}
}MM;

int main()
{
int n;
while(scanf("%d",&n)==1)
{
MM.init(n);
for(int i=0;i<n;i++)
{
int u,num,v;
scanf("%d:(%d)",&u,&num);
while(num--)
{
scanf("%d",&v);
MM.g[u].push_back(v);
MM.g[v].push_back(u);
}
}
printf("%d\n",MM.solve()/2);
}
return 0;
}


Description

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:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: