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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #define others #ifdef poj #include <iostream> #include <cstring> #include <cmath> #include <cstdio> #include <algorithm> #include <vector> #include <string> #endif #ifdef others #include <bits/stdc++.h> #endif
#define all(x) x.begin(), x.end() using namespace std; const double eps = 1e-8; int dcmp(double x) { if (fabs(x) <= eps) return 0; return (x > 0) ? 1 : -1; }; typedef long long LL; void umax(LL &a, LL b) { a = max(a, b); } void umin(LL &a, LL b) { a = min(a, b); } #ifdef backup cccc #endif
void file() { freopen("out.txt", "r", stdin); freopen("1.txt", "w", stdout); }
namespace solver { const int maxn = 10001; int n; vector<int> G[maxn]; int sz[maxn]; LL maxv = 0, id = 0; void dfs(int u, int fa) { sz[u] = 1; vector<int> V; int sum = 0; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (v == fa) continue; dfs(v, u); V.push_back(sz[v]); sum += sz[v]; sz[u] += sz[v]; } int ff = n - sum; V.push_back(ff); LL res = 0; LL ss = 0; for (int i = 0; i < V.size(); i++) { res += ss * V[i]; ss += V[i]; } if (res > maxv) { maxv = res; id = u; } } void dfs2(int u, int fa) { sz[u] = 1; int sum = 0; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (v == fa) continue; dfs2(v, u); sz[u] += sz[v]; } } void solve() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(0, -1); dfs2(id, -1); LL ss = 0; vector<LL> V; for (int i = 0; i < G[id].size(); i++) V.push_back(sz[G[id][i]]); if (V.size() == 1) { cout << 0 << " " << 0 << endl; } else { sort(all(V)); V[V.size() - 2] += V[V.size() - 1]; LL ss = 0, res = 0; for (int i = 0; i < V.size() - 1; i++) { res += ss * V[i]; ss += V[i]; } cout << maxv << " " << res << endl; } } }
int main() { solver::solve(); return 0; }
|