UVa 12166 - Equilibrium Mobile <二叉树+DFS>
2015-01-28 00:14
225 查看
A mobile is a type of kinetic sculpture constructed to take advantage of the principle of equilibrium. It consists of a number of rods, from which weighted objects or further rods hang. The objects hanging from the rods balance each other, so that the rodsremain more or less horizontal. Each rod hangs from only one string, which gives it freedom to rotate about the string.We consider mobiles where each rod is attached to its string exactly in the middle, as in the figure underneath. You are given such a configuration, but the weights on the ends are chosen incorrectly, so that the mobile is not in equilibrium. Since that's notaesthetically pleasing, you decide to change some of the weights.What is the minimum number of weights that you must change in order to bring the mobile to equilibrium? You may substitute any weight by any (possibly non-integer) weight. For the mobile shown in the figure, equilibrium can be reached by changing the middleweight from 7 to 3, so only 1 weight needs to changed.
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:One line with the structure of the mobile, which is a recursively defined expression of the form:<expr> ::= <weight> | "[" <expr> "," <expr> "]"with <weight> a positive integer smaller than 109 indicating a weight and [<expr>,<expr>] indicating a rod with the two expressions at the ends of the rod. The total number of rods in the chain from a weight to the top of the mobilewill be at most 16.
Output
Per testcase:One line with the minimum number of weights that have to be changed.Sample Input
3 [[3,7],6] 40 [[2,3],[4,5]]
Sample Output
1 0 3
#include <bits/stdc++.h>using namespace std;int cur = 0;char str[1024000];map<long long, int> cnt;void DFS(int dep){if(isdigit(str[cur])){long long a = 0;while(isdigit(str[cur]))a = a * 10 + str[cur++] - '0';cnt[a<<dep]++;}else{cur++; DFS(dep + 1);cur++; DFS(dep + 1);cur++;}}int main(){ios::sync_with_stdio(false);int T; cin >> T; cin.get();while(cur = 0, cnt.clear(), T--) {cin >> str; DFS(0);int mx = INT_MIN, sum = 0;for(map<long long, int>::iterator it = cnt.begin(); it != cnt.end(); it++)sum += it->second, mx = max(mx, it->second);cout << sum - mx << endl;}return 0;}[/code]
相关文章推荐
- UVa 12166 - Equilibrium Mobile <二叉树+DFS>
- 4_3 救济金发放(UVa133)<子过程/函数设计>
- uva 10391 Compound Words <set>
- UVA 10361 - Automatic Poetry(<>里字符串交换)
- 6_12 油田(UVa572)<图的连通块DFS>
- UVa290 - Palindroms <---> smordnilaP(进制转换、回文)
- UVA 10534 - Wavio Sequence(自写set<node>优化o(n^2)dp)
- 5_5 集合栈计算机(UVa12096)<stack与STL其他容器的综合运用>
- UVa 12333 - Revenge of Fibonacci <大数 字典树>
- UVA_Rotation Game<旋转游戏> UVA 1343
- UVA 11995 I Can Guess the Data Structure!<STL数据结构使用>
- UVALive 7146 muliset<> 容器用法 防御塔
- 6_15 给任务排序(UVa10305)<拓扑排序>
- UVa 12333 - Revenge of Fibonacci <大数 字典树>
- 【UVALive】3029.City Game<C++>
- Uva572种子填充<dfs>
- 5_7 丑数(UVa136)<priority_queue的使用>
- 6_14 Abbott的复仇(UVa816)<图的最短路BFS>
- 4_2 刽子手游戏(UVa489)<自顶向下逐步求精法>
- UVA - 10129 - Play on Words <欧拉道路+并查集>