【bzoj1468】Tree

*题目描述:
给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K
*输入:
N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k
*输出:
一行,有多少对点之间的距离小于等于k
*样例输入:
7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
*样例输出:
5
*题解:
点分治。就是把树上的路径统计问题分成过某个顶点和不过这个点的来做。不过这个点的就递归下去做,过这个点的就直接做。每次递归的时候找到这棵树的重心然后继续往下做,这样时间复杂度就有保证了,每一次找到重心后,这棵树的size至少缩小了一半,于是乎点分治的时间复杂度则降为了O(nlog2n)。不过这题统计过一个点的路径时需要排序(貌似可以用基排?反正我懒得手写),于是乎这题的复杂度就为O(nlog22n)的了(如果是基数排序的话就为O(nlog2n))。
*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#ifdef WIN32
    #define LL "%I64d"
#else
    #define LL "%lld"
#endif

#ifdef CT
    #define debug(...) printf(__VA_ARGS__)
    #define setfile() 
#else
    #define debug(...)
    #define filename ""
    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif

#define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
    R char ch; R int cnt = 0; R bool minus = 0;
    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    ch == '-' ? minus = 1 : cnt = ch - '0';
    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    return minus ? -cnt : cnt;
}
#define maxn 300010
#define maxm 600010
struct Edge
{
    Edge *next;
    int to, w;
}*last[maxn], e[maxm], *ecnt = e;
inline void link(R int a, R int b, R int v)
{
    *++ecnt = (Edge) {last[a], b, v}; last[a] = ecnt;
}
int n, k, size[maxn], son[maxn], root, sum, depcnt;
long long ans, deep[maxm], d[maxn];
bool vis[maxn];
void dfsroot(R int x, R int fa)
{
    size[x] = 1; son[x] = 0;
    for (R Edge *iter = last[x]; iter; iter = iter -> next)
    {
        R int pre = iter -> to;
        if (pre == fa || vis[pre]) continue;
        dfsroot(pre, x);
        size[x] += size[pre];
        cmax(son[x], size[pre]);
    }
    cmax(son[x], sum - size[x]);
    if (root == 0 || son[x] < son[root]) root = x;
}
void dfsdep(R int x, R int fa)
{
    deep[++depcnt] = d[x];
    for (R Edge *iter = last[x]; iter; iter = iter -> next)
    {
        R int pre = iter -> to;
        if (pre == fa || vis[pre]) continue;
        d[pre] = d[x] + iter -> w;
        dfsdep(pre, x);
    }
}
inline long long cal(R int x, R int val)
{
    d[x] = val; depcnt = 0;
    dfsdep(x, 0);
    std::sort(deep + 1, deep + depcnt + 1);
    R long long t = 0;
    R int l, r;
    for (l = 1, r = depcnt; l < r; )
    {
        if (deep[l] + deep[r] <= k) {t += r - l; ++l;}
        else --r;
    }
    return t;
}
void work(R int x)
{
    ans += cal(x, 0);
    vis[x] = 1;
    for (R Edge *iter = last[x]; iter; iter = iter -> next)
    {
        R int pre = iter -> to;
        if (vis[pre]) continue;
        ans -= cal(pre, iter -> w);
        sum = size[pre];
        root = 0;
        dfsroot(pre, 0);
        work(root);
    }
}
int main()
{
    n = FastIn(); sum = n;
    for (R int i = 1; i < n; ++i)
    {
        R int a = FastIn(), b = FastIn(), v = FastIn();
        link(a, b, v); link(b, a, v);
    }
    k = FastIn();
    son[root = 0] = 1 << 30;
    dfsroot(1, 0);
    work(root);
    printf("%lld\n", ans );
    return 0;
}
posted @ 2016-06-01 17:04  cot  阅读(107)  评论(0编辑  收藏  举报