POJ 1463 Strategic game (二分图最小覆盖点(匈牙利算法) 或 树形DP)
2017-08-16 17:39
519 查看
Strategic game
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:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/15/43f52b994a402a64db401be44050adc4.jpg)
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 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
Sample Output
Source
Southeastern Europe 2000
用vector建立邻接表实现匈牙利算法
König定理:最小覆盖点数==最大匹配数
#include <stdio
a038
.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;
}
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 8456 | Accepted: 3961 |
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:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201707/15/43f52b994a402a64db401be44050adc4.jpg)
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 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
Southeastern Europe 2000
用vector建立邻接表实现匈牙利算法
König定理:最小覆盖点数==最大匹配数
#include <stdio
a038
.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;
}
相关文章推荐
- poj 1463 Strategic game(树形dp,最小边覆盖集)
- POJ 1463 Strategic game 最小点覆盖,树形DP
- poj 1463 Strategic game(最小点覆盖树形DP)
- POJ 1463 Strategic game 最小点覆盖集(树形dp)
- POJ 1463 Strategic game (树形dp 树的最小点覆盖 水)
- POJ - 1463 Strategic game 最小边覆盖(二分图匹配-匈牙利算法)
- E - Strategic Game (HDU - 1054 )(最小顶点覆盖)(匈牙利算法模板)(树形DP)
- POJ 1463 Strategic game (树形DP,放置点覆盖边)
- POJ - 1463 Strategic game(树形DP)
- POJ 1463 Strategic game(树形DP)
- poj 1463 Strategic game(树形dp)
- 【贪心】【树形DP】[POJ1463][HDU1054]Strategic game 战略游戏
- POJ 1463 Strategic game( 树形DP )
- 【POJ 1463】【hdu 1054】Strategic game(树形dp)
- POJ 1463 ||HDU 1054 Strategic game(二分图最小顶点覆盖)
- poj1463 Strategic game(树形dp)
- poj 1463 树形dp 最小点覆盖
- poj 3020 Antenna Placement (最小路径覆盖, 匈牙利算法, 拆点形成二分图)
- POJ 1463 && HDU 1054 Strategic Game(树形DP)
- POJ1463:Strategic game(树形DP)