Tree
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 24702 | Accepted: 8256 |
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.
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.
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
题意:一棵有n个点的有边权的树,问满足x到y距离小于等于m的无序点对(x,y)的个数有多少
解题思路:树分治
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int n, m, x, y, w;
int s[maxn], nt[maxn], e[maxn], val[maxn], cnt;
int sum[maxn], mx[maxn], dis[maxn], vis[maxn], tot;
int dfs(int k, int fa, int p)
{
int ans = 0;
sum[k] = 1; mx[k] = 0;
for (int i = s[k]; ~i; i = nt[i])
{
if (e[i] == fa || vis[e[i]]) continue;
int temp = dfs(e[i], k, p);
sum[k] += sum[e[i]];
mx[k] = max(mx[k], sum[e[i]]);
if (mx[temp] < mx[ans]) ans = temp;
}
mx[k] = max(mx[k], p - sum[k]);
return mx[k] < mx[ans] ? k : ans;
}
void dfs1(int k, int fa, int len)
{
dis[tot++] = len;
for (int i = s[k]; ~i; i = nt[i])
{
if (e[i] == fa || vis[e[i]]) continue;
dfs1(e[i], k, len + val[i]);
}
}
int Find(int k, int len)
{
tot = 0;
dfs1(k, k, len);
sort(dis, dis + tot);
int ans = 0;
for (int i = 0, j = tot - 1; i < tot; i++)
{
while (j >= 0 && dis[i] + dis[j] > m) j--;
if (i < j) ans += j - i;
else break;
}
return ans;
}
int solve(int k, int p)
{
int y = dfs(k, k, p), ans = Find(y, 0);
vis[y] = 1;
for (int i = s[y]; ~i; i = nt[i])
{
if (vis[e[i]]) continue;
ans -= Find(e[i], val[i]);
if (sum[e[i]] < sum[y]) ans += solve(e[i], sum[e[i]]);
else ans += solve(e[i], p - sum[y]);
}
return ans;
}
int main()
{
while (~scanf("%d %d", &n, &m) && (n + m))
{
memset(s, -1, sizeof s);
memset(vis, 0, sizeof vis);
cnt = 0, mx[0] = INF;
for (int i = 1; i < n; i++)
{
scanf("%d%d%d", &x, &y, &w);
nt[cnt] = s[x], s[x] = cnt, e[cnt] = y, val[cnt++] = w;
nt[cnt] = s[y], s[y] = cnt, e[cnt] = x, val[cnt++] = w;
}
printf("%d\n", solve(1, n));
}
return 0;
}