E. Tree Painting(换根dp)

Problem - 1187E - Codeforces

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. 

AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

#define ll long long
const int N = 2e5 + 100, M = 5e5;
int h[N], ne[M], e[M], idx, n;
ll dp[N], cnt[N];

void add(int a, int b) {
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int dfs1(int x, int fa) {
	cnt[x] = 1;
	for (int i = h[x]; ~i; i = ne[i]) {
		int u = e[i];
		if (u == fa) continue;
		cnt[x] += dfs1(u, x);
	}
	dp[1] += cnt[x];
	return cnt[x];
}

void dfs2(int x, int fa) {
	for (int i = h[x]; ~i; i = ne[i]) {
		int u = e[i];
		if (u == fa)continue;
		dp[u] = dp[x] - cnt[u] * 2 + n;
		dfs2(u, x);
	}
}

int main() {
	cin >> n;
	memset(h, -1, sizeof h);
	for (int i = 1; i < n; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		add(u, v), add(v, u);
	}

	dfs1(1, 0);
	dfs2(1, 0);

	ll maxx = 0;

	for (int i = 1; i <= n; i++) maxx = max(dp[i], maxx);

	cout << maxx << endl;
	return 0;
}

总结:这题当时想到大概怎么做,然而居然忘记在dfs之前换根,导致做不出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值