답안 #26290

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26290 2017-06-29T05:23:13 Z 김현수(#1102) Zagrade (COI17_zagrade) C++11
100 / 100
1613 ms 58728 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 300005;

ll n, tot, chi[N], a[N], ans;
bool blk[N];
char ipt[N];

vector<ll> adj[N];
map<ll, ll> pl, mi;

void calc (ll C, ll P) {
	tot++;
	chi[C] = 1;
	for(auto &T : adj[C]) {
		if(T == P || blk[T]) continue;
		calc(T, C);
		chi[C] += chi[T];
	}
}

ll findcen (ll C, ll P) {
	ll mx = 0;
	for(auto &T : adj[C]) {
		if(T == P || blk[T]) continue;
		if(chi[mx] < chi[T]) mx = T;
	}
	if(chi[mx] <= tot/2) return C;
	return findcen(mx, C);
}

void match (ll C, ll P, ll M, ll V, ll B, map<ll,ll> &S) {
	if(V == M) ans += S[V];
	for(auto &T : adj[C]) {
		if(T == P || blk[T]) continue;
		match(T, C, max(M, V+a[T]*B), V+a[T]*B, B, S);
	}
}

void save (ll C, ll P, ll M, ll V, ll B, map<ll,ll> &S) {
	if(V == M) S[V]++;
	for(auto &T : adj[C]) {
		if(T == P || blk[T]) continue;
		save(T, C, max(M, V+a[T]*B), V+a[T]*B, B, S);
	}
}

void solve (ll I) {
	tot = 0; calc(I, 0);
	ll C = findcen(I, 0);
	pl.clear(); mi.clear();
	pl[a[C]]++; mi[0]++;
	for(auto &T : adj[C]) {
		if(blk[T]) continue;
		ll M1 = max({0ll, a[C], a[T]+a[C]}), M2 = max(0ll, -a[T]);
		match(T, C, M1, a[T]+a[C], 1, mi);
		match(T, C, M2, -a[T], -1, pl);
		save(T, C, M1, a[T]+a[C], 1, pl);
		save(T, C, M2, -a[T], -1, mi);
	}
	blk[C] = true;
	for(auto &T : adj[C]) {
		if(blk[T]) continue;
		solve(T);
	}
}

int main()
{
	scanf("%lld%s",&n,ipt+1);
	for(ll i=1;i<=n;i++) {
		a[i] = 2*(ipt[i] == '(') - 1;
	}
	for(ll i=1;i<n;i++) {
		ll A, B;
		scanf("%lld%lld",&A,&B);
		adj[A].push_back(B);
		adj[B].push_back(A);
	}
	solve(1);
	printf("%lld\n",ans);
}

Compilation message

zagrade.cpp: In function 'int main()':
zagrade.cpp:71:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%s",&n,ipt+1);
                          ^
zagrade.cpp:77:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld",&A,&B);
                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14332 KB Output is correct
2 Correct 6 ms 14332 KB Output is correct
3 Correct 6 ms 14332 KB Output is correct
4 Correct 3 ms 14332 KB Output is correct
5 Correct 0 ms 14332 KB Output is correct
6 Correct 0 ms 14332 KB Output is correct
7 Correct 6 ms 14332 KB Output is correct
8 Correct 0 ms 14332 KB Output is correct
9 Correct 3 ms 14332 KB Output is correct
10 Correct 0 ms 14332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 699 ms 40116 KB Output is correct
2 Correct 1076 ms 47508 KB Output is correct
3 Correct 666 ms 40120 KB Output is correct
4 Correct 973 ms 45664 KB Output is correct
5 Correct 669 ms 40120 KB Output is correct
6 Correct 836 ms 42624 KB Output is correct
7 Correct 676 ms 40116 KB Output is correct
8 Correct 773 ms 42624 KB Output is correct
9 Correct 693 ms 40112 KB Output is correct
10 Correct 1613 ms 58728 KB Output is correct
11 Correct 559 ms 39984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 14332 KB Output is correct
2 Correct 6 ms 14332 KB Output is correct
3 Correct 6 ms 14332 KB Output is correct
4 Correct 3 ms 14332 KB Output is correct
5 Correct 0 ms 14332 KB Output is correct
6 Correct 0 ms 14332 KB Output is correct
7 Correct 6 ms 14332 KB Output is correct
8 Correct 0 ms 14332 KB Output is correct
9 Correct 3 ms 14332 KB Output is correct
10 Correct 0 ms 14332 KB Output is correct
11 Correct 699 ms 40116 KB Output is correct
12 Correct 1076 ms 47508 KB Output is correct
13 Correct 666 ms 40120 KB Output is correct
14 Correct 973 ms 45664 KB Output is correct
15 Correct 669 ms 40120 KB Output is correct
16 Correct 836 ms 42624 KB Output is correct
17 Correct 676 ms 40116 KB Output is correct
18 Correct 773 ms 42624 KB Output is correct
19 Correct 693 ms 40112 KB Output is correct
20 Correct 1613 ms 58728 KB Output is correct
21 Correct 559 ms 39984 KB Output is correct
22 Correct 1403 ms 25552 KB Output is correct
23 Correct 1416 ms 25552 KB Output is correct
24 Correct 1413 ms 25552 KB Output is correct
25 Correct 1273 ms 25552 KB Output is correct
26 Correct 813 ms 30496 KB Output is correct
27 Correct 843 ms 28012 KB Output is correct
28 Correct 809 ms 27084 KB Output is correct
29 Correct 1539 ms 58728 KB Output is correct
30 Correct 1529 ms 58728 KB Output is correct
31 Correct 153 ms 28236 KB Output is correct
32 Correct 556 ms 39984 KB Output is correct