题意:
给出一棵树,选一个节点作为根,使得所有子树的和最大,输出最大值。
思路:
换根$DP$。
代码:
1 //#include<bits/stdc++.h> 2 #include <set> 3 #include <map> 4 #include <stack> 5 #include <cmath> 6 #include <queue> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstring> 11 #include <iostream> 12 #include <algorithm> 13 14 #define ll long long 15 #define pll pair<ll,ll> 16 #define pii pair<int,int> 17 #define bug printf("********* ") 18 #define FIN freopen("input.txt","r",stdin); 19 #define FON freopen("output.txt","w+",stdout); 20 #define IO ios::sync_with_stdio(false),cin.tie(0) 21 #define ls root<<1 22 #define rs root<<1|1 23 #define pb push_back 24 25 using namespace std; 26 const int inf = 2e9 + 7; 27 const ll Inf = 1e18 + 7; 28 const int maxn = 2e5 + 5; 29 const int mod = 1e9 + 7; 30 31 int n; 32 vector<int>vec[maxn]; 33 int son[maxn]; 34 ll sum = 0, ans = 0;; 35 36 void dfs1(int x, int fa) 37 { 38 son[x] = 1; 39 for (int i = 0, len = vec[x].size(); i < len; ++i) 40 { 41 int v = vec[x][i]; 42 if (v == fa) continue; 43 dfs1(v, x); 44 son[x] += son[v]; 45 } 46 sum += son[x]; 47 ans = max(ans, sum); 48 } 49 50 void dfs(int x, int fa, ll res) 51 { 52 for (int i = 0, len = vec[x].size(); i < len; ++i) 53 { 54 int v = vec[x][i]; 55 if (v == fa) continue; 56 ll tmp = res - son[v] + n - son[v]; 57 ans = max(ans, tmp); 58 dfs(v, x, tmp); 59 } 60 } 61 62 int main() 63 { 64 scanf("%d", &n); 65 for (int i = 1; i < n; ++i) 66 { 67 int u, v; 68 scanf("%d %d", &u, &v); 69 vec[u].push_back(v); 70 vec[v].push_back(u); 71 } 72 dfs1(1, 0); 73 dfs(1, 0, sum); 74 printf("%lld ", ans); 75 }