Strategic game
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 8456 | Accepted: 3961 |
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.
For example for the tree:
the solution is one soldier ( at the node 1).
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:
the solution is one soldier ( at the node 1).
Input
The input contains several data sets in text format. Each data set represents a tree with the following description:
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
- the number of nodes
- the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.
Output
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:
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
用vector建立邻接表实现匈牙利算法
König定理:最小覆盖点数==最大匹配数
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#define maxn 210
const int INF=0x3f3f3f3f;
using namespace std;
int n;
vector<int>g[1510];
int used[1510],match[1510];
int dfs(int x)
{
int i;
for(i=0;i<g[x].size();i++)
{
int k=g[x][i];
if(!used[k])
{
used[k]=1;
if(match[k]==-1||dfs(match[k]))
{
match[k]=x;
return 1;
}
}
}
return 0;
}
int main ()
{
int i,j,a,b,k;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
memset(g,0,sizeof(g));
for(i=1;i<=n;i++)
{
scanf("%d:(%d)",&a,&k);
while(k--)
{
scanf("%d",&b);
g[a].push_back(b);
g[b].push_back(a);
}
}
memset(match,-1,sizeof(match));
for(i=0;i<n;i++)
{
memset(used,0,sizeof(used));
if(dfs(i))
ans++;
}
printf("%d\n",ans/2);
}
return 0;
}
树形dp
dp[i][1]表示第i个节点放置哨兵
dp[i][0]表示第i个节点不放置哨兵
从上往下,从左往右来遍历树,即用father[i]来纵向遍历,用brother[i]来横向遍历
状态转移方程:
dp[f][1] += min(dp[s][0],dp[s][1]); //父亲放了,儿子可以放,也可以不放
dp[f][0] += dp[s][1]; //父亲不放,儿子必须放
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
const int INF=0x3f3f3f3f;
using namespace std;
int n;
int father[1510],brother[1510],son[1510];
int dp[1510][2];
void tree(int x)
{
dp[x][0]=0;
dp[x][1]=1;
int k=son[x];
while(k!=-1)
{
tree(k);
dp[x][1]+=min(dp[k][1],dp[k][0]);
dp[x][0]+=dp[k][1];
k=brother[k];
}
}
int main ()
{
int i,j,a,b,k;
while(scanf("%d",&n)!=EOF)
{
memset(son,-1,sizeof(son));
memset(father,0,sizeof(father));
int ans,root;
for(i=1;i<=n;i++)
{
scanf("%d:(%d)",&a,&k);
while(k--)
{
scanf("%d",&b);
brother[b]=son[a];
son[a]=b;
father[b]=1;
}
}
for(i=0;i<n;i++)
{
if(!father[i])
{
root=i;
break;
}
}
tree(root);
ans=min(dp[root][1],dp[root][0]);
printf("%d\n",ans);
}
return 0;
}