CodeForces 293E Close Vertices

Description

You've got a weighted tree, consisting of n vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.

Two vertices are close if there exists a path of length at most l between them and a path of weight at most w between them. Count the number of pairs of vertices v, u(v < u), such that vertices v and u are close.

Input

The first line contains three integers nl and w(1 ≤ n ≤ 105, 1 ≤ l ≤ n, 0 ≤ w ≤ 109). The next n - 1 lines contain the descriptions of the tree edges. The i-th line contains two integers pi, wi(1 ≤ pi < (i + 1), 0 ≤ wi ≤ 104), that mean that the i-th edge connects vertex (i + 1) and pi and has weight wi.

Consider the tree vertices indexed from 1 to n in some way.

Output

Print a single integer — the number of close pairs.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Sample Input

Input
4 4 6
1 3
1 4
1 3
Output
4
Input
6 2 17
1 3
2 5
2 13
1 6
5 9
Output

9

树分治,然后里面是二维点对的处理,这个怎么做可以参考之前我写的关于偏序的问题的处理方法。

这里可以直接排序加树状数组维护,为了方便一点我直接写的cdq分治。

#include<queue>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int INF = 0x7FFFFFFF;
const int maxn = 2e5 + 10;
int n, l, w, x, y;

struct Tree
{
	int ft[maxn], nt[maxn], u[maxn], v[maxn], sz;
	int mx[maxn], ct[maxn], vis[maxn], t;
	struct point
	{
		int x, y, z;
		point(int x = 0, int y = 0, int z = 0) :x(x), y(y), z(z) {};
		bool operator<(const point&a)const
		{
			return x == a.x ? y == a.y ? z < a.z : y < a.y : x < a.x;
		}
	}a[maxn], b[maxn];

	void clear(int n)
	{
		mx[sz = 0] = INF;
		for (int i = 1; i <= n; i++) ft[i] = -1, vis[i] = 0;
	}
	void AddEdge(int x, int y, int z)
	{
		u[sz] = y;	v[sz] = z;	nt[sz] = ft[x];	ft[x] = sz++;
		u[sz] = x;	v[sz] = z;	nt[sz] = ft[y]; ft[y] = sz++;
	}
	int dfs(int x, int fa, int sum)
	{
		int y = mx[x] = (ct[x] = 1) - 1;
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (vis[u[i]] || u[i] == fa) continue;
			int z = dfs(u[i], x, sum);
			ct[x] += ct[u[i]];
			mx[x] = max(mx[x], ct[u[i]]);
			y = mx[y] < mx[z] ? y : z;
		}
		mx[x] = max(mx[x], sum - ct[x]);
		return mx[x] < mx[y] ? x : y;
	}
	void get(int x, int fa, int dep, int len)
	{
		a[t++] = point(dep, len, 0);
		a[t++] = point(l - dep, w - len, 1);
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (u[i] == fa || vis[u[i]]) continue;
			get(u[i], x, dep + 1, len + v[i]);
		}
	}
	LL merge(int l, int r)
	{
		if (l == r) return 0;
		int m = l + r >> 1;
		LL ans = merge(l, m) + merge(m + 1, r);
		for (int i = l, j = l, k = m + 1, ct = 0; i <= r; i++)
		{
			if (j <= m && (a[j].y < a[k].y || a[j].y == a[k].y&&a[j].z <= a[k].z || k > r))
			{
				b[i] = a[j++];	ct += b[i].z ^ 1;
			}
			else
			{
				b[i] = a[k++];	ans += b[i].z*ct;
			}
		}
		for (int i = l; i <= r; i++) a[i] = b[i];
		return ans;
	}
	LL find(int x, int dep, int len)
	{
		LL ans = t = 0;
		get(x, -1, dep, len);
		sort(a, a + t);
		for (int i = 0; i < t; i++)
		{
			if (a[i].x + a[i].x <= l&&a[i].y + a[i].y <= w) ans += a[i].z ^ 1;
		}
		return merge(0, t - 1) - ans >> 1;
	}
	LL work(int x, int sum)
	{
		int y = dfs(x, -1, sum);
		LL ans = find(y, 0, 0);	vis[y] = 1;
		for (int i = ft[y]; i != -1; i = nt[i])
		{
			if (vis[u[i]]) continue;
			ans -= find(u[i], 1, v[i]);
			ans += work(u[i], ct[u[i]] > ct[y] ? sum - ct[y] : ct[u[i]]);
		}
		return ans;
	}
}solve;

int main()
{
	while (scanf("%d%d%d", &n, &l, &w) != EOF)
	{
		solve.clear(n);
		for (int i = 1; i < n; i++)
		{
			scanf("%d%d", &x, &y);
			solve.AddEdge(i + 1, x, y);
		}
		printf("%I64d\n", solve.work(1, n));
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值