Strategic game UVA - 1292

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<vector>
 6 #define mem(array) memset(array,0,sizeof(array))
 7 using namespace std;
 8
 9 int n,dp[1600][2];
10 bool vis[1600];
11 vector<int> G[1600];
12
13 void Read(){
14     for(int i=0;i<n;i++) G[i].clear();
15     for(int i=0;i<n;i++){
16         int a,b,c;
17         scanf("%d:(%d)",&a,&b);
18         for(int j=0;j<b;j++){
19             scanf("%d",&c);
20             G[a].push_back(c);
21             G[c].push_back(a);
22         }
23     }
24 }
25
26 void DFS(int root){
27     dp[root][1]=1;
28     dp[root][0]=0;
29     vis[root]=true;
30
31     for(int i=0;i<G[root].size();i++){
32         int sons=G[root][i];
33         if(vis[sons]) continue;
34
35         DFS(sons);
36         dp[root][0]+=dp[sons][1];
37         dp[root][1]+=min(dp[sons][1],dp[sons][0]);
38     }
39 }
40
41 int main()
42 {   while(~scanf("%d",&n)){
43         Read();
44         mem(vis);mem(dp);
45         DFS(0);
46         cout<<min(dp[0][1],dp[0][0])<<endl;
47     }
48     return 0;
49 } 
时间: 2024-10-26 03:29:19

Strategic game UVA - 1292的相关文章

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

uva 1292

题目大意 给定一棵树,选择尽量少的节点,使得每个没有选中的结点至少和一个已选结点相邻. 思路 经典的树形dp题,据说是最小顶点覆盖. f[u][0]: 表示不选i点,覆盖这个子树的最少点f[u][1]:选i点,覆盖这个子树的最少点 对于u点,如果选择这个点,那么他的字节点可选也可不选如果不选u点的话,那么它的子结点就必须要选!开始时我以为字节点只要至少选一个就可以了,但是这样是错的! 因为会出现下面这种情况: 顶点1不选,子节点中2有选了,但是3却没有相邻结点有选. 所以可以得到状态转移方程:

uva 1292(树形dp)

题意:有一个树,上面有n个结点,给出每个结点有边相连的直接相邻的点,问最少选几个点能让所有的边至少有一个结点被选中. 题解:树形dp简单题,把0当做根节点. f[i][0]:不选i点,覆盖所有边的最少点 f[i][1]:选i点,覆盖所有边的最少点 状态转移方程: f[u][1] += min(f[v][1], f[v][0]);//v是u的子节点,选u点子节点可以选或不选 f[u][0] += f[v][1];//不选u点就一定要选子节点,这样保证边能覆盖 #include <stdio.h>

[SinGuLaRiTy] 贪心题目复习

[SinGuLaRiTy-1024] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 2709] 颜料 (Painter) 题目描述 杂货店出售一种由N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),组成的颜料套装.你现在需要使用这N种颜料:不但如此,你还需要一定数量的灰色颜料.杂货店从来不出售灰色颜料——也就是它不属于这N种之一.幸运的是,灰色颜料是比较好配置的,如果你取出三种不同颜色的颜料各x ml,混合起来就

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 497 - Strategic Defense Initiative

题目:最大上升子序列,输出一组解. 分析:dp,LIS.数据较小 O(n^2)算法即可. 设以第i个数字作为最大上升子序列中的最后一个数的长度为 f(i),则有转移方程: f(i)= max(f(j)) { 0=< j < i  && data[j] < data[i] }: 用一个数组记录前驱,递归输出即可. 说明:注意输出格式有点纠结. #include <iostream> #include <cstdlib> #include <cs

UVA之1330 - City Game

[题目] Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees, factories and buildings. There is still some space in the area tha

UVA - 11609 Teams (排列组合数公式)

In a galaxy far far awaythere is an ancient game played among the planets. The specialty of the game isthat there is no limitation on the number of players in each team, as long asthere is a captain in the team. (The game is totally strategic, so som

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d