POJ 1740 Tree (Point division)

Source: Internet
Author: User
Problem Analysis

This problem is a challenge on the topic, challenge the above code written very long and not easy to understand. This question is also one of the eight questions of Lou men. However, with the increasing difficulty of the algorithm competition, the previous thesis problem has now become a template problem, there are a lot of points on the point of treatment, regional competitions have also been out.
This question concrete method is this, we first to the tree to find the center of gravity, the center of gravity does not understand can go to look for information. Then we divide the tree according to the center of gravity, we calculate the root distance is less than or equal to K node, and then calculate how many pairs, but we will find that our sub-tree is divided when these points are repeated calculations, so we can need to subtract the same nodes on a subtree, and only calculate the vertex pairs not on the same subtree. Then a recursive calculation can be done. Because there is a sort time complexity of O (n∗log2n) O (n*log^2n), see the code if there is nothing to understand.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace
Std

const int MAXN = 1E5+100; struct edge{int to, Next, Val;}

e[maxn<<1]; int HEAD[MAXN], tot, N, k, ans = 0;
Forward star, title input value int VIS[MAXN], SIZ[MAXN], NUM[MAXN], DEP[MAXN], S[MAXN], root, top, tot_size;
    Center, subtree node number of PIPS nodes maximum distance from root node void Addedge (int from, int. to, int val) {e[tot].to = to;
    E[tot].val = val;
    E[tot].next = Head[from];
Head[from] = tot++;
    } void Get_root (int u, int fa) {//Find center of gravity siz[u] = 1;
    Num[u] = 0;
        for (int i = head[u]; i =-1; i = e[i].next) {int v = e[i].to;
        if (Vis[v] | | v = = FA) continue;
        Get_root (V, u);
        Siz[u] + = Siz[v];
    Num[u] = max (Num[u], siz[v]);
    } Num[u] = max (Num[u], tot_size-siz[u]);
if (Num[u] < num[root]) root = u;
    } void Get_dep (int u, int fa) {if (Dep[u] <= k) s[top++] = Dep[u]; for (int i = head[u]; i =-1; i = e[i].next) {iNT V = e[i].to;
        if (Vis[v] | | v = = FA) continue;
        DEP[V] = Dep[u] + e[i].val;
    GET_DEP (V, u);
    }} int Count_pair (int u, int val) {top = 0;
    Dep[u] = val;
    GET_DEP (U,-1);
    Sort (S, s+top);
    int L = 0, r = top-1, res = 0;
            while (L < R) {if (S[l] + s[r] <= k) {res + = R-l;
        ++l;
    } else--r;
} return res; } void Dfs (int u) {//divide vis[u] = 1;//The point is marked as center of gravity ans + = count_pair (u, 0);//Calculates all points to the center of gravity distance not exceeding the value of K all values for (int i = he Ad[u]; I! =-1;
        i = e[i].next) {int v = e[i].to;
        if (Vis[v]) continue;
        Ans-= Count_pair (V, e[i].val);
        root = 0;
        Tot_size = Siz[v];
        Get_root (V,-1);
    DFS (root);
    }} void Init () {memset (Vis, 0, sizeof (VIS));
    Memset (Head,-1, sizeof (head));
    root = tot = 0;
Num[0] = Tot_size = n;
    } void Solve () {init ();
    int from, to, Val; for (int i = 1; i < n; i++) {scanf ("%d%d%d", &AMP;FROM, &to, &val);
        Addedge (from, to, Val);
    Addedge (To, from, Val);
    } ans = 0;
    Get_root (1,-1);
    DFS (1);
printf ("%d\n", ans); } int main () {while (scanf ("%d%d", &n, &k)! = EOF && (n| |
    k)) solve ();
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.