hdu 1054 Strategic Game(tree dp)
Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3806 Accepted Submission(s): 1672
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:
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
Source
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 2010 int Min(int i,int j){return i<j?i:j;} struct node{ node *l,*r;//l-son,r-brother int f[2]; void init(){ l=r=NULL;f[0]=f[1]=0; } }t[N],*rt; int r[N]; void dfs(node *x){ //遍历x儿子 node *l=x->l; while(l){ dfs(l); //放在x上 x->f[1]+=Min(l->f[0],l->f[1]); //x不放 x->f[0]+=l->f[1]; l=l->r; } } int main(){ int i,j,k,x,y,n,m; while(~scanf("%d",&n)){ m=0; memset(r,-1,sizeof(r)); while(n--){ scanf("%d:(%d)",&i,&k); if(r[i]==-1){r[i]=m;t[m++].init();} x=r[i]; while(k--){//i->l=j scanf("%d",&j); if(r[j]==-1){r[j]=m;t[m++].init();} y=r[j]; t[y].f[1]=1; t[y].r=t[x].l; t[x].l=&t[y]; } } for(i=0;;i++) if(t[r[i]].f[1]==0){ rt=&t[r[i]]; rt->f[1]=1; break; } dfs(rt); printf("%d\n",Min(rt->f[0],rt->f[1])); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)