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

楼教主的男人八题里的,很出名我就简单说下题意,给你一片森林,每条边有距离权值,求两点之间距离小于K的点对数。

这题明显树的点分治。每棵树(包括子树)的点对分为三种:root到其他点,跨过root的两子树的点,子树内部的点

因为分治的思想,其中子树内部的点我们是不能重复算的,每次计算减掉即可。

dfs求出每棵树以及子树的重心作为新的root(为了降低复杂度),dfs求出每个点到root的距离,统计即可,其中代码多为找重心以及对多棵树的处理,敲的时候很懵逼,敲完思路就很清晰了。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=40005;
char ch[5];
struct node
{
    int to,w;
    node(int _to,int _w):to(_to),w(_w) {}
};
vector<int> dep;
vector<node> mp[N];
int son[N],d[N],f[N];
bool vis[N],done[N];
int n,m,root,k,size,ans=0;
void dfs(int x) //第一次处理,为了寻找root后能知道该树的size值
{
    vis[x]=1;
    son[x]=1;
    for(int i=0; i<mp[x].size(); i++)
    {
        int t=mp[x][i].to;
        if(!vis[t])
        {
            dfs(t);
            son[x]+=son[t];
        }
    }
}
void getroot(int x,int fa) //找root值 注意其实f[x]没什么作用,完全可以用两个变量替代,懒了就这么写了
{
	f[x]=0;
	son[x]=1;
	for(int i=0;i<mp[x].size();i++)
	{
		int t=mp[x][i].to;
		if(t!=fa&&!done[t])
		{
			getroot(t,x);
			son[x]+=son[t];
			f[x]=max(f[x],son[t]);
		}
	}
	f[x]=max(f[x],size-son[x]);
	if(f[x]<f[root]) root=x;
}
void getdep(int x,int fa) //找dep
{
	dep.push_back(d[x]);
	for(int i=0;i<mp[x].size();i++)
	{
		int t=mp[x][i].to;
		if(t!=fa&&!done[t])
		{
			d[t]=d[x]+mp[x][i].w;
			getdep(t,x);
		}
	}
}
int calc(int x,int init) //计算root两边和root与子树点距离小于K的点对数量
{
	d[x]=init;
	dep.clear();
	getdep(x,0);
	sort(dep.begin(),dep.end());
	int res=0;
	for(int l=0,r=dep.size()-1;l<r;)
		if(dep[l]+dep[r]<=k) res+=r-l++;
		else r--;
	return res;
}
void work(int x)
{
	ans+=calc(x,0);
	done[x]=1;
	for(int i=0;i<mp[x].size();i++)
	{
		int t=mp[x][i].to;
		if(!done[t])
		{
			ans-=calc(t,mp[x][i].w);
			f[0]=size=son[t];
			getroot(t,root=0);
			work(root);
		}
	}
}
int main()
{
    int tt=0,T,x,y,w;
    scanf("%d %d",&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d %d %d %s",&x,&y,&w,ch);
        mp[x].push_back(node(y,w));
        mp[y].push_back(node(x,w));
    }
    scanf("%d",&k);
    memset(done,0,sizeof(done));
    memset(vis,0,sizeof(vis));
    for(int i=1; i<=n; i++)
        if(!vis[i]) dfs(i);
        ans=0;
	for(int i=1;i<=n;i++)
	{
		if(!done[i])
		{
			f[0]=size=son[i];
			getroot(i,root=0);
			work(root);
		}
	}
	printf("%d\n",ans);
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值