Distance Statistics
Time Limit: 2000MS | Memory Limit: 64000K | |
Total Submissions: 2588 | Accepted: 933 | |
Case Time Limit: 1000MS |
Description
Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K <= 1,000,000,000) and wants to know how many pairs of farms lie at a distance at most K from each other (distance is measured in terms of the length of road required to travel from one farm to another). Please only count pairs of distinct farms (i.e. do not count pairs such as (farm #5, farm #5) in your answer).
Input
* Lines 1 ..M+1: Same input format as in "Navigation Nightmare"
* Line M+2: A single integer, K.
* Line M+2: A single integer, K.
Output
* Line 1: The number of pairs of farms that are at a distance of at most K from each-other.
Sample Input
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 10
Sample Output
5
Hint
There are 5 roads with length smaller or equal than 10, namely 1-4 (3), 4-7 (2), 1-7 (5), 3-5 (7) and 3-6 (9).
Source
题意:一棵树内最短距离小于K的点对数量
解题思路:树分治
#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, sum1;
int s[maxn], nt[maxn], e[maxn], val[maxn], cnt;
int sum[maxn], mx[maxn], dis[maxn], vis[maxn], tot;
char ch[5];
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] > sum1) 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 <= m; i++)
{
scanf("%d%d%d%s", &x, &y, &w, ch);
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;
}
scanf("%d", &sum1);
printf("%d\n", solve(1, n));
}
return 0;
}