Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4061    Accepted Submission(s): 1791

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
 
Source
 
这一道题和 hdu2412是一个类型的。而且还容易了一些。
 
 
 #include<stdio.h>
#include<string.h>
#include<stdlib.h> struct node
{
int next[];
int num;
}f[];
int dp[][]; int Min(int x,int y)
{
return x>y? y:x;
} void dfs(int k)
{
int i,j,cur;
if( f[k].num== )
{
dp[k][]=;
dp[k][]=;
return;
}
for(i=;i<=f[k].num;i++)
{
cur=f[k].next[i];
dfs(cur);
j=Min(dp[cur][],dp[cur][]);
dp[k][]+=j; dp[k][]+=dp[cur][];
}
dp[k][]++;
} int main()
{
int n,root,r,num,x,len;
int i,j;
while(scanf("%d",&n)>)
{
getchar();
for(i=;i<=;i++) f[i].num=;
for(i=;i<=n;i++)
{
scanf("%d:(%d)",&r,&num);
if(i==) root=r;
f[r].num=num;
len=;
for(j=;j<=num;j++)
{
scanf("%d",&x);
f[r].next[++len]=x;
}
}
memset(dp,,sizeof(dp));
dfs(root);
printf("%d\n",Min(dp[root][],dp[root][]));
}
return ;
}

最新文章

  1. 文件描述符、文件表项指针、inode节点的关系
  2. Windows平台下Qt中glut库的使用
  3. yii2 Pjax的使用
  4. 浏览器User-agent String里的历史故事
  5. Confluence Wiki Markup &amp; Markdown
  6. SimpleDateFormat日期格式化
  7. Div+css中ul ol li dl dt dd使用
  8. 在线CRC校验
  9. 史上最全然oophper php文件上传之文件类型相应表,ie,火狐各一份。
  10. 第一章C语言简介及输出函数 上机部分
  11. kickstart 实现批量安装centos7.x系统
  12. selenium--unittest 框架/selenium--常见异常
  13. 一个故事带你理解if __name__ == &#39;__main__&#39;
  14. 提问:MicrosoftUnderlying input stream returned zero bytes
  15. python datetime模块详解
  16. 关于iwinfo的调试
  17. mysql-8.0.15-winx64 解压版安装 图文详解
  18. Python 练习: 简单的用户登录判断
  19. jquery ajax 方法实例
  20. win7使用问题解决

热门文章

  1. Python unittest第二篇:测试夹具
  2. Groovy 反序列化漏洞分析(CVE-2015-3253)
  3. html5: 复制到剪贴板 clipboard.js
  4. 论文分享NO.2(by_xiaojian)
  5. 转 在子线程中new Handler报错--Can&#39;t create handler inside thread that has not called Looper.prepare()
  6. macOS High Sierra 10.13正式版USB安装盘制作
  7. LINQ to Entities does not recognize the method , and this method cannot be translated into a store expression 解决办法
  8. 【转载】伪静态SQL注入
  9. Android序列化:Parcelable/Serializable
  10. 控制反转(IOC) 和依赖注入(DI) 的理解