답안 #864538

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
864538 2023-10-23T07:34:51 Z serifefedartar Zagrade (COI17_zagrade) C++17
100 / 100
776 ms 50388 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20; 
const ll MAXN = 3e5 + 100;

vector<vector<int>> graph;
int marked[MAXN], sz[MAXN], ch[MAXN];
map<int,int> cnt;
ll ans = 0;
int get_sz(int node, int parent) {
	sz[node] = 1;
	for (auto u : graph[node]) {
		if (u == parent || marked[u])
			continue;
		sz[node] += get_sz(u, node);
	}
	return sz[node];
}

int find_centro(int node, int parent, int n) {
	for (auto u : graph[node]) {
		if (u != parent && !marked[u] && sz[u] * 2 >= n)
			return find_centro(u, node, n);
	}
	return node;
}

void dfs(int node, int parent, int sum, int mx, int mn) {
	sum += ch[node];
	mx = max(mx, sum);
	mn = min(mn, sum);

	/*
		( ) ) ) (
		1 0 -1 -2 -1
		sum != mn olduğunda sağlamıyor.
	*/

	if (sum == mn)
		ans += cnt[-mn];

	for (auto u : graph[node]) {
		if (u == parent || marked[u])
			continue;
		dfs(u, node, sum, mx, mn);
	}
}

void add(int node, int parent, int sum, int mx, int mn) {
	sum += ch[node];
	mx = max(mx, sum);
	mn = min(mn, sum);

	if (sum == mx)
		cnt[sum]++; 

	/*
		( ) ) ) ( (
		1 0 -1 -2 -1 0
		sum = 0
		mx = 1
	*/

	for (auto u : graph[node]) {
		if (u == parent || marked[u])
			continue;
		add(u, node, sum, mx, mn);
	}
}

void solve(int node) {
	int n = get_sz(node, node);
	int centro = find_centro(node, node, n);

	cnt.clear();
	marked[centro] = true;
	if (ch[centro] == 1)
		cnt[1]++;

	for (auto u : graph[centro]) {
		if (marked[u])
			continue;
		dfs(u, centro, 0, 0, 0);
		add(u, centro, ch[centro], max(0, ch[centro]), min(0, ch[centro]));
	}

	ans += cnt[0];

	cnt.clear();
	reverse(graph[centro].begin(), graph[centro].end());
	for (auto u : graph[centro]) {
		if (marked[u])
			continue;
		dfs(u, centro, 0, 0, 0);
		add(u, centro, ch[centro], max(0, ch[centro]), min(0, ch[centro]));
	}

	for (auto u : graph[centro]) {
		if (!marked[u])
			solve(u);
	}
}

int main() {
	fast
	int n, a, b;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		char c; cin >> c;
		ch[i] = (c == '(' ? 1 : -1);
	}

	graph = vector<vector<int>>(n+1, vector<int>());
	for (int i = 1; i < n; i++) {
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	solve(1);
	cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 384 ms 39112 KB Output is correct
2 Correct 570 ms 46160 KB Output is correct
3 Correct 391 ms 43344 KB Output is correct
4 Correct 524 ms 45652 KB Output is correct
5 Correct 374 ms 43296 KB Output is correct
6 Correct 450 ms 44372 KB Output is correct
7 Correct 391 ms 43348 KB Output is correct
8 Correct 475 ms 44240 KB Output is correct
9 Correct 372 ms 43340 KB Output is correct
10 Correct 748 ms 50260 KB Output is correct
11 Correct 307 ms 43164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 384 ms 39112 KB Output is correct
12 Correct 570 ms 46160 KB Output is correct
13 Correct 391 ms 43344 KB Output is correct
14 Correct 524 ms 45652 KB Output is correct
15 Correct 374 ms 43296 KB Output is correct
16 Correct 450 ms 44372 KB Output is correct
17 Correct 391 ms 43348 KB Output is correct
18 Correct 475 ms 44240 KB Output is correct
19 Correct 372 ms 43340 KB Output is correct
20 Correct 748 ms 50260 KB Output is correct
21 Correct 307 ms 43164 KB Output is correct
22 Correct 516 ms 24632 KB Output is correct
23 Correct 498 ms 24660 KB Output is correct
24 Correct 532 ms 24636 KB Output is correct
25 Correct 566 ms 24636 KB Output is correct
26 Correct 450 ms 30784 KB Output is correct
27 Correct 456 ms 27988 KB Output is correct
28 Correct 453 ms 26992 KB Output is correct
29 Correct 761 ms 50308 KB Output is correct
30 Correct 776 ms 50388 KB Output is correct
31 Correct 76 ms 24200 KB Output is correct
32 Correct 347 ms 43292 KB Output is correct