uva 12186 - Another Crisis-树上动态规划

来源:互联网 发布:大数据时代的教育 编辑:程序博客网 时间:2024/05/22 12:35
//  uva 12186 - Another Crisis-树上动态规划/* 典型的树形动态规划问题: 此题题目意思很简单,也很好想到解法,但是就是写程序需要注意。 设d[i]表示以节点i为向上级提交申请需要工人签字的人数。 那么对于节点i,d[i] 是节点i子节点  d[j]集合的最小的前c个值,c为满足直属下属签字率至少为T时签字的人数。 在计算时我们采用了递归的方式。还有就是这提议为啥 不用记忆化搜索呢,因为一个节点最多访问一次,所以没有必要记忆化。 */#include <iostream>#include <queue>#include <stack>#include <stdio.h>#include <stdlib.h>#include <math.h>#include<vector>#include <string.h>#include <algorithm>#include <set>#include <map>#include <cstdio>#define ll long longusing namespace std ;int d[500000 + 10] ;vector<int> son[500000+10]  ;int T  , n  ;int dp(int u ){    if(son[u].size() == 0 )  return 1 ;    int len = (int)son[u].size() ;    vector<int> temp ;temp.clear() ;    for (int i = 0 ; i < len ; i++) {        temp.push_back(dp(son[u][i])) ;    }    sort(temp.begin() , temp.end()) ;    int c = (len * T - 1)/100 + 1 ;    int ans = 0 ;    for (int i = 0; i < c ; i++) {        ans += temp[i] ;    }    return ans ;}int main(int argc, const char * argv[]) {    int d  ;    while(scanf("%d%d" ,&n,&T)==2 &&n+T){        for(int i = 0 ; i < n + 1 ; i++) son[i].clear() ;        for (int i = 0; i < n; i++) {            scanf("%d" ,&d) ;            son[d].push_back(i+1) ;        }        printf("%d\n" , dp(0)) ;    }    return 0;}

0 0
原创粉丝点击