HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

分析:这里使用树形DP做。

1、最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2。

2、树形DP:

dp[i][0]表示i为根节点,并且该节点不放,所需的最少的点数。

dp[i][1]表示i为根节点,并且该节点放,所需要的最少的点数。

dp[i][0]=sum(dp[son[i][j]][1]) 该点不放,则它的儿子节点必须都放,只有这样之间的边才可以被覆盖。

dp[i][1]=sum(min(dp[son[i][j]][0],dp[son[i][j]][1])) 该点放的话,则它的儿子节点有两种决策,放或不放,取min即可。

#include<iostream>
#include<vector>
#include<limits.h>
using namespace std;

#define N 1505
int dp[N][2];    //dp[i]表示以i为根节点时所需要的最小点数
int f[N];        //用来记录父节点
vector<int> son[N]; //记录儿子节点

int min(int x,int y)
{
	return x<y?x:y;
}

int dfs(int pos,int v)
{
	int sum,i;

	if(dp[pos][v]!=INT_MIN)
		return dp[pos][v];
	sum=v;
	for(i=0;i<son[pos].size();i++)
		if(v==1)                    //当前节点选
			sum+=min(dfs(son[pos][i],0),dfs(son[pos][i],1));
		else
			sum+=dfs(son[pos][i],1);//当前节点不选,子节点必选
	dp[pos][v]=sum;
	return sum;
}

int main()
{
	int ans,n,i,x,m,j,t;

	while(scanf("%d",&n)==1)
	{
		for(i=0;i<n;i++)
		{
			son[i].clear();
			f[i]=i;
			dp[i][0]=dp[i][1]=INT_MIN;
		}
		for(i=0;i<n;i++)
		{
			scanf("%d:(%d)",&x,&m);
			for(j=0;j<m;j++)
			{
				scanf("%d",&t);
				son[x].push_back(t);
				f[t]=x;
			}
		}
		for(i=0;i<n;i++)
			if(f[i]==i)    //找到根节点
			{
				ans=min(dfs(i,0),dfs(i,1));
				break;
			}
		printf("%d\n",ans);
	}
    return 0;
}
时间: 2024-12-25 20:23:13

HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP的相关文章

hdu 1054 Strategic Game (最小顶点覆盖+稀疏图)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4889    Accepted Submission(s): 2225 Problem Description Bob enjoys playing computer games, especially strategic games, but some

UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)

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 wh

hdu 1054 Strategic Game 二分图最小点覆盖

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题意: 给出一个无向图,求最小点覆盖. 思路: 用网络流来做设立一个超级源点和一个超级汇点. 每个点拆成i和i'. 从超级源点向点i连一条边,容量为1. 从i’向超级汇点连一条边,容量为1. 从i向i'连一条边,容量为正无穷. 然后求最小割/2.因为拆点拆成了2个. 也可以用二分图匹配来做,也是求出最大匹配然后/2. 1 #include <bits/stdc++.h> 2 using na

hdu 1150 Machine Schedule(二分图-最小顶点覆盖)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5424    Accepted Submission(s): 2691 Problem Description As we all know, machine scheduling is a very classical problem in compu

poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)

Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the

[ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2963    Accepted Submission(s): 1492 Problem Description On a grid map there are n little men and n houses. In each unit time, every

HDU——T 1054 Strategic Game

http://acm.hdu.edu.cn/showproblem.php?pid=1054 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8510    Accepted Submission(s): 4096 Problem Description Bob enjoys playing computer games, espec

POJ1325 Machine Schedule 【二分图最小顶点覆盖】

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11958   Accepted: 5094 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int