이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
using namespace std;
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;
const int N = (int)1e5 + 7;
const int M = (int)7e6 + 7;
const ll MOD = (ll)1e9;
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7;
pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
int n, p[N], d[N], D[N];
vector<int> g[N];
void dfs(int v, int pr) {
p[v] = pr;
for(auto to : g[v]) {
if(to != pr) {
d[to] = d[v] + 1;
dfs(to, v);
}
}
}
void DFS(int v, int pr) {
for(auto to : g[v]) {
if(to != pr) {
D[to] = D[v] + 1;
DFS(to, v);
}
}
}
void solve() {
cin >> n;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
int mx = 0, cnt = 0;
vector<int> v;
for(int i = 1; i <= n; ++i) {
if(sz(g[i]) == 1) v.pb(i);
}
for(int i = 0; i < sz(v); ++i) {
for(int j = i + 1; j < sz(v); ++j) {
vector<int> pth;
int a = v[i], b = v[j];
d[a] = 0;
dfs(a, a);
int h = 0;
int x = b;
while(x != a) {
pth.pb(x);
x = p[x];
}
pth.pb(a);
fill(D + 1, D + 1 + n, 0);
for(auto x : v) {
if(x != a && x != b) {
D[x] = 0;
DFS(x, x);
int cur = inf;
for(auto e : pth) cur = min(cur, D[e]);
h = max(h, cur);
}
}
mx = max(mx, d[b] * h);
}
}
for(int i = 0; i < sz(v); ++i) {
for(int j = i + 1; j < sz(v); ++j) {
vector<int> pth;
int a = v[i], b = v[j];
d[a] = 0;
dfs(a, a);
int h = 0;
int x = b;
while(x != a) {
pth.pb(x);
x = p[x];
}
pth.pb(a);
fill(D + 1, D + 1 + n, 0);
for(auto x : v) {
if(x != a && x != b) {
D[x] = 0;
DFS(x, x);
int cur = inf;
for(auto e : pth) cur = min(cur, D[e]);
h = max(h, cur);
}
}
if(d[b] * h == mx) cnt++;
}
}
cout << mx << ' ' << cnt << nl;
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
/*
3
3 1
2 3
1 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |