#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();
ll upd_len = -value.first - i + h[cycle[i]];
answer[upd_len] += value.second * max_h_val[cycle[i]];
assert(value.second != 0);
pph[-(h[cycle[(i + 1) % len]] + i + 1)] -= max_h_val[cycle[(i + 1) % len]];
if (pph[-(h[cycle[(i + 1) % len]] + i + 1)] == 0) {
pph.erase(-(h[cycle[(i + 1) % len]] + i + 1));
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
87 ms |
10488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |