Hdu2196-computer (tree-shaped DP)

Source: Internet
Author: User

Too happy, finally made out.

First six months of the problem AH ~ ~ ~

First, a simple approach, is to find the two ends of the diameter, DFS, respectively, find the two ends to each point of the distance to take the maximum is OK.

/********************************************************problem:2196 (computer) Judge status:acceptedrunid:15 495812 language:g++ author:g_lory********************************************************/#include <bits/stdc+ +.h>using namespace Std;const int N = 100005;vector<int> g[n];vector<int> l[n];int dis1[N];int Dis2[N];    int Len;int fa, fb;void dfs (int pre, int v, int d[], int x) {D[v] = x;        for (int i = 0; i < g[v].size (); ++i) {int u = g[v][i];        if (U = = pre) continue;    DFS (V, u, D, x + L[v][i]);        } if (x > Len) {len = x;    FA = V;    }}int Main () {Std::ios::sync_with_stdio (false);    int n;    int A, B;            while (CIN >> N) {for (int i = 0; I <= N; ++i) {g[i].clear ();        L[i].clear ();            } for (int i = 2; I <= n; ++i) {cin >> a >> b;            G[a].push_back (i);      G[i].push_back (a);      L[a].push_back (b);        L[i].push_back (b);        } len =-1;        DFS ( -1, 1, dis1, 0);        Len =-1;        DFS ( -1, FA, Dis1, 0);        Len =-1;        DFS ( -1, FA, Dis2, 0);        for (int i = 1; I <= n; ++i) {cout << max (Dis1[i], dis2[i]) << Endl; }} return 0;}

  

Then there is the approach of the tree-shaped DP. Really do not understand do not understand Ah oh ah ah ah!

Over and over again bullish Bull Calf's blog, finally see Kuangbin giant code understand ... Sad... Watched for two or three days ...

For each point, its longest distance must be its child tree chain, or the path through its parent node. Crap

The first time DFS is evaluated is the longest sub-path of each knot tree.

The second time DFS is to update each of its child nodes with the parent node.

If the node is on the longest chain, it is updated with the junior chain, otherwise it is updated with the longest chain.

Give me a chestnut.

Input data:

5

1 1

2 1

3 1

3 1

By the first time Dfs can find out

dis[1]=3,sdis[1]=0;

dis[2]=2,sdis[2]=0;

Dis[3]=1,sdis[3]=1;

dis[4]=0,sdis[4]=0;

Dis[5]=0,sdis[5]=0.

Then the second time DFS starts with 1 updates.

2 is the oldest path of the 1,sdis[1]+d[1][2]>sdis[2],sdis[2]=1;

3 is the oldest path of the 2,sdis[2]+d[2][3]>dis[3],dis[3]=2,sdis[3]=1; At the same time, update 3 's oldest path is position 2

Dis[3]+d[3][4]>dis[4],dis[4]=3

Dis[3]+d[3][5]>dis[5],dis[5]=3

It's a long-winded story, anyway ^_^

/****************************************************problem:2196 (computer) Judge status:acceptedrunid:154969 language:g++ author:g_lory****************************************************/#include <bits/stdc++.h> using namespace Std;const int N = 100005;vector<int> g[n];vector<int> l[n];int dis[n];int sdis[n];int id[N];v        OID DFS (int v, int pre) {for (int i = 0; i < g[v].size (); ++i) {int u = g[v][i];        if (U = = pre) continue;        DFS (U, v);            if (Dis[v] < Dis[u] + L[v][i]) {Sdis[v] = Dis[v];            DIS[V] = Dis[u] + l[v][i];        ID[V] = u;        } else if (Sdis[v] < Dis[u] + L[v][i]) {Sdis[v] = Dis[u] + l[v][i];        }}}void dfs1 (int v, int pre) {for (unsigned i = 0; i < g[v].size (); ++i) {int u = g[v][i];        if (U = = pre) continue;     if (U = = Id[v]) {if (L[v][i] + sdis[v] > Dis[u]) {           Sdis[u] = Dis[u];                Dis[u] = L[v][i] + sdis[v];            Id[u] = v;            } else if (L[v][i] + sdis[v] > Sdis[u]) {Sdis[u] = L[v][i] + sdis[v]; }} else {if (L[v][i] + dis[v] > Dis[u]) {Sdis[u] = Dis[u]                ;                Dis[u] = L[v][i] + dis[v];            Id[u] = v;            } else if (L[v][i] + dis[v] > Sdis[u]) {Sdis[u] = L[v][i] + dis[v];    }} dfs1 (U, v);    }}int Main () {Std::ios::sync_with_stdio (false);    int n;    int A, B;        while (CIN >> N) {memset (dis, 0, sizeof dis);        memset (SDIs, 0, sizeof SDIs);            for (int i = 0; I <= N; ++i) {g[i].clear ();        L[i].clear ();            } for (int i = 2; I <= n; ++i) {cin >> a >> b;            G[a].push_back (i); G[i].push_back (a);            L[a].push_back (b);        L[i].push_back (b);        } DFS (1,-1);        DFS1 (1,-1);        for (int i = 1; I <= n; ++i) {cout << dis[i] << Endl; }} return 0;}

  

Hdu2196-computer (tree-shaped DP)

Related Article

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.