POJ1987-Distance Statistics

本文介绍了一种使用树分治算法解决特定距离内点对数量统计的问题,通过实例详细解析了输入输出格式及核心代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值