#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
const int N = 5e5 + 100;
ve<int> gr[N];
int dep[N];
pii dpd[N];
int dpu[N];
inline void dfs(int v, int p) {
dep[v] = 0;
dpd[v] = {0, 0};
for (auto &to : gr[v]) {
if (to == p) continue;
dfs(to, v);
chmax(dep[v], dep[to] + 1);
chmax(dpd[v].se, dep[to] + 1);
if (dpd[v].se > dpd[v].fi) swap(dpd[v].fi, dpd[v].se);
}
}
inline void dfs_down(int v, int p) {
if (v == p) dpu[v] = 0;
else {
chmax(dpu[v], 1 + dpu[p]);
int len = (dpd[p].fi == dep[v] + 1 ? dpd[p].se : dpd[p].fi);
chmax(dpu[v], 1 + len);
}
for (auto &to : gr[v]) {
if (to == p) continue;
dfs_down(to, v);
}
}
pll answer = {-inf, 0};
inline pll mg(const pll &a, const pll &b) {
if (a.fi > b.fi) return a;
else if (a.fi < b.fi) return b;
else return mpp(a.fi, a.se + b.se);
}
inline void dfs_ans(int v, int p) {
int deg = 0;
for (auto &to : gr[v]) {
if (to == p) continue;
++deg;
dfs_ans(to, v);
}
ve<int> G;
for (auto &to : gr[v]) {
if (to == p) continue;
G.pb(to);
}
sort(all(G), [&](const int &a, const int &b) {
return dep[a] < dep[b];
});
if (deg <= 1) return;
int m = sz(G);
ve<int> pf(m);
for (int i = 0; i < m; ++i) {
pf[i] = dep[G[i]] + 1;
if (i) chmax(pf[i], pf[i - 1]);
}
int cur_mx = 0;
for (int sec = m - 2; ~sec; --sec) {
pll cur = mpp( max(!sec ? 0 : pf[sec - 1], max(dpu[v], cur_mx)) * 1ll * (dep[G[sec]] + dep[G[m - 1]] + 2), 1 );
answer = mg(answer, cur);
chmax(cur_mx, dep[G[sec]] + 1);
}
if (m >= 3) {
int mx = dep[G[m - 2]] + dep[G[m - 3]] + 2;
ll c = mx * 1ll * max(dpu[v], dep[G[m - 1]] + 1);
ll ways = 0;
map<int,int> mp;
for (int i = 0; i < m - 1; ++i) {
ways += mp[mx - (dep[G[i]] + 1)];
++mp[dep[G[i]] + 1];
}
answer = mg(answer, mpp(c, ways));
}
}
inline void solve() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int v, u;
cin >> v >> u, --v, --u;
gr[v].pb(u), gr[u].pb(v);
}
int root = -1;
for (int i = 0; i < n; ++i) {
if (sz(gr[i]) >= 2) {
root = i;
break;
}
}
if (!~root) return void(cout << 0 << " " << 1);
dfs(root, root);
dfs_down(root, root);
dfs_ans(root, root);
cout << answer.fi << " " << answer.se << '\n';
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12052 KB |
Output is correct |
4 |
Correct |
6 ms |
12024 KB |
Output is correct |
5 |
Correct |
8 ms |
12080 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12052 KB |
Output is correct |
4 |
Correct |
6 ms |
12024 KB |
Output is correct |
5 |
Correct |
8 ms |
12080 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12052 KB |
Output is correct |
4 |
Correct |
6 ms |
12024 KB |
Output is correct |
5 |
Correct |
8 ms |
12080 KB |
Output is correct |
6 |
Correct |
7 ms |
12080 KB |
Output is correct |
7 |
Incorrect |
7 ms |
11988 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |