Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7029 Accepted Submission(s): 3322
Problem 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
Source
Recommend
JGShining | We have carefully selected several similar problems for you: 1053 1151 1281 1142 1507
树型dp:
因为搞定最小顶点问题是可以在树上解决的,所以我们也可以用动态规划的思想在树上做文章,也就是所谓的树型dp。
我们设dp【i】【0】表示点i属于点覆盖,并且以点i为根的子树中所连接的边都被覆盖了的情况下点覆盖集中所包含的最少的点的个数。
我们设dp【i】【1】表示点i不属于点覆盖,并且以点i为根的子树中所连接的边都背覆盖了的情况下覆盖集中所包含的最少的点的个数。
不难理解:
u是点i直接相连的子节点。
dp【i】【0】=1+所有u的和【min(dp【u】【0】,dp【u】【1】)】:因为点i属于点覆盖集,所以它的子节点无论是否属于覆盖集,边(i,u)都已经被覆盖了,所以这个时候我们只需要从其子节点的最优状态转移过来即可。
dp【i】【1】=所有u的和【dp【u】【0】】:因为点i不属于点覆盖集,那么想要覆盖所有边(i,u),那么必须保证其子节点u必须属于覆盖集,才能保证边(i,u)被覆盖,所以这个时候我们只能从其所有子节点属于点覆盖的状态转移过来。
AC代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int head[100000];
struct EdgeNode
{
int to;
int w;
int next;
}e[100000];
int dp[10000][2];
int vis[10000];
int n;
int cont;
void add(int from,int to)
{
e[cont].to=to;
e[cont].next=head[from];
head[from]=cont++;
e[cont].to=from;
e[cont].next=head[to];
head[to]=cont++;
}
void dfs(int root)
{
vis[root]=1;
dp[root][0]=1;
dp[root][1]=0;
for(int k=head[root];k!=-1;k=e[k].next)
{
int to=e[k].to;
if(vis[to]==1)continue;
dfs(to);
dp[root][0]+=min(dp[to][0],dp[to][1]);
dp[root][1]+=dp[to][0];
}
}
int main()
{
while(~scanf("%d",&n))
{
cont=0;
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
for(int i=0;i<n;i++)
{
int u,k;
scanf("%d:(%d)",&u,&k);
for(int j=0;j<k;j++)
{
int v;
scanf("%d",&v);
add(u,v);
}
}
dfs(0);
printf("%d\n",min(dp[0][0],dp[0][1]));
}
}
因为有:最小点覆盖==最大二分匹配数/2(双向图)
那么我们也可以用二分匹配来解决这个问题:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int >mp[1505];
int vis[1505];
int match[1505];
int find(int x)
{
for(int i=0;i<mp[x].size();i++)
{
int v=mp[x][i];
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
mp[i].clear();
}
for(int i=0;i<n;i++)
{
int u,k;
scanf("%d:(%d)",&u,&k);
for(int j=0;j<k;j++)
{
int v;
scanf("%d",&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
}
int sum=0;
memset(match,-1,sizeof(match));
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i))sum++;
}
printf("%d\n",sum/2);
}
}