首页 > 其他分享 >HDU-1054 Strategic Game(树形DP)

HDU-1054 Strategic Game(树形DP)

时间:2022-10-18 14:08:26浏览次数:75  
标签:node HDU 1054 int number dfs Strategic root dp

Strategic Game

Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 11
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

The input file contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 … node_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:

the solution is one soldier ( at the node 1).

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:
Sample Input
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)
Sample Output
1
2

经典简单直白的树形DP题目,
这是我写的第一道树形dp题目,其实树形DP问题,从这道题目就可以找到其中的规律,树形DP无非是在树上面进行动态规划。这道题目是在DFS遍历的过程,进行动态规划,状态转移方程是:
dp[root][0]+=dp[k][1];
dp[root][1]+=min(dp[root][0],dp[root][1]);
状态转移方程不难理解,结合题目的意思就可以得到。
这里dfs配合状态转移方程,前面也写到过,其实dfs就是遍历所有情况,一般的动态规划,都是遍历到所有的情况才得出来最优解。dfs就像一般DP里的几层for循环。注意,状态转移方程的位置,
还有一道题目和这个题目是差不多,建议一起做一下
​​​http://acm.hdu.edu.cn/showproblem.php?pid=1520​​​
做完之后总计一下,对树形DP就应该初步的了解

#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;
#define MAX 1500
int n;
int root;
int tot;
struct Node
{
int value;
int next;
}edge[MAX*2+5];
int head[MAX+5];
int dp[MAX+5][2];
int vis[MAX+5];
void add(int x,int y)
{
edge[tot].value=y;
edge[tot].next=head[x];
head[x]=tot++;
}
void dfs(int root)
{
dp[root][0]=0;
dp[root][1]=1;
vis[root]=1;
for(int i=head[root];i!=-1;i=edge[i].next)
{
int k=edge[i].value;
if(!vis[k])
{
dfs(k);
dp[root][0]+=dp[k][1];
dp[root][1]+=min(dp[k][0],dp[k][1]);

}
}
}
int main()
{

int a,b;
int m;
while(scanf("%d",&n)!=EOF)
{
tot=0;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
{
scanf("%d:(%d)",&a,&m);
if(i==0) root=a;
for(int j=0;j<m;j++)
{
scanf("%d",&b);
add(a,b);
add(b,a);
}
}
dfs(root);
printf("%d\n",min(dp[root][0],dp[root][1]));


}
return 0;
}



标签:node,HDU,1054,int,number,dfs,Strategic,root,dp
From: https://blog.51cto.com/u_15834522/5766252

相关文章

  • HDU-1520 Anniversary party(树形DP)
    AnniversarypartyTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):7566AcceptedSubmission(s):3321P......
  • HDU 4245
    ​​HDU-4245​​​​AFamousMusicComposer​​​​Submit​​​ ​​Status​​DescriptionMr.Bisafamousmusiccomposer.Oneofhismos......
  • HDU 1698 Just a Hook(线段树)
    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1698思路:updata()区间替换,query()区间求和先上3篇博客1:http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html 2:ht......
  • HDU 5373 The shortest problem(判断一个数能否被11整除)
    题目地址;​​点击打开链接​​思路:参考队友的代码写的,资料地址:​​点击打开链接​​ 怎样判断一个数能不能被11整除?判断一个数能不能被11整除与判断一个数能不能被......
  • HDU7239 Matryoshka Doll (DP)
    题目大意:题目描述.有......
  • HDU4417 Super Mario (主席树)
    主席树另一模板。查询的是[L,R]中<=h的个数。1#include<bits/stdc++.h>2usingnamespacestd;3#definelctr[i].ch[0]4#definerctr[i].ch[1]5#define......
  • HDU3652 B-number
    B-number题意:求1-n(<=1e9)中是13的倍数且包含“13”的个数。多组数据。数位DP#include<bits/stdc++.h>usingnamespacestd;intn;intdat[15],cnt;intf[13][11......
  • HDU-5380 Travel with candy(贪心+单调队列)
    TravelwithcandyTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:262144/262144K(Java/Others)TotalSubmission(s):396    AcceptedSubmission(s)......
  • 「HDU4035」 Maze
    \(\texttt{「HDU4035」Maze}\)\(\texttt{Describe}\)迷宫有\(n\)个房间,由\(n-1\)条隧道连通起来形成了一棵树,从结点\(1\)出发,在每个结点\(i\)都有\(3\)种可能......
  • A Magic Lamp HDU - 3183
    AMagicLampHDU-3183给定一个数字求删除N个数字后的最小数字。Input有多个测试用例。每个测试用例将包含一个给定的x整数和一个整数n(如果该整数包含m位,n将......