이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod = (ll)1e9 + 7;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
void solve() {
ll n;
cin >> n;
vector<vector<ll>> g(n);
for (int i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
g[a].pb(b);
g[b].pb(a);
}
vector<ll> cycle;
vector<bool> on_cyc(n), used(n);
function<ll(ll, ll)> dfs = [&](ll v, ll pr) {
used[v] = true;
for (auto u : g[v]) {
if (u == pr) {
continue;
}
if (used[u]) {
cycle.pb(v);
on_cyc[v] = true;
return u;
}
ll res = dfs(u, v);
if (res == -1) {
continue;
}
if (res == -2) {
return -2LL;
}
cycle.pb(v);
on_cyc[v] = true;
if (res == v) {
res = -2;
}
return res;
}
return -1LL;
};
dfs(0, -1);
vector<ll> cnt(n), diam(n + 1), h(n + 1), max_h_val(n);
function<void(ll, ll)> dfs1 = [&](ll v, ll pr) {
h[v] = 0;
ll mx1 = 0, cnt1 = 0, mx2 = 0, cnt2 = 0;
for (auto u : g[v]) {
if (u == pr or on_cyc[u]) {
continue;
}
dfs1(u, v);
h[v] = max(h[v], h[u] + 1);
if (mx1 < h[u] + 1) {
mx2 = mx1;
cnt2 = cnt1;
cnt1 = 1;
mx1 = h[u] + 1;
} else if (mx1 == h[u] + 1) {
cnt1++;
} else if (mx2 < h[u] + 1) {
mx2 = h[u] + 1;
cnt2 = 1;
} else if (mx2 == h[u] + 1) {
cnt2++;
}
}
if (cnt1 > 1) {
diam[v] = mx1 * 2;
cnt[v] = cnt1 * (cnt1 - 1) / 2;
} else {
diam[v] = mx1 + mx2;
cnt[v] = max(cnt2, 1LL);
}
max_h_val[v] = max(1LL, cnt1);
};
for (auto v : cycle) {
dfs1(v, -1);
}
map<ll, ll> pph;
ll len = cycle.size();
for (int i = 1; i <= len / 2; i++) {
pph[-(h[cycle[i]] + i)] += max_h_val[cycle[i]];
}
vector<ll> answer(n + 1);
for (int i = 0; i < len; i++) {
auto value = *pph.begin();
answer[-value.first - i + h[cycle[i]]] += value.second * max_h_val[cycle[i]];
pph[-(h[cycle[(i + 1) % len]] + i + 1)] -= max_h_val[cycle[(i + 1) % len]];
pph[-(h[cycle[(i + 1 + len / 2) % len]] + i + 1 + len / 2)] += max_h_val[cycle[(i + 1 + len / 2) % len]];
}
for (int i = 0; i < n; i++) {
answer[diam[i]] += cnt[i];
}
ll i = n;
while (answer[i] == 0) {
i--;
}
cout << answer[i];
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
cerr << "Seed: " << seed << "\n";
srand(seed);
solve();
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... |