Submission #232888

# Submission time Handle Problem Language Result Execution time Memory
232888 2020-05-18T14:51:25 Z Saboon Zagrade (COI17_zagrade) C++14
100 / 100
964 ms 37476 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<long double> point;

const int maxn = 3e5 + 10;

string s;
vector<int> t[maxn];
int sz[maxn];
bool mark[maxn];
ll answer = 0, ans2;
int mxm = 0;
int cnt[maxn];

void dfs2(int v, int mnm, int sum, int par = -1){
	if (s[v] == '(')
		sum ++;
	else
		sum --;
	mnm = min(mnm, sum);
	if (mnm == sum){
		cnt[-mnm] ++;
		mxm = max(mxm, -mnm);
		if (mnm == 0)
			ans2 ++;
	}
	for (auto u : t[v])
		if (!mark[u] and u != par)
			dfs2(u, mnm, sum, v);
}

void dfs1(int v, int need, int sum, int par = -1){
	if (s[v] == '('){
		need = max(need - 1, 0);
		sum ++;
	}
	else{
		need ++;
		sum --;
	}
	if (need == 0)
		answer += cnt[sum];
	for (auto u : t[v])
		if (!mark[u] and u != par)
			dfs1(u, need, sum, v);
}

int dfssz(int v, int par = -1){
	sz[v] = 1;
	for (auto u : t[v])
		if (!mark[u] and u != par)
			sz[v] += dfssz(u, v);
	return sz[v];
}

void centroidDecomposition(int v){
	int Max = dfssz(v), par = -1;
	bool found = 1;
	while (found){
		found = 0;
		for (auto u : t[v])
			if (!mark[u] and u != par and 2*sz[u] >= Max){
				par = v, v = u, found = 1;
				break;
			}
	}
	mxm = 1;
	if (s[v] == ')')
		cnt[1] ++;
	for (auto u : t[v]){
		if (mark[u])
			continue;
		dfs1(u, 0, 0, v);
		int mnm = 0, sum = 0;
		if (s[v] == '(')
			sum ++;
		else
			mnm = sum = -1;
		dfs2(u, mnm, sum, v);
	}
	cnt[0] = 0;
	for (int i = 1; i <= mxm; i++)
		cnt[i] = 0;
	reverse(t[v].begin(), t[v].end());
	mxm = 1;
	for (auto u : t[v]){
		if (mark[u])
			continue;
		dfs1(u, 0, 0, v);
		int mnm = 0, sum = 0;
		if (s[v] == '(')
			sum ++;
		else
			mnm = sum = -1;
		dfs2(u, mnm, sum, v);
	}
	cnt[0] = 0;
	for (int i = 1; i <= mxm; i++)
		cnt[i] = 0;
	mark[v] = 1;
	for (auto u : t[v])
		if (!mark[u])
			centroidDecomposition(u);
}

int main(){
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	cin >> s;
	s = "#" + s;
	for (int i = 1; i <= n-1; i++){
		int v, u;
		cin >> v >> u;
		t[v].push_back(u);
		t[u].push_back(v);
	}
	centroidDecomposition(1);
	cout << answer + ans2/2 << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7424 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 9 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 10 ms 7424 KB Output is correct
7 Correct 12 ms 7424 KB Output is correct
8 Correct 9 ms 7424 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 32792 KB Output is correct
2 Correct 461 ms 37184 KB Output is correct
3 Correct 473 ms 36832 KB Output is correct
4 Correct 458 ms 36960 KB Output is correct
5 Correct 468 ms 36832 KB Output is correct
6 Correct 462 ms 36920 KB Output is correct
7 Correct 470 ms 36832 KB Output is correct
8 Correct 462 ms 36960 KB Output is correct
9 Correct 473 ms 36836 KB Output is correct
10 Correct 387 ms 37476 KB Output is correct
11 Correct 386 ms 36836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 7424 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 9 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 10 ms 7424 KB Output is correct
7 Correct 12 ms 7424 KB Output is correct
8 Correct 9 ms 7424 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 467 ms 32792 KB Output is correct
12 Correct 461 ms 37184 KB Output is correct
13 Correct 473 ms 36832 KB Output is correct
14 Correct 458 ms 36960 KB Output is correct
15 Correct 468 ms 36832 KB Output is correct
16 Correct 462 ms 36920 KB Output is correct
17 Correct 470 ms 36832 KB Output is correct
18 Correct 462 ms 36960 KB Output is correct
19 Correct 473 ms 36836 KB Output is correct
20 Correct 387 ms 37476 KB Output is correct
21 Correct 386 ms 36836 KB Output is correct
22 Correct 964 ms 22968 KB Output is correct
23 Correct 933 ms 22880 KB Output is correct
24 Correct 897 ms 23012 KB Output is correct
25 Correct 925 ms 23132 KB Output is correct
26 Correct 586 ms 27692 KB Output is correct
27 Correct 583 ms 25440 KB Output is correct
28 Correct 594 ms 24548 KB Output is correct
29 Correct 388 ms 37476 KB Output is correct
30 Correct 380 ms 37476 KB Output is correct
31 Correct 106 ms 22488 KB Output is correct
32 Correct 384 ms 36960 KB Output is correct