이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int, int>
#define PB push_back
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 5100;
const int oo = 2e9;
vector<int> g[N];
int n, ans = -1, kol = 0, mx[N];
void build_sz(int v, int p){
mx[v] = 0;
for (int u : g[v]){
if (u == p) continue;
build_sz(u, v);
mx[v] = max(mx[v], mx[u] + 1);
}
}
void dfs(int v, int p, int len, int cmax){
if (sz(g[v]) == 1 && p >= 0){
int clen = len * cmax;
if (ans < clen){
ans = clen;
kol = 0;
}
if (ans == clen)
kol++;
return;
}
int mx1 = -1, mx2 = -1, n1 = -1, n2 = -1;
for (int u : g[v]){
if (u == p) continue;
if (mx1 < mx[u] + 1){
swap(mx1, mx2);
swap(n1, n2);
mx1 = mx[u] + 1;
n1 = u;
} else if (mx2 < mx[u] + 1){
mx2 = mx[u] + 1;
n2 = u;
}
}
for (int u : g[v]){
if (u == p) continue;
if (n1 == u)
dfs(u, v, len + 1, max(cmax, mx2));
else dfs(u, v, len + 1, max(cmax, mx1));
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++){
int x, y; cin >> x >> y;
x--; y--;
g[x].PB(y);
g[y].PB(x);
}
for (int i = 0; i < n; i++)
if (sz(g[i]) == 1){
build_sz(i, -1);
dfs(i, -1, 0, 0);
}
cout << ans << " " << (kol >> 1);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |