POJ 1987 Distance Statistics

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). 


树分治,同POJ1741,这次又重新写了一遍,一A,感觉理解有深刻了一点。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const double pi = acos(-1.0);
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int n, m, x, y, z, K;
char ch[5];

struct Tree
{
	const static int maxn = 1e5 + 10;
	int ft[maxn], nt[maxn], u[maxn], v[maxn], sz, tot;
	int dis[maxn], vis[maxn], cnt[maxn], mx[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 z = mx[x] = 0;
		cnt[x] = 1;
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (u[i] == fa || vis[u[i]]) continue;
			int y = dfs(u[i], x, sum);
			cnt[x] += cnt[u[i]];
			mx[x] = max(mx[x], cnt[u[i]]);
			if (mx[z] > mx[y]) z = y;
 		}
		mx[x] = max(mx[x], sum - cnt[x]);
		return mx[x] < mx[z] ? x : z;
	}
	void dfsdis(int x, int fa, int len)
	{
		dis[tot++] = len;
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (u[i] == fa || vis[u[i]]) continue;
			dfsdis(u[i], x, len + v[i]);
		}
	}
	LL find(int x, int d)
	{
		tot = 0;
		dfsdis(x, -1, d);
		sort(dis, dis + tot);
		LL ans = 0;
		for (int i = 0, j = tot - 1; i < j; i++)
		{
			while (j > i&&dis[i] + dis[j] > K) j--;
			ans += j - i;
		}
		return ans;
	}
	LL work(int x, int sum)
	{
		int y = dfs(x, -1, sum);
		LL ans = find(y, 0);	vis[y] = 1;
		for (int i = ft[y]; i != -1; i = nt[i])
		{
			if (vis[u[i]]) continue;
			ans -= find(u[i], v[i]);
			ans += work(u[i], cnt[u[i]] > cnt[y] ? sum - cnt[y] : cnt[u[i]]);
		}
		return ans;
	}
}solve;

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		solve.clear(n);
		while (m--)
		{
			scanf("%d%d%d%s", &x, &y, &z, ch);
			solve.AddEdge(x, y, z);
		}
		scanf("%d", &K);
		printf("%d\n", solve.work(1, n));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值