• 树形DP UVA 1292 Strategic game


    题目传送门

     1 /*
     2     题解:选择一个点,它相邻的点都当做被选择,问最少选择多少点将所有点都被选择
     3     树形DP:dp[i][0/1]表示当前点选或不选,如果选,相邻的点可选可不选,取最小值
     4 */
     5 /************************************************
     6  * Author        :Running_Time
     7  * Created Time  :2015-8-12 10:54:05
     8  * File Name     :UVA_1292.cpp
     9  ************************************************/
    10 
    11 #include <cstdio>
    12 #include <algorithm>
    13 #include <iostream>
    14 #include <sstream>
    15 #include <cstring>
    16 #include <cmath>
    17 #include <string>
    18 #include <vector>
    19 #include <queue>
    20 #include <deque>
    21 #include <stack>
    22 #include <list>
    23 #include <map>
    24 #include <set>
    25 #include <bitset>
    26 #include <cstdlib>
    27 #include <ctime>
    28 using namespace std;
    29 
    30 #define lson l, mid, rt << 1
    31 #define rson mid + 1, r, rt << 1 | 1
    32 typedef long long ll;
    33 const int MAXN = 1500 + 10;
    34 const int INF = 0x3f3f3f3f;
    35 const int MOD = 1e9 + 7;
    36 vector<int> G[MAXN];
    37 bool vis[MAXN];
    38 int dp[MAXN][2];
    39 int n;
    40 
    41 void DFS(int u) {
    42     vis[u] = true;
    43     for (int i=0; i<G[u].size (); ++i)  {
    44         int v = G[u][i];
    45         if (vis[v]) continue;
    46         DFS (v);
    47         dp[u][1] += min (dp[v][0], dp[v][1]);
    48         dp[u][0] += dp[v][1];
    49     }
    50 }
    51 
    52 int main(void)    {     //UVA 1292 Strategic game
    53     while (scanf ("%d", &n) == 1)   {
    54         for (int i=0; i<n; ++i) G[i].clear ();
    55         memset (dp, 0, sizeof (dp));
    56         for (int i=0; i<n; ++i) dp[i][1] = 1;
    57         memset (vis, false, sizeof (vis));
    58 
    59         for (int i=1; i<=n; ++i)    {
    60             int u, v, num;   scanf ("%d:(%d)", &u, &num);
    61             for (int j=1; j<=num; ++j)  {
    62                 scanf ("%d", &v);
    63                 G[u].push_back (v); G[v].push_back (u);
    64             }
    65         }
    66 
    67         DFS (1);
    68         printf ("%d
    ", min (dp[1][0], dp[1][1]));
    69     }
    70 
    71     return 0;
    72 }
    编译人生,运行世界!
  • 相关阅读:
    poj3083(Children of the Candy Corn)
    poj3278(Catch That Cow)
    poj2996(Help Me with the Game)
    poj2993(Emag eht htiw Em Pleh)
    js 对多sheet Excel赋值操作
    学习进度总结(三)
    学习进度总结(二)
    学习进度总结(一)
    《人月神话》阅读笔记(1)
    Android studio的安装与使用
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4724305.html
Copyright © 2020-2023  润新知