HDU 5886Tower Defence (tree diameter + tree dp)

Source: Internet
Author: User
Tags bool fread relative

"HDU 5886" Tower Defence (tree diameter + tree dp)

Main topic:
A tree with edges, randomly deleting an edge, preserving the larger midline of the remaining two trees.
Request to delete the median line size after an edge * (n-1)

White is the statistic to delete each side of the case of the two trees in the larger midline, to find a and.

For the original tree, two Dfs can find out the midline, and can mark points on the midline.
Then for any one edge u−v u-v, if two points have one not on the midline, after deletion has no effect on the result, the larger midline is still the center line of the original tree.

If two points are in the middle line, then it is necessary for the two newly generated trees to be the largest line.

For the midline of the original tree, a A, a, B, two-time tree DP, respectively, when a is root, the midline of all the subtrees and the midline of all the subtrees at the root.

When you look at this query, determine the distance (i.e. depth) of u,v, respectively, relative to a, b
Figure out the cut after two trees who are relative to a subtree, who is relative to the B's sub-tree.

Take a maximum.

The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack&
Gt  #include <list> #include <algorithm> #include <map> #include <set> #define LL Long Long #define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace s
td
const int INF = 0X3F3F3F3F;
const int MSZ = 10000;
const int mod = 1E9+7;
Const double EPS = 1e-8;

const int MAXN = 100010; 

struct Edge {int v,w,next;} eg[maxn<<1];
BOOL VIS[MAXN],ON[MAXN];
int DIS[MAXN],COST[2][3][MAXN],DEPTH[2][MAXN];
int HEAD[MAXN];
int TP;

int n;
    void Add (int u,int v,int w) {eg[tp].v = v;
    EG[TP].W = W;
    Eg[tp].next = Head[u];
Head[u] = tp++;

    } void dfs1 (int u,int pre) {int v,w; for (int i = head[u]; i =-1; i = eg[i].next) {v = eg[I].V;
        W = EG[I].W;
        if (v = = pre) continue;
        DIS[V] = Dis[u] + W;
    DFS1 (V,u);

}} int st,en;

    BOOL DFS2 (int u,int pre) {int V;

    On[u] = u==en;
        for (int i = head[u]; I! =-1; i = eg[i].next) {v = eg[i].v;
        if (v = = pre) continue;
    if (DFS2 (v,u)) on[u] = 1;
} return On[u];
    } void Dfs3 (int pos,int u,int pre,int dep) {Depth[pos][u] = dep;

    Cost[pos][0][u] = Cost[pos][1][u] = Cost[pos][2][u] = 0;
    int v,w;
        for (int i = head[u]; I! =-1; i = eg[i].next) {v = eg[i].v;
        W = EG[I].W;

        if (v = = pre) continue;

        DFS3 (pos,v,u,dep+1);

        int tmp = COST[POS][1][V]+W;
            if (tmp > Cost[pos][1][u]) {Cost[pos][2][u] = Cost[pos][1][u];
        COST[POS][1][U] = tmp;

        } else if (tmp > Cost[pos][2][u]) cost[pos][2][u] = tmp;
    Cost[pos][0][u] = max (cost[pos][0][u],cost[pos][0][v]); } Cost[pos][0][u] = max (COSt[pos][0][u],cost[pos][1][u]+cost[pos][2][u]);
    } void Init () {dis[1] = 0;
    DFS1 (a);

    st = 1;

    for (int i = 1; I <= n; ++i) if (Dis[i] > dis[st]) st = i;
    DIS[ST] = 0;
    Memset (On,0,sizeof (on));
    DFS1 (ST,ST);

    En = 1;

    for (int i = 1; I <= n; ++i) if (Dis[i] > dis[en]) en = i;

    DFS2 (ST,ST);
    DFS3 (0,st,st,0);
DFS3 (1,en,en,0);

    } int cal (int u,int v) {if (!on[u] | |!on[v]) return Dis[en];
    if (Depth[0][u] > Depth[0][v]) {return max (Cost[0][0][u],cost[1][0][v]);
} return Max (Cost[0][0][v],cost[1][0][u]);
    } int main () {//fread ("");

    Fwrite ("");

    int t,u,v,w;

    scanf ("%d", &t);
        while (t--) {memset (head,-1,sizeof (head));

        TP = 0;
        scanf ("%d", &n);
            for (int i = 1; i < n; ++i) {scanf ("%d%d%d", &u,&v,&w);
            ADD (U,V,W);
        ADD (V,U,W);

        } init (); LL ans = 0; for (U = 1, u <= n; ++u) for (int i = head[u]; I! = 1; i = Eg[i].next) {ans + =
            Cal (U,EG[I].V);
    } printf ("%lld\n", ANS/2);
} 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.