Submission #232340

# Submission time Handle Problem Language Result Execution time Memory
232340 2020-05-16T19:08:01 Z amoo_safar Zagrade (COI17_zagrade) C++14
100 / 100
663 ms 42224 KB
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 3e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int n, a[N], mk[N];
vector<int> G[N];
ll ans = 0;

int sz[N];
int DFS(int u, int p){
	sz[u] = 1;
	for(auto adj : G[u])
		if(adj != p && !mk[adj])
			sz[u] += DFS(adj, u);
	return sz[u];
}
vector<int> V2, V3;
void DFS2(int u, int p, int sm, int mn){
	sm += a[u]; mn = min(mn, sm);
	if(sm == mn){
		if(mn == 0) ans ++;
		V2.pb(mn);
	}
	for(auto adj : G[u])
		if(adj != p && !mk[adj])
			DFS2(adj, u, sm, mn);
}
int tr;
void DFS3(int u, int p, int sm, int mn){
	sm -= a[u]; mn = min(mn, sm);
	if(sm == mn){
		if(tr && mn == -1) ans ++;
		V3.pb(mn);
	}
	for(auto adj : G[u])
		if(adj != p && !mk[adj])
			DFS3(adj, u, sm, mn);
}

int cnt2[N], cnt3[N];
void Centroid(int u){
	int m = DFS(u, -1);
	int fl = 1;
	int cen = u;
	while(fl){
		fl = 0;
		for(auto adj : G[cen]){
			if(!mk[adj] && sz[adj] < sz[cen] && sz[adj] + sz[adj] >= m){
				cen = adj;
				fl = 1;
				break;
			}
		}
	}
	//debug(cen);
	for(int i = 0; i <= m; i++) cnt2[i] = 0, cnt3[i] = 0;
	mk[cen] = 1;
	for(auto adj : G[cen]){
		if(mk[adj]) continue;
		V2.clear(); V3.clear();
		DFS2(adj, cen, a[cen], min(a[cen], 0));
		if(a[cen] == -1) tr = 1;
		else tr = 0;
		DFS3(adj, cen, 0, 0);
		for(auto &x : V2) ans += cnt3[-x];
		for(auto &x : V3) ans += cnt2[-x];
		
		for(auto &x : V2) cnt2[-x] ++;
		for(auto &x : V3) cnt3[-x] ++;
	}
	//debug(ans);
	for(auto adj : G[cen]){
		if(mk[adj]) continue;
		Centroid(adj);
	}

	return ;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	char c;
	for(int i = 1; i <= n; i++){
		cin >> c;
		a[i] = (c == '(' ? 1 : -1);
	}
	int u, v;
	for(int i = 1; i < n; i++){
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
	}
	Centroid(1);
	cout << ans << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 9 ms 7552 KB Output is correct
3 Correct 9 ms 7552 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 9 ms 7424 KB Output is correct
7 Correct 9 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 316 ms 40928 KB Output is correct
2 Correct 331 ms 41204 KB Output is correct
3 Correct 313 ms 40824 KB Output is correct
4 Correct 334 ms 41092 KB Output is correct
5 Correct 312 ms 40824 KB Output is correct
6 Correct 327 ms 41080 KB Output is correct
7 Correct 323 ms 40696 KB Output is correct
8 Correct 324 ms 40952 KB Output is correct
9 Correct 312 ms 40952 KB Output is correct
10 Correct 311 ms 42092 KB Output is correct
11 Correct 325 ms 41588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 9 ms 7552 KB Output is correct
3 Correct 9 ms 7552 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 9 ms 7424 KB Output is correct
7 Correct 9 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 316 ms 40928 KB Output is correct
12 Correct 331 ms 41204 KB Output is correct
13 Correct 313 ms 40824 KB Output is correct
14 Correct 334 ms 41092 KB Output is correct
15 Correct 312 ms 40824 KB Output is correct
16 Correct 327 ms 41080 KB Output is correct
17 Correct 323 ms 40696 KB Output is correct
18 Correct 324 ms 40952 KB Output is correct
19 Correct 312 ms 40952 KB Output is correct
20 Correct 311 ms 42092 KB Output is correct
21 Correct 325 ms 41588 KB Output is correct
22 Correct 654 ms 27464 KB Output is correct
23 Correct 652 ms 27504 KB Output is correct
24 Correct 644 ms 27640 KB Output is correct
25 Correct 663 ms 27496 KB Output is correct
26 Correct 387 ms 31608 KB Output is correct
27 Correct 387 ms 29688 KB Output is correct
28 Correct 395 ms 28792 KB Output is correct
29 Correct 318 ms 42224 KB Output is correct
30 Correct 308 ms 42224 KB Output is correct
31 Correct 103 ms 27372 KB Output is correct
32 Correct 319 ms 41712 KB Output is correct