Gym

来源:互联网 发布:linux c编程工具 编辑:程序博客网 时间:2024/06/07 00:09

Gym - 100814D Frozen Rivers

这题就是给你一个树形的河,所有的河都结冰了,从根节点u开始,u的所有儿子边都同时融化,每融化一条边,它的兄弟节点融化速度减半。查询在某个时间内,融化后到达饿叶子节点的个数。这题就是一个搜索题,我是用bfs写的,因为查询次数比较多,可以用二分。

/*upper_bound(begin,end,key),start是查找的起点,end是终点,key是关键值,lower_bound()用法一样,upper_bound()函数,返回第一个大于要找的值得位置而Lower_bound是小于等于关键字的位置*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include <bitset>#define INF 0x3f3f3f3f#define eps 1e-6#define PI 3.1415926#define mod 1000000009#define base 2333using namespace std;typedef long long LL;const int inf = 1e9;const int maxn = 1e5 + 10;const int maxx = 1e3 + 10;int t, n, v, q, cnt;LL c, x, ans[maxn], tmp[maxn], val[maxn];vector<int> G[maxn];void bfs() {    queue<int> q;    q.push(1);    while(!q.empty()) {        int cur = q.front();        q.pop();        if(G[cur].size() == 0)            ans[++cnt] = tmp[cur];        else {            LL minn = 1e18;            for(int i = 0; i < G[cur].size(); i++) {                int v = G[cur][i];                minn = min(minn, val[v]);                q.push(v);            }            for(int i = 0; i < G[cur].size(); i++) {                int v = G[cur][i];                tmp[v] = tmp[cur]+minn+2*(val[v]-minn);            }        }    }}void solve() {    scanf("%d", &t);    while(t--) {        scanf("%d", &n);        memset(ans, 0, sizeof(ans));        memset(tmp, 0, sizeof(tmp));        cnt = 0;        for(int i = 0; i <= n; i++)            G[i].clear();        for(int i = 2; i <= n; i++) {            scanf("%d%lld", &v, &c);            val[i] = c, G[v].push_back(i);        }        bfs();        sort(ans+1, ans+cnt+1);        scanf("%d", &q);        while(q--) {            scanf("%lld", &x);            LL *xx = upper_bound(ans+1, ans+cnt+1, x);            printf("%d\n", xx-(ans+1));        }    }}int main() {    //freopen("kingdom.in","r",stdin);    //freopen("kingdom.out","w",stdout);    solve();}