Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7787 Accepted Submission(s): 3715
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:
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
题意:给出一棵树,要求找到最少放几个士兵才能将所有点都看守到,每个节点的士兵只能看守临近的节点
解题思路:树形dp或者二分图
最小覆盖集,且树天然就是二分的(树上奇数层的顶点与偶数层的顶点构成了二分图的左右点集),所以只需要求该树的最小覆盖集点数
可以用染色法把树的所有节点二分,构建二分图,然后在求该二分图的最大匹配数,也可以直接构建翻倍的二分图,求出最大匹配数ans.最终结果等于ans/2
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
int n;
int s[4000],nt[4000],e[4000];
int dp[2000][2];
void dfs(int x,int pre)
{
dp[x][1]++;
for(int i=s[x];i!=-1;i=nt[i])
{
int ee=e[i];
if(ee==pre) continue;
dfs(ee,x);
dp[x][1]+=min(dp[ee][0],dp[ee][1]);
dp[x][0]+=dp[ee][1];
}
}
int main()
{
while(~scanf("%d",&n))
{
memset(dp,0,sizeof dp);
memset(s,-1,sizeof s);
memset(nt,-1,sizeof nt);
int u,v,k,sum=0;
for(int i=0;i<n;i++)
{
scanf("%d:(%d)",&u,&k);
for(int j=0;j<k;j++)
{
scanf("%d",&v);
nt[++sum]=s[u],s[u]=sum,e[sum]=v;
nt[++sum]=s[v],s[v]=sum,e[sum]=u;
}
}
dfs(0,-1);
printf("%d\n",min(dp[0][0],dp[0][1]));
}
return 0;
}
二分图
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <functional>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int n;
int x[1505],y[1505];
int s[1505],nt[30050],e[30050];
int visit[1505];
bool path(int k)
{
for(int i=s[k];~i;i=nt[i])
{
int ee=e[i];
if(!visit[ee])
{
visit[ee]=1;
if(y[ee]==-1||path(y[ee]))
{
y[ee]=k;
x[k]=ee;
return 1;
}
}
}
return 0;
}
void MaxMatch()
{
int ans=0;
memset(x,-1,sizeof x);
memset(y,-1,sizeof y);
for(int i=0;i<n;i++)
{
if(x[i]==-1)
{
memset(visit,0,sizeof visit);
if(path(i)) ans++;
}
}
printf("%d\n",ans/2);
}
int main()
{
while(~scanf("%d",&n))
{
int cnt=1,ee,ss,k;
memset(s,-1,sizeof s);
memset(nt,-1,sizeof nt);
for(int i=1;i<=n;i++)
{
scanf("%d: (%d)",&ss,&k);
for(int j=1;j<=k;j++)
{
scanf("%d",&ee);
nt[cnt]=s[ss],s[ss]=cnt,e[cnt++]=ee;
nt[cnt]=s[ee],s[ee]=cnt,e[cnt++]=ss;
}
}
MaxMatch();
}
return 0;
}