#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];
});
int m = sz(G);
for (int i = 0; i < m; ++i) {
for (int j = i + 1; j < m; ++j) {
int d = dpu[v];
for (int z = 0; z < m; ++z) {
if (z == i || z == j) continue;
chmax(d, dep[G[z]] + 1);
}
pll cur = mpp( d * 1ll * (dep[G[i]] + 1 + dep[G[j]] + 1), 1 );
answer = mg(answer, cur);
}
}
}
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 |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Incorrect |
7 ms |
12096 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Incorrect |
7 ms |
12096 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Incorrect |
7 ms |
12096 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |