답안 #370194

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
370194 2021-02-23T14:51:15 Z soroush Zagrade (COI17_zagrade) C++11
100 / 100
1423 ms 43904 KB
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 3e5 + 10;
const ll mod = 1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}


int n , op[maxn] , sz[maxn] , cnt[maxn];
vector < int > adj[maxn];
string s;
bool hide[maxn];
ll ans = 0;

void plant(int v ,int p = 0){
	sz[v] = 1;
	for(auto u : adj[v])if(!hide[u] and u^p)
		plant(u , v) , sz[v] += sz[u];
}

int cen(int v , int n , int par = 0 , bool found = 0){
	while(!found){
		found = 1;
		for(auto u : adj[v])if(u ^ par and !hide[u] and sz[u] * 2 > n){
			par = v , v = u , found = 0;
			break;
		}
	}
	return(v);
}

void add(int v , int h , int mx){
	h += op[v];
	if(h >= mx)cnt[h] ++ , mx = h;
	hide[v] = 1;
	for(auto u : adj[v])if(!hide[u])
		add(u , h , mx);
	hide[v] = 0;
}

void rm(int v , int h , int mx){
	h += op[v];
	if(h >= mx)cnt[h] -- , mx = h;
	hide[v] = 1;
	for(auto u : adj[v])if(!hide[u])
		rm(u , h , mx);
	hide[v] = 0;
}

ll chk(int v , int h = 0 , int mn = 0){
	h += op[v];
	ll ans = 0;
	if(h <= mn)ans += cnt[-h] , mn = h;
	hide[v] = 1;
	for(auto u : adj[v])if(!hide[u])
		ans += chk(u , h , mn);
	hide[v] = 0;
	return ans;
}

ll solve(int v){
	ll ans = 0;
	if(op[v] == 1){
		cnt[1] ++;
		for(auto u : adj[v])if(!hide[u])
			add(u , 1 , 1);
		for(auto u : adj[v])if(!hide[u])
			rm(u , 1 , 1), ans += chk(u) , add(u , 1 , 1);
		for(auto u : adj[v])if(!hide[u])
			rm(u , 1 , 1);
		cnt[1] --;
	}
	else{
		for(auto u : adj[v])if(!hide[u])
			add(u , -1 , 0);
		ans += cnt[0];
		for(auto u : adj[v])if(!hide[u])
			rm(u , -1 , 0), ans += chk(u) , add(u , -1 , 0);
		for(auto u : adj[v])if(!hide[u])
			rm(u , -1 , 0);
	}
	return(ans);
}

void decomp(int v){
	plant(v);
	v = cen(v , sz[v]);
	hide[v] = 1;
	ans += solve(v);
	for(auto u : adj[v])
		if(!hide[u])decomp(u);
}

int32_t main(){
	migmig;
	cin >> n >> s;
	for(int i = 1 ; i <= n ; i ++)op[i] = (s[i - 1] == '(' ? 1 : -1);
	for(int i = 1 ; i <  n ; i ++){
		int u , v;
		cin >> u >> v;
		adj[u].pb(v) , adj[v].pb(u);
	}
	decomp(1);
	cout << ans;
	return(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 7 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 6 ms 7404 KB Output is correct
8 Correct 7 ms 7404 KB Output is correct
9 Correct 6 ms 7404 KB Output is correct
10 Correct 7 ms 7532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 506 ms 43136 KB Output is correct
2 Correct 481 ms 43284 KB Output is correct
3 Correct 522 ms 43136 KB Output is correct
4 Correct 486 ms 43136 KB Output is correct
5 Correct 501 ms 43136 KB Output is correct
6 Correct 529 ms 43392 KB Output is correct
7 Correct 499 ms 43296 KB Output is correct
8 Correct 522 ms 43228 KB Output is correct
9 Correct 488 ms 43136 KB Output is correct
10 Correct 483 ms 43776 KB Output is correct
11 Correct 524 ms 43264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 7 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 6 ms 7404 KB Output is correct
8 Correct 7 ms 7404 KB Output is correct
9 Correct 6 ms 7404 KB Output is correct
10 Correct 7 ms 7532 KB Output is correct
11 Correct 506 ms 43136 KB Output is correct
12 Correct 481 ms 43284 KB Output is correct
13 Correct 522 ms 43136 KB Output is correct
14 Correct 486 ms 43136 KB Output is correct
15 Correct 501 ms 43136 KB Output is correct
16 Correct 529 ms 43392 KB Output is correct
17 Correct 499 ms 43296 KB Output is correct
18 Correct 522 ms 43228 KB Output is correct
19 Correct 488 ms 43136 KB Output is correct
20 Correct 483 ms 43776 KB Output is correct
21 Correct 524 ms 43264 KB Output is correct
22 Correct 1412 ms 20096 KB Output is correct
23 Correct 1321 ms 20096 KB Output is correct
24 Correct 1361 ms 20096 KB Output is correct
25 Correct 1423 ms 20096 KB Output is correct
26 Correct 615 ms 27596 KB Output is correct
27 Correct 618 ms 24064 KB Output is correct
28 Correct 645 ms 22800 KB Output is correct
29 Correct 479 ms 43776 KB Output is correct
30 Correct 489 ms 43904 KB Output is correct
31 Correct 105 ms 20856 KB Output is correct
32 Correct 481 ms 43136 KB Output is correct