-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathreroot.cpp
More file actions
80 lines (76 loc) · 1.79 KB
/
reroot.cpp
File metadata and controls
80 lines (76 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 200001
#define mod 1000000007
int n;
vector<int> adj[MAXN];
int sz[MAXN];
int dp[MAXN];
int dfs(int u, int v)
{
sz[u] = 1;
for (auto const &i : adj[u])
if (i != v)
sz[u] += dfs(i, u);
return sz[u];
}
void reroot(int u, int v)
{
for (auto const &i : adj[u])
{
if (i != v)
{
int a = sz[u], b = sz[i];
dp[i] = dp[u];
dp[i] -= sz[u], dp[i] -= sz[i];
sz[u] -= sz[i], sz[i] = n;
dp[i] += sz[u], dp[i] += sz[i];
reroot(i, u);
sz[u] = a, sz[i] = b;
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(0, -1);
for (int i = 0; i < n; i++)
dp[0] += sz[i]; // answer when tree is rooted on vertex 0
reroot(0, -1);
cout << *max_element(dp, dp + n) << endl;
return 0;
}
// https://codeforces.com/contest/1187/problem/E
// f(v) = when tree is rooted at vertex v, the current
// answer is the sum of all subtrees sizes
// final answer = max(f(0), f(1), f(2), ..., f(n))
// easy approach: O(N^2)
// with reroot: O(N)
// 1 - run a dfs and calculate f(0)
// 2 - let be dp[i] = f(i)
// 3 - now, lets run a another dfs, and re-calculate the
// answer when tree is rooted at vertex i (dp[i])
// 4 - the final answer is the maximum value of dp[i]