답안 #26329

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26329 2017-06-29T08:18:52 Z khsoo01 Zagrade (COI17_zagrade) C++11
100 / 100
1573 ms 58732 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 6 ms 14332 KB Output is correct
2 Correct 6 ms 14332 KB Output is correct
3 Correct 0 ms 14332 KB Output is correct
4 Correct 6 ms 14332 KB Output is correct
5 Correct 0 ms 14332 KB Output is correct
6 Correct 3 ms 14332 KB Output is correct
7 Correct 3 ms 14332 KB Output is correct
8 Correct 6 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 689 ms 40116 KB Output is correct
2 Correct 1069 ms 47512 KB Output is correct
3 Correct 666 ms 40120 KB Output is correct
4 Correct 943 ms 45660 KB Output is correct
5 Correct 693 ms 40116 KB Output is correct
6 Correct 806 ms 42628 KB Output is correct
7 Correct 686 ms 40120 KB Output is correct
8 Correct 836 ms 42624 KB Output is correct
9 Correct 646 ms 40116 KB Output is correct
10 Correct 1409 ms 58732 KB Output is correct
11 Correct 506 ms 39988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 14332 KB Output is correct
2 Correct 6 ms 14332 KB Output is correct
3 Correct 0 ms 14332 KB Output is correct
4 Correct 6 ms 14332 KB Output is correct
5 Correct 0 ms 14332 KB Output is correct
6 Correct 3 ms 14332 KB Output is correct
7 Correct 3 ms 14332 KB Output is correct
8 Correct 6 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 689 ms 40116 KB Output is correct
12 Correct 1069 ms 47512 KB Output is correct
13 Correct 666 ms 40120 KB Output is correct
14 Correct 943 ms 45660 KB Output is correct
15 Correct 693 ms 40116 KB Output is correct
16 Correct 806 ms 42628 KB Output is correct
17 Correct 686 ms 40120 KB Output is correct
18 Correct 836 ms 42624 KB Output is correct
19 Correct 646 ms 40116 KB Output is correct
20 Correct 1409 ms 58732 KB Output is correct
21 Correct 506 ms 39988 KB Output is correct
22 Correct 1366 ms 25552 KB Output is correct
23 Correct 1086 ms 25552 KB Output is correct
24 Correct 1076 ms 25552 KB Output is correct
25 Correct 1376 ms 25552 KB Output is correct
26 Correct 783 ms 30500 KB Output is correct
27 Correct 803 ms 28008 KB Output is correct
28 Correct 726 ms 27084 KB Output is correct
29 Correct 1573 ms 58728 KB Output is correct
30 Correct 1529 ms 58732 KB Output is correct
31 Correct 123 ms 28236 KB Output is correct
32 Correct 579 ms 39984 KB Output is correct