CodeForces 293E Close Vertices
2016-05-11 18:16
429 查看
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 n, l 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 cin, cout streams or the %I64dspecifier.
Sample Input
Input
Output
Input
Output
9
树分治,然后里面是二维点对的处理,这个怎么做可以参考之前我写的关于偏序的问题的处理方法。
这里可以直接排序加树状数组维护,为了方便一点我直接写的cdq分治。
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 n, l 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 cin, cout 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; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B