POJ 2114 Boatherds (树的点分治简单题)

Boatherds

Boatherds Inc. is a sailing company operating in the country of Trabantustan and offering boat trips on Trabantian rivers. All the rivers originate somewhere in the mountains and on their way down to the lowlands they gradually join and finally the single resulting river flows to the sea. Moreover, the Trabantian villages are exactly at the rivers’ springs, junctions and at the mouth of the largest river. Please note that more than 2 rivers can join at a junction. However, the rivers always form a tree (with villages as vertices).

The pricing policy of the Boatherds is very simple: each segment of each river between two villages is assigned a price (the price is same in both directions), so if a tourist requests a journey between any two villages, the ticket office clerks just add the prices of the segments along the only path between the villages.

One day, a very strange tourist appeared. She told the clerks that she returns to her country on the next day and she wants to spend all the remaining money on a boat trip, so they should find a route with exactly this cost. Being just poor (ahem) businessmen, they have asked the Abacus Calculator Makers for help.

You are given a description of the river network with costs of river segments and a sequence of integers x1,…, xk. For each xi, you should determine if there is a pair of cities (a, b) in the river network such that the cost of the trip between a and b is exactly xi.

Input

The input consists of several instances. Each instance is described by (in the following order):
A single line containing a single integer: the number of villages N (1 <= N <= 10 000).
N lines describing the villages. The i-th of these lines (1 <= i <= N) describes the village with number i. It contains space separated integers d1, c1, d2, c2, , dki, cki, 0. The dj’s are numbers of villages from which the rivers flow directly to the village i (with no other villages in between), each cj is the price of the journey between villages i and dj. Moreover, 2 <= dj <= N and 0 <= cj <= 1 000. Village 1 always corresponds to the mouth of the largest river, therefore no di can ever be equal to 1.
M <= 100 lines describing the queries. The i-th of these lines corresponds to the i-th query and contains a single integer xi (1 <= xi <= 10 000 000).
The instance is finished by a single line containing the number 0.

The whole input is ended by a single line containing the number 0.

Output

For each instance you should produce a sequence of M lines (where M is the number of queries in the particular instance). The i-th of these lines contains the word “AYE” if there exists a pair of cities in the river network which is connected by a path of cost xi, or the word “NAY” otherwise.

Output for each instance must be followed by a single line containing just the dot character.

Sample Input

6
2 5 3 7 4 1 0
0
5 2 6 3 0
0
0
0
1
8
13
14
0
0

Sample Output

AYE
AYE
NAY
AYE
.

题意: 给一棵树,然后有若干询问,每次询问给一个 x,是否存在树上两个点的距离恰好等于 x

思路: 和 poj 1741 一样的写法。 对每一次的询问都跑一遍点分治

点分治统计的便是 dis[u] + dis[v] == k 的对数,最后判断对数是否 >0 即可确定答案 .

Code:

#include "cstdio"
#include "cstring"
#include "vector"
#include "algorithm"
#define debug(x) cerr << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a, b) memset((a),b,sizeof(a))
#define rep(i, a, b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()

using namespace std;

#ifndef ONLINE_JUDGE
//clock_t prostart = clock();
#endif

const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
int head[maxn], cnt;
struct xx {
    int v, w, nex;
} edge[maxn << 1];
int ans, vis[maxn], k;
int sonmax[maxn], sonnum[maxn];
vector<int> dis;

void init1(int n) {
    for (int i = 1; i <= n + 5; ++i) head[i] = -1;
    cnt = 0;
}

void init2(int n) {
    for (int i = 0; i <= n + 5; ++i) vis[i] = 0;
    ans = 0;
}

void Tree_Size(int p, int fa) {
    sonmax[p] = sonnum[p] = 1;
    for (int i = head[p]; ~i; i = edge[i].nex) {
        int v = edge[i].v;
        if (vis[v] || v == fa) continue;
        Tree_Size(v, p);
        sonnum[p] += sonnum[v];
        sonmax[p] = max(sonmax[p], sonnum[v]);
    }
}

void Tree_center(int &mi, int &rt, int p, int fa, int sum) {
    for (int i = head[p]; ~i; i = edge[i].nex) {
        int v = edge[i].v;
        if (v == fa || vis[v]) continue;
        Tree_center(mi, rt, v, p, sum);
    }
    if (mi > max(sonmax[p], sum - sonnum[p])) {
        mi = max(sonmax[p], sum - sonnum[p]);
        rt = p;
    }
}

void Tree_dis(int p, int fa, int dep) {
    dis.pb(dep);
    for (int i = head[p]; ~i; i = edge[i].nex) {
        int v = edge[i].v;
        if (v == fa || vis[v]) continue;
        Tree_dis(v, p, dep + edge[i].w);
    }
}

int cal(int p, int d) {
    dis.clear();
    Tree_dis(p, -1, d);
    sort(dis.begin(), dis.end());
    int res = 0;
    for (int i = 0; i < dis.size(); ++i) {
        if (k - dis[i] < dis[i]) break;
        int be = lower_bound(dis.begin(), dis.end(), k - dis[i]) - dis.begin();
        int en = upper_bound(dis.begin(), dis.end(), k - dis[i]) - dis.begin();
        if (dis[be] == k - dis[i]) {
            if (dis[be] == dis[i]) {
                res += en - (i + 1);
            } else {
                res += en - be;
            }
        }
    }
    return res;
}

void dfs(int p, int fa) {
    int mi = inf, rt;
    Tree_Size(p, fa);
    Tree_center(mi, rt, p, fa, sonnum[p]);
    ans += cal(rt, 0);
    vis[rt] = 1;
    for(int i = head[rt]; ~i;i = edge[i].nex){
        int v = edge[i].v;
        if(vis[v] || v == fa) continue;
        ans -= cal(v,edge[i].w);
        dfs(v,rt);
    }
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif

    int n;
    while (~scanf("%d", &n) && n) {
        init1(n);
        int d, c;
        for (int i = 1; i <= n; ++i) {
            while (scanf("%d", &d) && d) {
                scanf("%d", &c);
                edge[cnt] = xx{d, c, head[i]};
                head[i] = cnt++;
                edge[cnt] = xx{i, c, head[d]};
                head[d] = cnt++;
            }
        }
        while (scanf("%d", &k) && k) {
            init2(n);
            dfs(1,-1);
//            debug(ans);
            if(ans > 0) puts("AYE");
            else puts("NAY");
        }
        puts(".");
    }

#ifndef ONLINE_JUDGE
//    cerr << "time: " << 1.0 * (clock() - prostart) / CLOCKS_PER_SEC << " s" << endl;
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值