CodeForce - 1187 E. Tree Painting (换根dp)

叁歲伎倆 2021-11-09 01:18 372阅读 0赞

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:

ad4fad5ce28ee4d1715321cbdee7db39ee67260c.png

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

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

output

Copy

  1. 36

input

Copy

  1. 5
  2. 1 2
  3. 1 3
  4. 2 4
  5. 2 5

output

Copy

  1. 14
  2. 题意:
  3. 要把树上每个节点染成黑色,每次只能选择染与黑色点相邻的点,第一次染色随意。
  4. 每次染色,所获贡献是,与当前点联通的白色点的个数,包括自身。
  5. 思路:
  6. 首先以1号节点为根节点,计算出每一个节点的子树大小(siz[i]) ,然后计算出每一个节点的所有子树siz之和(dp[i])。
  7. 假设当前根节点是u,子节点是v,那么u去掉v子树的贡献为 res = dp[u]-dp[v]-siz[v];
  8. dp[v]就会变成 dp[v]=dp[v]+res+(n-siz[v])

ContractedBlock.gif ExpandedBlockStart.gif

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<stack>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<cmath>
  11. #include<ctime>
  12. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  13. #define debug(a, x) cout<<#a<<"["<<x<<"] = "<<a[x]<<endl;
  14. #define ls (t<<1)
  15. #define rs ((t<<1)|1)
  16. using namespace std;
  17. typedef long long ll;
  18. typedef unsigned long long ull;
  19. const int maxn = 400086;
  20. const int maxm = 100086;
  21. const int inf = 0x3f3f3f3f;
  22. const ll Inf = 999999999999999999;
  23. const int mod = 1000000007;
  24. const double eps = 1e-6;
  25. const double pi = acos(-1);
  26. int Head[maxn];
  27. struct edge{
  28. int v;
  29. int Next;
  30. }e[maxn];
  31. int cnt=0;
  32. void add(int x,int y){
  33. e[cnt].v=y;
  34. e[cnt].Next = Head[x];
  35. Head[x]=cnt++;
  36. }
  37. ll dp[maxn];
  38. ll siz[maxn];
  39. bool vis[maxn];
  40. int n;
  41. void dfs(int u){
  42. vis[u]=true;
  43. for(int k=Head[u];~k;k=e[k].Next){
  44. if(vis[e[k].v]){ continue;}
  45. dfs(e[k].v);
  46. siz[u]+=siz[e[k].v];
  47. dp[u]+=dp[e[k].v];
  48. }
  49. siz[u]++;
  50. dp[u]+=siz[u];
  51. }
  52. void dfs1(int u){
  53. vis[u]=true;
  54. for(int k=Head[u];~k;k=e[k].Next){
  55. int v = e[k].v;
  56. if(vis[v]){ continue;}
  57. ll res = dp[u]-dp[v]-siz[v];
  58. dp[v]=dp[v]+res-siz[v]+n;
  59. dfs1(v);
  60. }
  61. }
  62. int main() {
  63. // ios::sync_with_stdio(false);
  64. // freopen("in.txt", "r", stdin);
  65. memset(Head,-1,sizeof(Head));
  66. scanf("%d",&n);
  67. for(int i=1;i<n;i++){
  68. int x,y;
  69. scanf("%d%d",&x,&y);
  70. add(x,y);
  71. add(y,x);
  72. }
  73. dfs(1);
  74. memset(vis,0,sizeof(vis));
  75. dfs1(1);
  76. ll ans=0;
  77. for(int i=1;i<=n;i++){
  78. // printf("%lld ",siz[i]);
  79. ans = max(ans,dp[i]);
  80. }
  81. // printf("\n");
  82. printf("%lld\n",ans);
  83. return 0;
  84. }

转载于:https://www.cnblogs.com/ZGQblogs/p/11161361.html

发表评论

表情:
评论列表 (有 0 条评论,372人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Codeforces 735E 树形DP

    题意:给你一棵树,你需要在这棵树上选择一些点染成黑色,要求染色之后树中任意节点到离它最近的黑色节点的距离不超过m,问满足这种条件的染色方案有多少种? 思路:设dp\[x\]\

    相关 CF1187E Tree Painting

    思路: 树形dp,首先使用dp计算以1为根的时候的最大分数,同时得到各个子树i的最大分数dp\[i\]。然后利用前面得到的dp数组分别计算以其他每个点作为根的时候的最大分数。