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

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

题意:要把树上每个节点染成黑色,每次只能选择染与黑色点相邻的点,第一次染色随意。每次染色,所获贡献是,与当前点联通的白色点的个数,包括自身。

思路:首先以1号节点为根节点,计算出每一个节点的子树大小(siz[i]) ,然后计算出每一个节点的所有子树siz之和(dp[i])。假设当前根节点是u,子节点是v,那么u去掉v子树的贡献为 :res = dp[u]-dp[v]-siz[v];dp[v]就会变成 dp[v]=dp[v]+res+(n-siz[v])

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>

#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a, x) cout<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 400086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

int Head[maxn];
struct edge{
    int v;
    int Next;
}e[maxn];
int cnt=0;
void add(int x,int y){
    e[cnt].v=y;
    e[cnt].Next = Head[x];
    Head[x]=cnt++;
}
ll dp[maxn];
ll siz[maxn];
bool vis[maxn];

int n;
void dfs(int u){
    vis[u]=true;
    for(int k=Head[u];~k;k=e[k].Next){
        if(vis[e[k].v]){ continue;}
        dfs(e[k].v);
        siz[u]+=siz[e[k].v];
        dp[u]+=dp[e[k].v];
    }
    siz[u]++;
    dp[u]+=siz[u];
}

void dfs1(int u){
    vis[u]=true;
    for(int k=Head[u];~k;k=e[k].Next){
        int v = e[k].v;
        if(vis[v]){ continue;}
        ll res = dp[u]-dp[v]-siz[v];
        dp[v]=dp[v]+res-siz[v]+n;
        dfs1(v);
    }
}

int main() {
//    ios::sync_with_stdio(false);
//    freopen("in.txt", "r", stdin);

    memset(Head,-1,sizeof(Head));

    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }

    dfs(1);
    memset(vis,0,sizeof(vis));
    dfs1(1);

    ll ans=0;
    for(int i=1;i<=n;i++){
//        printf("%lld ",siz[i]);
        ans = max(ans,dp[i]);
    }
//    printf("\n");
    printf("%lld\n",ans);
    return 0;
}



原文地址:https://www.cnblogs.com/ZGQblogs/p/11161361.html

时间: 2024-08-30 12:36:13

CodeForce - 1187 E. Tree Painting (换根dp)的相关文章

换根dp「小奇的仓库&#183;randomwalking&#183;」

把以前考试换根题集中写一下 随便选一个点做根一遍$dfs$求子树内贡献,再通过特殊手段算$ans[1]$,最后$dfs$求其他$ans$ 拆成子树内,子树外分别算贡献差,得儿子是很常见套路了 小奇的仓库 $M<=15$ 题解 很久之前做的换根dp,当时觉得真是神仙,现在看还是觉得很神仙 不同于一般换根dp,这个题$n^2$并不好写 所以$n^2$算法就省略了 考虑$M$非常小,可以计算$M$对答案影响 一个直接的想法是先算出来原答案,再减去现在答案 //本来为j现在异或M,变化了j-delta

换根DP

换根dp的通法:1.第一次扫描时,任选一个点为根,在"有根树"上执行一次树形DP,也就在回溯时发生的,自底向上的状态转移. 2.第二次扫描时,从刚才选出的根出发,对整棵树执行一次dfs,在每次递归前进行自上向下的推导,计算出换根后的解. 1.POJ3585 Accumulation Degree dp[i]以i为根的子树中,把i作为源点的最大流量 转移\(dp[x]=\sum_{y\epsilon son(x)}^{}\left\{\begin{matrix} min(dp[y],le

poj3585 Accumulation Degree(换根dp)

传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz const int maxn=2e5+10; int head[maxn],tot; struct node { int nt,to;long long w; }q[2*maxn]; long long dp[maxn];int cnt[maxn]; void insert(int u,int v,long long w) { q[tot].nt=head[u];q[tot].w=w;q[

【换根dp】9.22小偷

换根都不会了 题目大意 给定一棵$n$个点的树和树上一撮关键点,求到所有$m$个关键点距离的最大值$dis_{max}\le LIM$的点的个数. $n,m\le 30000,LIM\le 30000$ 题目分析 考虑在求出一个点的情况下如何转移到其子节点. 对点$u$最直接关心的状态是$mx[u]$:所有关键点到$u$的最大距离. 对点$u$的子节点$v$来说,$u$能带给它的只是“外面的世界”——$v$子树的补集这块贡献,也就是对于$u$的除了$v$子树的$mx[u]$. 因为$mx[u]$

codeforces1156D 0-1-Tree 换根dp

题目传送门 题意: 给定一棵n个点的边权为0或1的树,一条合法的路径(x,y)(x≠y)满足,从x走到y,一旦经过边权为1的边,就不能再经过边权为0的边,求有多少边满足条件? 思路:设$f[u]$为以1为根,自下而上到$u$的末节点是1的合法路径数量,$g[u]$代表以1为根,自下而上到$v$末节点是0的合法路径数量,这个可以通过一遍dfs简单求解. 再设$nf[u]$和$ng[u]$代表以u为根的两种合法路径数量,进行换根dfs,在换根的过程中: 若某一条边是0边,则: $ng[st.to]=

HDU 2196 Computer 二次扫描与换根DP

题意:给定一棵树,求树上所有点到其最远点的距离. 数据范围: 1 <= N <= 100000 ------------------------------------------我是分割线------------------------------------------ 题解:对于每个节点u来说,其可能到达的最长距离为max{其子树内的最长距离,其父节点不经过u的子树内的最长距离}.于是,我们便可以在第一遍dfs中预处理节点x到其子树内的最长距离,顺带求一下次长距离,方便转移. // f[

换根dp+暴力+预处理+记忆化搜索——cf1292C好题!

/** 给定一棵树,要求给树边赋值[0,n-2],每个值只能使用一次 S = mex(u,v), mex(u,v)是u-v路径上没有出现过的编号最小的值 问使得S最大的赋值方式 由于很难直接统计答案,所以考虑统计每条边的贡献 包含(0)路径的贡献tot1是其左右子树size的乘积 包含(0,1)的路径的贡献tot2是其左右子树的size乘积 ...依次类推 显然:只包含(1,2)这样的路径是没有贡献的 那么原问题转化为如何分配[0,n-2],使得最后的乘积和最大 dp[u][v]表示路径(u,v

newcoder 79F 小H和圣诞树 换根 DP + 根号分治

Code: #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #include <map> #define N 100003 #define ll long long #define setIO(s) freopen(s".in", "r" , stdin) , freopen(s".out"

经典换根dp——hdu2196

给定一棵边权树,求距离每个点最远的点,输出这个距离 #include<bits/stdc++.h> using namespace std; #define N 10005 struct Edge{int to,nxt,w;}e[N<<1]; int head[N],tot,n; void init(){memset(head,-1,sizeof head);tot=0;} void add(int u,int v,int w){ e[tot].to=v;e[tot].w=w;e[