[codeforces 161D] Distance in Tree(树的点分治)

给定一棵树和一个正整数k,要求找出树中距离为k的节点对的数量。输入包含树的节点数和k值,以及描述树边的连接信息。输出为符合条件的节点对数。对于给定的例子,当k=2时,第一棵树中有4对节点距离为2,第二棵树中有2对节点距离为3。

题目http://codeforces.com/problemset/problem/161/D

D. Distance in Tree
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A tree is a connected graph that doesn’t contain any cycles.

The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.

You are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertices which have a distance of exactly k between them. Note that pairs (v, u) and (u, v) are considered to be the same pair.
Input

The first line contains two integers n and k (1 ≤ n ≤ 50000, 1 ≤ k ≤ 500) — the number of vertices and the required distance between the vertices.

Next n - 1 lines describe the edges as “ai bi” (without the quotes) (1 ≤ ai, bi ≤ n, ai ≠ bi), where ai and bi are the vertices connected by the i-th edge. All given edges are different.
Output

Print a single integer — the number of distinct pairs of the tree’s vertices which have a distance of exactly k between them.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input

5 2
1 2
2 3
3 4
2 5

Output

4

Input

5 3
1 2
2 3
3 4
4 5

Output

2

Note

In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).

题意
给一棵树,统计距离为k的点对数。

思路:和上一题一样,只不过统计是=k的对数;
在cal函数中引入 tmp[],cnt[],代表作到x子树时,当前层solve中之前子树《=k的dis个数,因为k<=500;O(n)计算,且不重复;

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#define N 50005
using namespace std;
int n,K,rt,sz[N];
int cnt[505],tmp[505];
vector<int> lin[N];
int vis[N],f[N];
int size;
long long  ans;
int getrt(int x,int fa)
{
    sz[x]=1;
    f[x]=0;
    for(int i=0;i<lin[x].size();i++)
    {
        int u=lin[x][i];
        if(u==fa||vis[u]) continue;
        getrt(u,x);
        sz[x]+=sz[u];
        f[x]=max(f[x],sz[u]);
    }
    f[x]=max(f[x],size-f[x]);
    if(f[rt]>f[x]) rt=x;

}
void getdis(int x,int fa,int dis)
{
    sz[x]=1;
    if(dis<=K)
    {   
        ans+=cnt[K-dis];
        ++tmp[dis];
    }
    for(int i=0;i<lin[x].size();i++)
    {
        int u=lin[x][i];
        if(vis[u]||u==fa) continue;
        getdis(u,x,dis+1);
        sz[x]+=sz[u];
    }
}
void cal(int x)
{
    memset(cnt,0,sizeof(cnt));
    int ret=0;
    cnt[0]=1;
    for(int i=0;i<lin[x].size();i++)
    {
        int u=lin[x][i];
        if(vis[u]) continue;
        memset(tmp,0,sizeof(tmp));
        getdis(u,u,1);
        for(int j=1;j<=K;j++)
        cnt[j]+=tmp[j];

    }
}
void solve(int x)
{
    vis[x]=1;
    cal(x);
    for(int i=0;i<lin[x].size();i++)
    {
        int u=lin[x][i];
        if(vis[u]) continue;    
        f[0]=size=sz[u];
        getrt(u,rt=0);
        solve(rt);
    }
}
int main()
{
    int aa,bb;
    scanf("%d%d",&n,&K);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&aa,&bb);
        lin[aa].push_back(bb);
        lin[bb].push_back(aa);
    }
    f[0]=size=n;
    getrt(1,rt=0);
    solve(rt);
    cout<<ans<<endl;
}
### 解决方案概述 对于给定的问题,在一维坐标系中有 N 个点,每个点具有特定的权重 w。如果两个点之间的距离大于它们各自权重之和,则这两个点之间可以建立一条边。目标是从这些点中找到最大数量的点使得任意两点间均存在边。 为了实现这一目的,可以通过构建不等式 Xi-Wi ≥ Xj+Wj 来确定有向图中的节点关系[^3]。具体来说: - 对于每一对不同的点 (Xi, Wi)(Xj, Wj),当 Xi > Xj 并且 Xi - Wi >= Xj + Wj 成立时,表示从 j 到 i 存在一个方向。 - Xj| >= Wi + Wj 的判断过程。 基于上述分析,算法设计如下: 1. 创建一个新的数组 P[],其中存储的是经过变换后的数据对 (Xi + Wi, Xi - Wi)。 2. 将此新创建的数据集按照第二个分量升序排列;如果有相同的情况,则按第一个分量降序处理。 3. 初始化计数器 count=0 及当前最小右端点 cur_min_right=-&infin;。 4. 遍历排序后的列表: - 如果当前元素的第一个分量大于等于cur_min_right,则更新count并设置新的cur_min_right为该元素的第二分量。 5. 输出最终的结果即为所求的最大团大小。 以下是 Python 实现代码示例: ```python def max_clique(n, points): # 计算转换后的点集合 [(xi+wi, xi-wi)] transformed_points = sorted([(points[i][0]+points[i][1], points[i][0]-points[i][1]) for i in range(n)], key=lambda x:(x[1], -x[0])) result = 0 current_end = float('-inf') for start, end in transformed_points: if start >= current_end: result += 1 current_end = end return result ``` 通过这种方法能够有效地解决问题,并获得最优解。值得注意的是,这里采用了一种贪心策略来逐步增加符合条件的顶点数目,从而保证了结果是最优的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值