Submission #26340

# Submission time Handle Problem Language Result Execution time Memory
26340 2017-06-29T08:56:53 Z kdh9949 Zagrade (COI17_zagrade) C++14
100 / 100
1346 ms 37344 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, v[300010], s[300010], chk[300010], cnt[300010];
char str[300010];
vector<int> e[300010];

int sf(int x, int p){
	s[x] = 1;
	for(auto &i : e[x]){
		if(i != p && !chk[i]) s[x] += sf(i, x);
	}
	return s[x];
}

int cf(int x, int p, int t){
	for(auto &i : e[x]){
		if(i != p && !chk[i] && s[i] > t / 2) return cf(i, x, t);
	}
	return x;
}

void g(ll &ret, int t, int mv, int x, int p, int d, int mx){
	if(d == mx){
		if(!t) ret += cnt[d];
		else cnt[d]++;
	}
	for(auto &i : e[x]){
		if(i != p && !chk[i]) g(ret, t, mv, i, x, d + mv * v[i], max(mx, d + mv * v[i]));
	}
}

ll f(int x){
	int ts = sf(x, 0);
	x = cf(x, 0, ts);
	chk[x] = 1;
	ll ret = 0;
	fill(cnt, cnt + ts + 1, 0); if(v[x] > 0) cnt[1] = 1;
	for(auto &i : e[x]){
		if(!chk[i]){
			g(ret, 0, -1, i, x, -v[i], max(0, -v[i]));
			g(ret, 1, 1, i, x, v[x] + v[i], max({0, v[x], v[x] + v[i]}));
		}
	}
	fill(cnt, cnt + ts + 1, 0); if(v[x] < 0) cnt[1] = 1;
	for(auto &i : e[x]){
		if(!chk[i]){
			g(ret, 0, 1, i, x, v[i], max(0, v[i]));
			g(ret, 1, -1, i, x, -(v[x] + v[i]), max({0, -v[x], -(v[x] + v[i])}));
		}
	}
	for(auto &i : e[x]) if(!chk[i]) ret += f(i);
	return ret;
}

int main(){
	scanf("%d%s", &n, str + 1);
	for(int i = 1; i <= n; i++) v[i] = 2 * (str[i] == '(') - 1;
	for(int i = 0, x, y; i < n - 1; i++){
		scanf("%d%d", &x, &y);
		e[x].push_back(y);
		e[y].push_back(x);
	}
	printf("%lld\n", f(1));
}

Compilation message

zagrade.cpp: In function 'int main()':
zagrade.cpp:58:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%s", &n, str + 1);
                            ^
zagrade.cpp:61:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &x, &y);
                        ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 14032 KB Output is correct
2 Correct 0 ms 14032 KB Output is correct
3 Correct 3 ms 14032 KB Output is correct
4 Correct 3 ms 14032 KB Output is correct
5 Correct 3 ms 14032 KB Output is correct
6 Correct 3 ms 14032 KB Output is correct
7 Correct 3 ms 14032 KB Output is correct
8 Correct 3 ms 14032 KB Output is correct
9 Correct 6 ms 14032 KB Output is correct
10 Correct 3 ms 14032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 563 ms 37344 KB Output is correct
2 Correct 559 ms 37336 KB Output is correct
3 Correct 553 ms 37340 KB Output is correct
4 Correct 569 ms 37340 KB Output is correct
5 Correct 576 ms 37344 KB Output is correct
6 Correct 573 ms 37340 KB Output is correct
7 Correct 496 ms 37340 KB Output is correct
8 Correct 576 ms 37340 KB Output is correct
9 Correct 579 ms 37340 KB Output is correct
10 Correct 476 ms 37344 KB Output is correct
11 Correct 519 ms 37340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 14032 KB Output is correct
2 Correct 0 ms 14032 KB Output is correct
3 Correct 3 ms 14032 KB Output is correct
4 Correct 3 ms 14032 KB Output is correct
5 Correct 3 ms 14032 KB Output is correct
6 Correct 3 ms 14032 KB Output is correct
7 Correct 3 ms 14032 KB Output is correct
8 Correct 3 ms 14032 KB Output is correct
9 Correct 6 ms 14032 KB Output is correct
10 Correct 3 ms 14032 KB Output is correct
11 Correct 563 ms 37344 KB Output is correct
12 Correct 559 ms 37336 KB Output is correct
13 Correct 553 ms 37340 KB Output is correct
14 Correct 569 ms 37340 KB Output is correct
15 Correct 576 ms 37344 KB Output is correct
16 Correct 573 ms 37340 KB Output is correct
17 Correct 496 ms 37340 KB Output is correct
18 Correct 576 ms 37340 KB Output is correct
19 Correct 579 ms 37340 KB Output is correct
20 Correct 476 ms 37344 KB Output is correct
21 Correct 519 ms 37340 KB Output is correct
22 Correct 1049 ms 23800 KB Output is correct
23 Correct 1309 ms 23800 KB Output is correct
24 Correct 1346 ms 23800 KB Output is correct
25 Correct 1096 ms 23800 KB Output is correct
26 Correct 739 ms 27968 KB Output is correct
27 Correct 733 ms 25972 KB Output is correct
28 Correct 749 ms 25172 KB Output is correct
29 Correct 513 ms 37340 KB Output is correct
30 Correct 586 ms 37340 KB Output is correct
31 Correct 116 ms 25516 KB Output is correct
32 Correct 526 ms 37336 KB Output is correct