zroj#398. "18 Increase 7" Random walk (expected DP tree DP)

Source: Internet
Author: User

Test instructions

[topic link] Copyright reason is not issued.

Give a tree to find the maximum value of the desired distance between any two points

Sol

A more halal question.

Set \ (f[x]\) indicates the desired number of steps from \ (x\) to the father of \ (x\)

\ (g[x]\) indicates the desired number of steps coming from the father

\ (d[x]\) indicates the degree of the \ (x\) node

Not difficult to get the equation \ (f[x] = \sum_{to \in son[x]} F[to] + d[x]\)

\ (G[x] = g[fa[x]] + \sum_{to \in son[fa[x]] \text{and} to \not = x} F[to] + d[fa[x]]\)

Maintain up-down maximum value when calculating

Of course, careful observation is not difficult to find \ (f[x]\) is the degree of all nodes in the subtree

\ (g[x]\) the degree of the node outside the subtree for the entire tree

Considering the contribution of each side is not difficult to get

\ (F[x] = 2 * siz[x]-1\)

\ (G[x] = 2 * (n-siz[x])-1\)

#include <bits/stdc++.h> #define CHMAX (A, b) (A = a > B a:b) #define LL Long Long const int MAXN = 1e5 + 10;inli    NE int read () {char c = getchar (); int x = 0, f = 1;    while (C < ' 0 ' | | c > ' 9 ') {if (c = = '-') f =-1; c = GetChar ();}    while (c >= ' 0 ' && C <= ' 9 ') x = x * ten + C-' 0 ', C = GetChar (); return x * f;}  Std::vector<int> v[maxn];int N, UP[MAXN], DOWN[MAXN], D[MAXN], Siz[maxn], ans, f[maxn], g[maxn];void dfs3 (int x, int    FA) {siz[x] = 1;        for (int i = 0, to; i < v[x].size (); i++) {if (to = v[x][i]) = = FA) continue;        DFS3 (to, X);                SIZ[X] + = siz[to];        Ans = std::max (ans, Std::max (up[x] + g[to] + down[to], down[x] + f[to] + up[to]));        Chmax (Up[x], up[to] + f[to]);    Chmax (Down[x], down[to] + g[to]);    Chmax (ans, up[x] + down[x]);    } F[x] = (Siz[x] << 1)-1; G[X] = ((n-siz[x) << 1)-1;}    int main () {N = read (); for (int i = 1; i < N; i++) {int x = Read (), y = Read (); d[x]++;        d[y]++; V[x].push_back (y);     V[y].push_back (x);    } dfs3 (1, 0); printf ("%lld", ans);    Puts (". 00000"); return 0;}

zroj#398. "18 Increase 7" Random walk (expected DP tree DP)

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.