Submission #26284

# Submission time Handle Problem Language Result Execution time Memory
26284 2017-06-29T04:32:53 Z 김동현(#1101) Zagrade (COI17_zagrade) C++14
100 / 100
1526 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 3 ms 14032 KB Output is correct
2 Correct 3 ms 14032 KB Output is correct
3 Correct 0 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 6 ms 14032 KB Output is correct
7 Correct 6 ms 14032 KB Output is correct
8 Correct 0 ms 14032 KB Output is correct
9 Correct 3 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 593 ms 37344 KB Output is correct
3 Correct 586 ms 37336 KB Output is correct
4 Correct 579 ms 37344 KB Output is correct
5 Correct 559 ms 37344 KB Output is correct
6 Correct 506 ms 37340 KB Output is correct
7 Correct 543 ms 37340 KB Output is correct
8 Correct 506 ms 37340 KB Output is correct
9 Correct 549 ms 37340 KB Output is correct
10 Correct 559 ms 37340 KB Output is correct
11 Correct 503 ms 37340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14032 KB Output is correct
2 Correct 3 ms 14032 KB Output is correct
3 Correct 0 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 6 ms 14032 KB Output is correct
7 Correct 6 ms 14032 KB Output is correct
8 Correct 0 ms 14032 KB Output is correct
9 Correct 3 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 593 ms 37344 KB Output is correct
13 Correct 586 ms 37336 KB Output is correct
14 Correct 579 ms 37344 KB Output is correct
15 Correct 559 ms 37344 KB Output is correct
16 Correct 506 ms 37340 KB Output is correct
17 Correct 543 ms 37340 KB Output is correct
18 Correct 506 ms 37340 KB Output is correct
19 Correct 549 ms 37340 KB Output is correct
20 Correct 559 ms 37340 KB Output is correct
21 Correct 503 ms 37340 KB Output is correct
22 Correct 1526 ms 23800 KB Output is correct
23 Correct 1486 ms 23800 KB Output is correct
24 Correct 1409 ms 23800 KB Output is correct
25 Correct 1509 ms 23800 KB Output is correct
26 Correct 709 ms 27964 KB Output is correct
27 Correct 733 ms 25964 KB Output is correct
28 Correct 736 ms 25168 KB Output is correct
29 Correct 513 ms 37344 KB Output is correct
30 Correct 546 ms 37344 KB Output is correct
31 Correct 126 ms 25516 KB Output is correct
32 Correct 539 ms 37344 KB Output is correct