UVa 12186 Another Crisis dp:树上dp
来源:互联网 发布:先学c还是先学java 编辑:程序博客网 时间:2024/06/07 06:25
UVA - 12186
Thank you!
Another Crisis
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
The question is from here.
My Solution
d(i)表示给上级发信至少需要多少工人。把所有子结点的d值排序然后取前c个,
对除非的处理 ★★ c = (k*T - 1) /100 +1;
用前向星储存树 每组数据搞完要 for 1 -> n 对sons[ i ].clear();
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>using namespace std;const int maxn = 100008;int n,t,T;vector<int> sons[maxn]; //sons[i] 为结点i的子列表int dp(int u){ if(sons[u].empty()) return 1; int k = sons[u].size(); vector<int> d; for(int i = 0; i < k; i++) d.push_back(dp(sons[u][i])); sort(d.begin(),d.end()); int c = (k*T-1) /100 +1; int ans = 0; for(int i = 0; i < c; i++) ans += d[i]; return ans;}int main(){ while(scanf("%d%d", &n, &T)){ if(n == 0 && T == 0) break; for(int i = 1; i <= n; i++){ scanf("%d", &t); sons[t].push_back(i); } printf("%d\n", dp(0)); for(int i = 0; i <= n; i++){ sons[i].clear(); } } return 0;}
也可以写成这样
<span style="font-size:14px;">#include <iostream>#include <cstdio>#include <vector>#include <algorithm>//#define LOCALusing namespace std;const int maxn = 100008;int n,t,T;vector<int> sons[maxn]; //sons[i] 为结点i的子列表void build(){ for(int i = 1; i <= n; i++){ scanf("%d", &t); sons[t].push_back(i); }}int dp(int u){ if(sons[u].empty()) return 1; int k = sons[u].size(); vector<int> d; for(int i = 0; i < k; i++) d.push_back(dp(sons[u][i])); sort(d.begin(),d.end()); int c = (k*T-1) /100 +1; int ans = 0; for(int i = 0; i < c; i++) ans += d[i]; return ans;}void initial(){ for(int i = 0; i <= n; i++){ //清空的时候要把大boss的也清理掉 sons[i].clear(); }}int main(){ #ifdef LOCAL freopen("a.txt","r",stdin); #endif // LOCAL while(scanf("%d%d", &n, &T)){ if(n == 0 && T == 0) break; build(); printf("%d\n", dp(0)); initial(); } return 0;}</span>
Thank you!
0 0
- UVa 12186 Another Crisis dp:树上dp
- UVA 12186 - Another Crisis(树形DP)
- Another Crisis - UVa 12186 树形dp
- UVA 12186 Another Crisis(树形DP)
- UVa 12186 Another Crisis(树形DP)
- UVA 12186 Another Crisis 树形DP
- UVa 12186 - Another Crisis(树形DP)
- uva 12186 Another Crisis 树形dp
- UVa 12186:Another Crisis(DP)
- UVa 12186 - Another Crisis(树形DP)
- UVa-12186 Another Crisis (dp)
- UVA - 12186 Another Crisis 水树形dp
- UVA 12186 Another Crisis [树形dp]
- uva 12186 Another Crisis 树形DP
- UVA - 12186 Another Crisis (树形dp)
- UVa - 12186 - Another Crisis ( 树形DP )
- UVA - 12186 Another Crisis (树形DP)
- UVA 12186 Another Crisis 树型DP
- adt怎么关联第三方jar包源码的方法
- 拥抱高效、拥抱 Bugtags 之来自用户的声音(五)
- HDU3037(Lucas定理)
- Git 合并 patch 时的冲突处理一例
- PHP数据库连接池SQL Relay安装使用
- UVa 12186 Another Crisis dp:树上dp
- Wireshark 文件分割和合并
- 后台为php的apns证书
- Android深入浅出系列之广播机制—2-Android中的广播机制
- Unity3D 优化之路(一):DrawCall
- Android ADB命令
- Android 异步加载图片
- Mac下如何重置MySQL Root密码
- 设计模式—代理模式