답안 #1019576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019576 2024-07-11T04:06:48 Z vjudge1 Zagrade (COI17_zagrade) C++14
100 / 100
654 ms 48828 KB
#include <bits/stdc++.h>

#define N 300000

using namespace std;

int a[300005];

vector<int> g[300005];

int sz[300005];

int del[300005];

void pre_dfs(int u, int p) {
	sz[u] = 1;
	
	for (int v: g[u]) {
		if (v != p && del[v] == 0) {
			pre_dfs(v, u);
			sz[u] += sz[v];
		}
	}
}

int cent(int u, int p, int n) {
	for (int v: g[u]) {
		if (v != p && sz[v] > n / 2 && del[v] == 0) {
			return cent(v, u, n);
		}
	}
	
	return u;
}

int cnt[600005];

long long ans = 0;

void dfs(int u, int p, int s, int k, int type) {
	if (type == 0) {
		if (s == min(k, s)) {
			ans += cnt[-s + N];
		}
	} else if (type == 1) {		
		if (min(a[u], a[u] + k) >= 0) {
			cnt[s + N]++;
		}
	} else {
		if (min(a[u], a[u] + k) >= 0) {
			cnt[s + N]--;
		}
	}
	
	for (int v: g[u]) {
		if (v != p && del[v] == 0) {
			if (type == 0) {
				dfs(v, u, s + a[v], min(k, s), type);
			} else {
				dfs(v, u, s + a[v], min(a[u], a[u] + k), type);
			}
		}
	}
}

void upd(int r) {
	
	if (a[r] == 1) {
		cnt[N + 1] = 1; 
	}
	
	for (int v: g[r]) {
		if (del[v] == 0) {
			dfs(v, r, a[v], 0, 0);
			dfs(v, r, a[r] + a[v], a[r], 1);
		}
	}
	
	for (int v: g[r]) {
		if (del[v] == 0) {
			dfs(v, r, a[r] + a[v], a[r], 2);
		}
	}
	
	if (a[r] == 1) {
		cnt[N + 1] = 0;
	}
}

void solve(int u) {
	pre_dfs(u, u);
	
	int r = cent(u, u, sz[u]);
	
	upd(r);
	
	del[r] = 1;
	
	for (int v: g[r]) {
		if (del[v] == 0) {
			solve(v);
		}
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;

	cin >> n;
	
	string s;

	cin >> s;
	
	s = '&' + s;
	
	for (int i = 1; i <= n; ++i) {
		a[i] = (s[i] == '(' ? 1 : -1);
	}
	
	for (int i = 1; i <= n - 1; ++i) {
		int u, v;
		cin >> u >> v;
		
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	
	memset(del, 0, sizeof del);
	
	solve(1);
	
	for (int i = 1; i <= n; ++i) {
		a[i] *= -1;
	}
	
	memset(del, 0, sizeof del);
	
	solve(1);
	
	cout << ans << '\n';
	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8540 KB Output is correct
2 Correct 7 ms 8540 KB Output is correct
3 Correct 7 ms 8540 KB Output is correct
4 Correct 5 ms 8540 KB Output is correct
5 Correct 6 ms 8672 KB Output is correct
6 Correct 5 ms 8540 KB Output is correct
7 Correct 4 ms 8540 KB Output is correct
8 Correct 6 ms 8540 KB Output is correct
9 Correct 5 ms 8792 KB Output is correct
10 Correct 4 ms 8536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 416 ms 43976 KB Output is correct
2 Correct 455 ms 48572 KB Output is correct
3 Correct 432 ms 48384 KB Output is correct
4 Correct 445 ms 48316 KB Output is correct
5 Correct 402 ms 48316 KB Output is correct
6 Correct 468 ms 48316 KB Output is correct
7 Correct 373 ms 48228 KB Output is correct
8 Correct 402 ms 48320 KB Output is correct
9 Correct 373 ms 48320 KB Output is correct
10 Correct 415 ms 48760 KB Output is correct
11 Correct 373 ms 48316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8540 KB Output is correct
2 Correct 7 ms 8540 KB Output is correct
3 Correct 7 ms 8540 KB Output is correct
4 Correct 5 ms 8540 KB Output is correct
5 Correct 6 ms 8672 KB Output is correct
6 Correct 5 ms 8540 KB Output is correct
7 Correct 4 ms 8540 KB Output is correct
8 Correct 6 ms 8540 KB Output is correct
9 Correct 5 ms 8792 KB Output is correct
10 Correct 4 ms 8536 KB Output is correct
11 Correct 416 ms 43976 KB Output is correct
12 Correct 455 ms 48572 KB Output is correct
13 Correct 432 ms 48384 KB Output is correct
14 Correct 445 ms 48316 KB Output is correct
15 Correct 402 ms 48316 KB Output is correct
16 Correct 468 ms 48316 KB Output is correct
17 Correct 373 ms 48228 KB Output is correct
18 Correct 402 ms 48320 KB Output is correct
19 Correct 373 ms 48320 KB Output is correct
20 Correct 415 ms 48760 KB Output is correct
21 Correct 373 ms 48316 KB Output is correct
22 Correct 629 ms 25024 KB Output is correct
23 Correct 599 ms 25020 KB Output is correct
24 Correct 654 ms 25020 KB Output is correct
25 Correct 639 ms 25020 KB Output is correct
26 Correct 497 ms 32696 KB Output is correct
27 Correct 476 ms 29116 KB Output is correct
28 Correct 466 ms 27832 KB Output is correct
29 Correct 392 ms 48824 KB Output is correct
30 Correct 344 ms 48828 KB Output is correct
31 Correct 87 ms 24496 KB Output is correct
32 Correct 346 ms 48316 KB Output is correct