欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

hdu1054

发布时间:2025/7/14 编程问答 22 豆豆
生活随笔 收集整理的这篇文章主要介绍了 hdu1054 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Strategic Game

Time Limit: 10000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1054
64-bit integer IO format: %lld      Java class name: Main

Prev

Submit Status Statistics Discuss

Next

窗体顶端

Type:

None

 

None Graph Theory     2-SAT     Articulation/Bridge/Biconnected Component     Cycles/Topological Sorting/Strongly Connected Component     Shortest Path         Bellman Ford         Dijkstra/Floyd Warshall     Euler Trail/Circuit     Heavy-Light Decomposition     Stable Marriage Problem     Trees     Directed Minimum Spanning Tree     Flow/Matching         Graph Matching         Bipartite Matching         Hopcroft–Karp Bipartite Matching         Weighted Bipartite Matching/Hungarian Algorithm         Flow         Max Flow/Min Cut         Min Cost Max Flow     Minimum Spanning Tree DFS-like     Backtracking with Pruning/Branch and Bound     Basic Recursion     IDA* Search     Parsing/Grammar     DFS/BFS     Advanced Search Techniques         Binary Search/Bisection         Ternary Search Geometry     Basic Geometry     Computational Geometry     Convex Hull     Pick's Theorem Game Theory     Green Hackenbush/Colon Principle/Fusion Principle     Nim     Sprague-Grundy Number Matrix     Gaussian Elimination     Matrix Exponentiation Data Structures     Basic Data Structures     Binary Indexed Tree     Binary Search Tree     Hashing     Orthogonal Range Search     Range Minimum Query/Lowest Common Ancestor     Segment Tree/Interval Tree     Trie Tree     Sorting     Disjoint Set     Dancing link String     Aho Corasick     Knuth-Morris-Pratt(KMP)     Suffix Array/Suffix Tree Math     Basic Math     Big Integer Arithmetic     Number Theory         Chinese Remainder Theorem         Extended Euclid         Inclusion/Exclusion         Modular Arithmetic     Combinatorics         Group Theory/Burnside's         Counting         Probability/Expected Value Others     Tricky     Hardest     Unusual     Brute Force     Implementation     Constructive Algorithms     Two Pointer     Bitmask     Beginner     Discrete Logarithm/Shank's Baby-step Giant-step Algorithm     Greedy     Divide and Conquer Dynamic Programming Tag it!

窗体底端

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

 

题意:

       给出一棵n个结点的树,现在需要选择最少的点,使得所有的边都具有至少一个被选择的点,输出这个最少的点数。

分析:

       由于题目中的结点呈现树结构,所以可以考虑记忆化搜索。在建图的时候考虑存储每个结点子结点的数量。搜索点的顺序在树上是由上至下,dp[pos][0],表示当前位于pos结点并且不选择pos结点时这个pos结点对应的子树最少需要选择多少个点才能在子树上满足题目的条件;dp[pos][1]表示的则是选择pos这个结点。如果pos这个结点没有选择,则它的子结点必须全部都被选择;如果pos没有被选择,则它的子结点可选择可不选择。即dp[i][0]=sum(dp[son[i][j]][1]) dp[i][1]=sum(min(dp[son[i][j]][0],dp[son[i][j]][1]))

1 #include <cstdio> 2 #include <cstring> 3 int min(int a,int b){return a < b ? a : b;} 4 const int MAX_N = 1500; 5 int n; 6 int son[MAX_N + 1][MAX_N + 1],dp[MAX_N + 1][2],son_num[MAX_N + 1],f[MAX_N + 1]; 7 // 位于pos,放或者不放soldier的行为带来的数量答案 8 int dfs(int pos,int key){ 9 if(dp[pos][key] != -1) return dp[pos][key]; 10 int ans = key; 11 for(int i = 0 ; i < son_num[pos] ; i++) 12 if(key == 0) 13 ans += dfs(son[pos][i],1); // 子结点必须全部选择放1个soldier 14 else 15 ans += min(dfs(son[pos][i],0),dfs(son[pos][i],1)); 16 return dp[pos][key] = ans; 17 } 18 int main(){ 19 while(scanf("%d",&n) == 1){ 20 memset(dp,-1,sizeof dp); 21 for(int i = 0 ; i < n ; i++) f[i] = i; // 用于寻找根结点 22 for(int i = 0 ; i < n ; i++){ 23 int x,m; scanf("%d:(%d)",&x,&m); 24 son_num[x] = m; 25 for(int j = 0 ; j < m ; j++){ 26 scanf("%d",&son[x][j]); 27 f[son[x][j]] = x; 28 } 29 } 30 int ans = 0; 31 for(int i = 0 ; i < n ; i++)if(f[i] == i) 32 ans = min(dfs(i,1),dfs(i,0)); 33 printf("%d\n",ans); 34 } 35 return 0; 36 } View Code

 

转载于:https://www.cnblogs.com/cyb123456/p/5917714.html

总结

以上是生活随笔为你收集整理的hdu1054的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。