UVALive 2038 Strategic game

题解:

基础树形dp

dp[u][0] 表示不选u.那么子节点v都要选

dp[u][1]表示选择u,那么子节点v可选可不选

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pii pair<int,int>
#define ll long long

const int maxn = 1600;
int dp[ maxn ][ 2 ];
int n;

int head[ maxn * 2 ], cnt;

struct Edge
{
    int u, v, nxt;
}edge[ maxn * 2 ];

void addedge( int u, int v )
{
    edge[ cnt ].u = u;
    edge[ cnt ].v = v;
    edge[ cnt ].nxt = head[ u ];
    head[ u ] = cnt ++;
}

void init()
{
    cnt = 0;
    memset( head, -1, sizeof( head ) );
}

void dfs( int u, int fa )
{
    dp[ u ][ 1 ] = 1;
    dp[ u ][ 0 ] = 0;
    for( int i = head[ u ]; i != -1; i = edge[ i ].nxt )
    {
        int v = edge[ i ].v;
        if( v == fa ) continue;
        dfs( v, u );
        dp[ u ][ 1 ] += min( dp[ v ][ 1 ], dp[ v ][ 0 ] );
        dp[ u ][ 0 ] += dp[ v ][ 1 ];
    }
}

int main()
{
    while( ~scanf( "%d", &n ) )
    {
        init();
        while( n )
        {
          int u, v, num;
          scanf( "%d:(%d)", &u, &num );
          for( int i = 1; i <= num; i ++  )
          {
              scanf( "%d", &v );
              addedge( u, v );
              addedge( v, u );
          }
          n --;
        }
        dfs( 0, -1 );
        printf( "%d\n", min( dp[ 0 ][ 0 ], dp[ 0 ][ 1 ] ) );
    }
    return 0;
}
时间: 2024-11-12 14:42:44

UVALive 2038 Strategic game的相关文章

UVALive - 2038 Strategic game (无向+记忆化+简单树形DP)

题目链接:https://vjudge.net/problem/UVALive-2038 题意:给定一棵树,选择尽量少的点,使得每个没有选中的结点至少和一个已经选中的结点相邻.输出最少需要选择的节点数.思路:经典的二分图最小顶点覆盖, 也是经典的树形 DP AC代码: #include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; c

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

LA 2038 Strategic game

题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u,1)表示父节点选,这时u不可选, dp(u,0)表示父节点不选,这时u可选可不选. #include<bits/stdc++.h> using namespace std; const int maxn = 1501; int meo[maxn][2]; int vis[maxn][2], clk

训练指南DP阶段训练1

最近又忙又颓.............时间抓不紧....下学期开始就要准备考研了.......就2个月左右可以做自己喜欢的事了....争取把紫书和白书没做的,做过的..来一次完整的总结 训练指南上面的5个例题+后面15个习题是第一阶段 vjudge训练地址 http://vjudge.net/contest/139533#overview -------------------------------------------------------------------------------

关于10月15日#5的四道图论题的心得与感悟

还是我,接着补..... 第一题:UVA 10943 送分题,几乎是动规入门的例题 第二题:UVA 11584 Partitioning by Palindromes .关于在字符串中寻找回文串,一开始的直观想法是先预处理再利用类似于线段覆盖的手法处理.但是两者之间存在差别.还是只有老老实实利用动规求解. 第三题:UVALive 6270一开始想暴力模拟后找规律,结果暴力没写出来.在手推的时候,经人指点,发现了规律F[i] = F[i - 1] + F[i - 2].过后才发现是斐波拉契数列.

LA 2038

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

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

bzoj 2038 小Z的袜子(hose)(莫队算法)

2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 11542  Solved: 5166[Submit][Status][Discuss] Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命--具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两