#include <bits/stdc++.h>
#define int long long
using namespace std;
using ii = array<int, 2>;
const int Z = 2e5;
int N, sz, vis[Z];
ii ans, cur, s[2*Z];
bool isR[Z];
vector<int> g[Z], st, h;
void dfs(int u, int p) {
vis[u] = 2;
st.push_back(u);
for(int v : g[u]) if(v != p) {
if(!empty(h)) return;
if(vis[v] > 1) {
while(st.back() != v) {
h.push_back(st.back());
st.pop_back();
}
h.push_back(v);
} else if(!vis[v]) dfs(v, u);
}
if(!empty(st)) st.pop_back();
vis[u] = 1;
}
ii comb(array<int, 2> x, array<int, 2> y) {
if(x < y) swap(x, y);
if(x[0] == y[0]) x[1] = x[1] + y[1];
return x;
}
ii calc(int u, int p) {
ii x = {0, 1}, y;
for(int v : g[u]) if(v != p && !isR[v]) {
y = calc(v, u);
++y[0];
ans = comb(ans, {x[0] + y[0], x[1] * y[1]});
x = comb(x, y);
}
return x;
}
ii query(int l, int r) {
ii x {-N};
for(l += sz, r += sz + 1; l < r; l /= 2, r /= 2) {
if(l & 1) x = comb(x, s[l++]);
if(r & 1) x = comb(x, s[--r]);
}
return x;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N;
for(int i = N; i--; ) {
int u, v; cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, 0);
sz = size(h);
for(int u : h) isR[u] = 1;
for(int i = sz; i--; ) {
s[i+sz] = calc(h[i], -1);
s[i+sz][0] += i;
}
for(int i = sz; --i; )
s[i] = comb(s[2*i], s[2*i+1]);
for(int i = sz; i--; ) {
int j = i + (sz / 2);
ii x = query(i+1, min(sz-1, j));
x[0] -= i;
if(j >= sz) {
ii y = query(0, j - sz);
y[0] += 1;
x = comb(x, y);
}
x[0] += s[i+sz][0] - i;
x[1] *= s[i+sz][1];
ans = comb(ans, x);
}
cout << ans[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
10568 KB |
Output is correct |
2 |
Correct |
64 ms |
10980 KB |
Output is correct |
3 |
Incorrect |
45 ms |
17892 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |