当前位置:   article > 正文

【换根DP】E. Tree Painting

tree painting

E. Tree Painting

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a tree (an undirected connected acyclic graph) consisting of nn vertices. You are playing a game on this tree.

Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any black vertex and paint it black.

Each time when you choose a vertex (even during the first turn), you gain the number of points equal to the size of the connected component consisting only of white vertices that contains the chosen vertex. The game ends when all vertices are painted black.

Let's see the following example:

Vertices 11 and 44 are painted black already. If you choose the vertex 22, you will gain 44 points for the connected component consisting of vertices 2,3,52,3,5 and 66. If you choose the vertex 99, you will gain 33 points for the connected component consisting of vertices 7,87,8 and 99.

Your task is to maximize the number of points you gain.

Input

The first line contains an integer nn — the number of vertices in the tree (2≤n≤2⋅1052≤n≤2⋅105).

Each of the next n−1n−1 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the indices of vertices it connects (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi).

It is guaranteed that the given edges form a tree.

Output

Print one integer — the maximum number of points you gain if you will play optimally.

Examples

input

Copy

9
1 2
2 3
2 5
2 6
1 4
4 9
9 7
9 8

output

Copy

36

input

Copy

5
1 2
1 3
2 4
2 5

output

Copy

14

Note

The first example tree is shown in the problem statement.


题意:找一个根,使得子树之和最大

思路:普通的树形DP是用一次DFS来获取DP[i] (以 i 点为根的子树大小 ),换根DP就是在此基础上 再跑一次DFS 通过推出的式子 O(1)来获取以下一个点做根的贡献

就此题来说,假设当前点的贡献是val ,则下一个点的贡献则为 val - dp[v] + ( n - dp[v] ) , 式子的意义也就是到下一个点时,会少一个以v为根的子树大小,但会多出一个以u为根的子树大小


  1. #include <bits/stdc++.h>
  2. #define endl '\n'
  3. using namespace std;
  4. typedef long long ll;
  5. typedef long double ld;
  6. //typedef __int128 bll;
  7. const int maxn = 2e5 + 100;
  8. const int mod = 1e9+7;
  9. const ll inf = 1e18;
  10. ll n,dp[maxn],ans;
  11. vector<ll>G[maxn];
  12. void dfs(ll now,ll fa,ll val)
  13. {
  14. for(ll i = 0; i < G[now].size(); ++i)
  15. {
  16. ll to = G[now][i];
  17. if(to != fa)
  18. {
  19. ll tmp = val-dp[to]+(n-dp[to]);
  20. ans = max(ans,tmp);
  21. dfs(to,now,tmp);
  22. }
  23. }
  24. return ;
  25. }
  26. void get(ll now,ll fa)
  27. {
  28. dp[now] = 1;
  29. for(ll i = 0; i < G[now].size(); ++i)
  30. {
  31. ll to = G[now][i];
  32. if(to != fa)
  33. {
  34. get(to,now);
  35. dp[now] += dp[to];
  36. }
  37. }
  38. ans += dp[now];
  39. return ;
  40. }
  41. int main()
  42. {
  43. ios::sync_with_stdio(false);
  44. cin.tie(0),cout.tie(0);
  45. cin >> n;
  46. for(ll i = 1; i < n; i++)
  47. {
  48. ll u,v;
  49. cin >> u >> v;
  50. G[u].push_back(v);
  51. G[v].push_back(u);
  52. }
  53. get(1,1);
  54. dfs(1,1,ans);
  55. cout << ans << endl;
  56. return 0;
  57. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/75101
推荐阅读
相关标签
  

闽ICP备14008679号