The diameter of the gym-101522b tree

Source: Internet
Author: User
gym-101522b

Test Instructions:

Give a tree, then create a new edge in the way given, and then build a layer of edges until you can build it. Ask how many hours, every one hours can build an edge, can be multi-threaded build.

Ideas:

Drawing can find the law, the worst case is that n points is a linear, then the time will be the longest, according to the linear law found that its length and time is twice times the increment, each new hour can be on the original basis to draw twice times the line, so find the longest side and then Judge 2 of the ANS can.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

Queue> #include <vector> using namespace std;

const int MAXN = 5E5+10;
    struct Edge {int to,v;
int next;

}EDGE[MAXN*2];
int n;

int head[maxn],pos;
int dp[maxn][3];

int SONMAX[MAXN];
    void init () {pos = 0;
    memset (head,-1,sizeof (head));
    Memset (Dp,0,sizeof (DP));
memset (sonmax,0,sizeof (Sonmax));
    } void Add_edge (int u,int v,int val) {edge[pos].to = v;
    EDGE[POS].V = val;
    Edge[pos].next = Head[u];
    Head[u] = pos++;
    edge[pos].to = u;
    EDGE[POS].V = val;
    Edge[pos].next = Head[v];
HEAD[V] = pos++;
        } void Input () {for (int i = 2;i <= n; i++) {int A, B;
        scanf ("%d%d", &a,&b);
    Add_edge (a,b,1);
    }} void dfs1 (int u,int fa) {dp[u][0] = dp[u][1] = dp[u][2] = Sonmax[u] = 0;
        for (int i = head[u];i! =-1; i = edge[i].next) {int to = edge[i].to; if (to = = fa) continue;
        DFS1 (To,u);
            if (Dp[u][0] < dp[to][0] + edge[i].v) {Sonmax[u] = to;
            DP[U][1] = max (dp[u][0],dp[u][1]);
        Dp[u][0] = dp[to][0] + edge[i].v;   
    } else dp[u][1] = max (dp[u][1],dp[to][0] + edge[i].v);
        }} void dfs2 (int u,int fa) {for (int i = head[u];i! = 1; i = edge[i].next) {int to = edge[i].to;
        if (to = = FA) continue;
        if (sonmax[u] = = to) {dp[to][2] = max (dp[u][2],dp[u][1]) + edge[i].v;
        } else dp[to][2] = max (dp[u][2],dp[u][0]) + edge[i].v;
    DFS2 (To,u);

    }} int main (int argc, char const *argv[]) {//freopen ("In.txt", "R", stdin);
    scanf ("%d", &n);
    Init ();
    Input ();
    DFS1 (1,-1);
    DFS2 (1,-1);
    int Max = 0;
    for (int i = 1;i <= n; i++) {max = max (max,dp[i][0]+dp[i][2]);
    A long long is 2;
    int ans = 1;
        while (both < Max) {n = two*2;
    ans++;
    }if (n = = 2) ans = 0;
    printf ("%d\n", ans);
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.