Tree
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 28194 Accepted: 9395
Description
Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
Source
LouTiancheng@POJ
POJ 1741 树上点分治
给你一个n个节点的带权无向树,n<=20000;
给你一个limit,limit<=1e9;
求存在多少对(u,v)满足dis(u,v)<=limit的关系;
如果给你一颗灰常简单的树,只有两层。
那么我们可以通过记录子节点到根节点的最短路,来进行处理。
即 if(dis[x]+dis[y]<=limit) ans++;
如果将所有子节点遍历一遍是O(n^2)的效率,但是这里存在决策单调性,可以O(n)处理。
核心代码为:
int compute(int u, int d) {//统计答案
t = 0;
dfs(u, d, 0);
sort(a + 1, a + t + 1);
int l = 1, r = t, res = 0;
while (l < r) {
if (a[l] + a[r] <= K) {
res += r - l;
l++;}else r--;}
return res;
}
已经知晓了子问题的解决方法,那么问题来了:
1.这颗树怎么分割;2.子节点和根节点之间的答案怎么统计。
问题一:
选择树的重心进行分割(避免出现链结构造成的效率退化)
树的重心:以重心为根的树,用节点最多的子树的节点数量,是所有树的划分中最小的。
void get_root(int u, int fa) {//寻找树的重心
sz[u] = 1; f[u] = 0;
for (int i = head[u], v = e[i].v; i > -1; i = e[i].nx, v = e[i].v) {
if (v != fa && !vis[v]) {
get_root(v, u);
sz[u] += sz[v];
f[u] = max(f[u], sz[v]);
}
}
f[u] = max(f[u], all - sz[u]);
if (f[u] < f[root])root = u;
}
问题二:
有了一的操作之后,除了和自己的父亲直接相连之外,其余的全部转换成左右字树操作。
注意距离需要更新下去。
那么可以开始分治了:
选择重心作为根节点——>统计所有答案-两点处于同一子树的答案——>答案合并
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 1e5 + 10;
const int N = 1e4 + 10;
struct node {
int v, nx, w;
}e[N * 4];
bool vis[N];
int n, K, head[N], k, d[N], a[N], f[N], sz[N], root, t, ans, all;
void add(int u, int v, int w) {
e[k].v = v; e[k].nx = head[u]; e[k].w = w; head[u] = k++;
e[k].v = u; e[k].nx = head[v]; e[k].w = w; head[v] = k++;
}
void get_root(int u, int fa) {
sz[u] = 1; f[u] = 0;
for (int i = head[u], v = e[i].v; i > -1; i = e[i].nx, v = e[i].v) {
if (v != fa && !vis[v]) {
get_root(v, u);
sz[u] += sz[v];
f[u] = max(f[u], sz[v]);
}
}
f[u] = max(f[u], all - sz[u]);
if (f[u] < f[root])root = u;
}
void dfs(int u, int d, int fa) {
a[++t] = d;
for (int i = head[u], v = e[i].v; i > -1; i = e[i].nx, v = e[i].v) {
if (v != fa && !vis[v])
dfs(v, d + e[i].w, u);
}
}
int compute(int u, int d) {
t = 0;
dfs(u, d, 0);
sort(a + 1, a + t + 1);
int l = 1, r = t, res = 0;
while (l < r) {
if (a[l] + a[r] <= K) {
res += r - l;
l++;
}
else r--;
}
return res;
}
void divided_rule(int u) {
ans += compute(u, 0); vis[u] = 1;
for (int i = head[u], v = e[i].v; i > -1; i = e[i].nx, v = e[i].v) {
if (!vis[v]) {
ans -= compute(v, e[i].w);
root = 0; all = sz[v];
get_root(v, u); divided_rule(root);
}
}
}
int main()
{
while (~scanf("%d%d", &n, &K) && n + K) {
memset(vis, 0, sizeof vis);
memset(head, -1, sizeof head);
ans = 0, k = 0;
for (int i = 2; i <= n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
root = 0; f[0] = INF; all = n; get_root(1, 0);
divided_rule(root); printf("%d\n", ans);
}
return 0;
}