POJ1463 Strategic game

题解:

树形dp,最小独立集裸题

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 2097152
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=1510;
const int mod=1e9+7;

int dp[maxn][2],not_root[maxn];
vector<int> Edge[maxn];
int n,root,num;

int DP(int u,int j){
    int& ans=dp[u][j];
    if(ans>=0) return ans;
    ans=0;
    if(j) ans+=1;
    for(int i=0;i<Edge[u].size();i++){
        if(j) ans+=min(DP(Edge[u][i],0),DP(Edge[u][i],1));
        else  ans+=DP(Edge[u][i],1);
    }
    return ans;
}

int main(){
    while(~scanf("%d",&n)){
        memset(not_root,0,sizeof(not_root));
        memset(dp,-1,sizeof(dp));
        for(int i=0;i<n;i++) Edge[i].clear();
        for(int i=1;i<=n;i++){
            int u,num,v;
            scanf("%d:(%d)",&u,&num);
            for(int i=1;i<=num;i++){
                scanf("%d",&v);
                Edge[u].pb(v);
                not_root[v]=1;
            }
            for(int i=0;i<n;i++){
                if(!not_root[i]){
                    root=i;
                    break;
                }
            }
        }
        num=DP(root,0);
        memset(dp,-1,sizeof(dp));
        num=min(num,DP(root,1));
        printf("%d\n",num);
    }
    return 0;
}
时间: 2024-08-05 19:57:33

POJ1463 Strategic game的相关文章

poj1463 Strategic game 树的最小点覆盖

http://poj.org/problem?id=1463 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6413   Accepted: 2973 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast

POJ1463 Strategic game (最小点覆盖 or 树dp)

题目链接:http://poj.org/problem?id=1463 给你一棵树形图,问最少多少个点覆盖所有的边. 可以用树形dp做,任选一点,自底向上回溯更新. dp[i][0] 表示不选i点 覆盖子树所有边的最少点个数,那选i点的话,那么i的邻接节点都是必选的,所以dp[i][0] += dp[i.son][1] dp[i][1] 表示选i点 覆盖子树所有边的最少点个数,那么i的邻接点可选可不选(而不是一定不选,看注释样例就知道了),所以dp[i][0] += min(dp[i.son][

Strategic Game(匈牙利算法)

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

HDU 1054 Strategic Game(树形DP)

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 for

hdoj 1054 Strategic Game【匈牙利算法+最小顶点覆盖】

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

Strategic game(POJ 1463 树形DP)

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

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

UVA 1292 十二 Strategic game

Strategic game Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1292 Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is

hdu 1054 Strategic Game (二分匹配)

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