Submission #573092

# Submission time Handle Problem Language Result Execution time Memory
573092 2022-06-05T18:14:07 Z sidon Shymbulak (IZhO14_shymbulak) C++17
100 / 100
113 ms 17832 KB
#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] += sz - i;
			x = comb(x, y);
		}
		x[0] += s[i+sz][0] - i;
		x[1] *= s[i+sz][1];
		ans = comb(ans, x);
	}

	cout << ans[1];
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5016 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5032 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 5016 KB Output is correct
10 Correct 2 ms 5032 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5028 KB Output is correct
5 Correct 4 ms 5292 KB Output is correct
6 Correct 4 ms 5332 KB Output is correct
7 Correct 4 ms 5344 KB Output is correct
8 Correct 6 ms 5332 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 5 ms 5168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 9512 KB Output is correct
2 Correct 38 ms 9812 KB Output is correct
3 Correct 40 ms 16712 KB Output is correct
4 Correct 29 ms 10572 KB Output is correct
5 Correct 31 ms 10408 KB Output is correct
6 Correct 113 ms 16428 KB Output is correct
7 Correct 68 ms 13464 KB Output is correct
8 Correct 62 ms 16040 KB Output is correct
9 Correct 60 ms 15528 KB Output is correct
10 Correct 55 ms 17832 KB Output is correct
11 Correct 71 ms 16160 KB Output is correct
12 Correct 68 ms 16784 KB Output is correct