# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
785125 |
2023-07-17T05:40:58 Z |
반딧불(#10021) |
Zagrade (COI17_zagrade) |
C++17 |
|
915 ms |
63008 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void input();
void makeCentroidTree();
int n;
int arr[300002];
vector<int> link[300002];
int main(){
input();
makeCentroidTree();
}
void input(){
scanf("%d", &n);
for(int i=1; i<=n; i++){
char c;
scanf(" %c", &c);
if(c == '(') arr[i] = 1;
else arr[i] = -1;
}
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y), link[y].push_back(x);
}
}
int centroid;
int subTreeSize[300002];
bool centroidUsed[300002];
int centroidPar[300002], centroidDepth[300002];
map<int, ll> mp;
map<int, ll> mp2;
ll ans;
void subTreeDfs(int x, int p=-1){
subTreeSize[x] = 1;
for(auto y: link[x]){
if(y==p || centroidUsed[y]) continue;
subTreeDfs(y, x);
subTreeSize[x] += subTreeSize[y];
}
}
int getCentroid(int x, int p, int lim){
for(auto y: link[x]){
if(y==p || centroidUsed[y]) continue;
if(subTreeSize[y] >= lim) return getCentroid(y, x, lim);
}
return x;
}
void dfs(int x, int p, int depth, int minDepth, vector<int> &vec){
depth += arr[x];
minDepth = min(minDepth, depth);
if(minDepth == depth) vec.push_back(-depth);
for(auto y: link[x]){
if(y==p || centroidUsed[y]) continue;
dfs(y, x, depth, minDepth, vec);
}
}
void dfs2(int x, int p, int depth, int minDepth, vector<int> &vec){
depth -= arr[x];
minDepth = min(minDepth, depth);
if(minDepth == depth) vec.push_back(-depth);
for(auto y: link[x]){
if(y==p || centroidUsed[y]) continue;
dfs2(y, x, depth, minDepth, vec);
}
}
int findCentroid(int x, int depth = 0){
subTreeDfs(x);
x = getCentroid(x, -1, (subTreeSize[x]+1)/2);
centroidUsed[x] = 1, centroidDepth[x] = depth;
mp.clear();
mp2.clear();
if(arr[x] == 1) mp[1]++;
else mp2[1]++;
for(auto y: link[x]){ /// �տ� ã�� front�� �ڿ� ã�� back�� ������
if(centroidUsed[y]) continue;
vector<int> vec, vec2;
dfs(y, x, 0, 0, vec);
dfs2(y, x, 0, 0, vec2);
for(auto p: vec) ans += mp[p];
for(auto p: vec2) ans += mp2[p];
for(auto p: vec) mp2[p-arr[x]]++;
for(auto p: vec2) mp[p+arr[x]]++;
}
for(auto y: link[x]){
if(centroidUsed[y]) continue;
int tmp = findCentroid(y, depth+1);
centroidPar[tmp] = x;
}
return x;
}
void makeCentroidTree(){
centroid = findCentroid(1);
printf("%lld", ans);
}
Compilation message
zagrade.cpp: In function 'void input()':
zagrade.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
zagrade.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf(" %c", &c);
| ~~~~~^~~~~~~~~~~
zagrade.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7420 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
358 ms |
45288 KB |
Output is correct |
2 |
Correct |
636 ms |
50800 KB |
Output is correct |
3 |
Correct |
344 ms |
45296 KB |
Output is correct |
4 |
Correct |
544 ms |
48976 KB |
Output is correct |
5 |
Correct |
358 ms |
45280 KB |
Output is correct |
6 |
Correct |
432 ms |
45552 KB |
Output is correct |
7 |
Correct |
344 ms |
45252 KB |
Output is correct |
8 |
Correct |
420 ms |
47920 KB |
Output is correct |
9 |
Correct |
340 ms |
45236 KB |
Output is correct |
10 |
Correct |
859 ms |
62964 KB |
Output is correct |
11 |
Correct |
328 ms |
45704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7420 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
358 ms |
45288 KB |
Output is correct |
12 |
Correct |
636 ms |
50800 KB |
Output is correct |
13 |
Correct |
344 ms |
45296 KB |
Output is correct |
14 |
Correct |
544 ms |
48976 KB |
Output is correct |
15 |
Correct |
358 ms |
45280 KB |
Output is correct |
16 |
Correct |
432 ms |
45552 KB |
Output is correct |
17 |
Correct |
344 ms |
45252 KB |
Output is correct |
18 |
Correct |
420 ms |
47920 KB |
Output is correct |
19 |
Correct |
340 ms |
45236 KB |
Output is correct |
20 |
Correct |
859 ms |
62964 KB |
Output is correct |
21 |
Correct |
328 ms |
45704 KB |
Output is correct |
22 |
Correct |
519 ms |
22356 KB |
Output is correct |
23 |
Correct |
562 ms |
22316 KB |
Output is correct |
24 |
Correct |
550 ms |
22252 KB |
Output is correct |
25 |
Correct |
536 ms |
22372 KB |
Output is correct |
26 |
Correct |
386 ms |
29632 KB |
Output is correct |
27 |
Correct |
379 ms |
26112 KB |
Output is correct |
28 |
Correct |
387 ms |
24812 KB |
Output is correct |
29 |
Correct |
915 ms |
63008 KB |
Output is correct |
30 |
Correct |
897 ms |
62952 KB |
Output is correct |
31 |
Correct |
119 ms |
23004 KB |
Output is correct |
32 |
Correct |
286 ms |
45688 KB |
Output is correct |