답안 #104556

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
104556 2019-04-07T22:55:19 Z Shtef Zagrade (COI17_zagrade) C++14
100 / 100
1460 ms 43940 KB
#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

typedef long long ll;

int n, val[300005], ss[300005], br;
ll dp[300005], sol;
char s[300005];
vector <int> ms[300005];
bool bio[300005];

void dfs1(int x, int p){
	br++;
	ss[x] = 1;
	for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = *i;
		if(o == p || bio[o])
			continue;
		dfs1(o, x);
		ss[x] += ss[o];
	}
}

int dfs2(int x, int p){
	for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = *i;
		if(o == p || bio[o])
			continue;
		if(ss[o] > br / 2)
			return dfs2(o, x);
	}
	return x;
}

void processdp(int x, int p, int sad, int sve, int add){
	sad = (val[x] == 1 ? max(1, sad + 1) : sad - 1);
	sve -= (sad < 0);
	if(sad <= 0){
		dp[-sve] += add;
	}
	for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = *i;
		if(o == p || bio[o])
			continue;
		processdp(o, x, sad, sve, add);
	}
}

void dfs3(int x, int p, int sad, int sve){
	sad = (val[x] == -1 ? min(-1, sad - 1) : sad + 1);
	sve += (sad > 0);
	if(sad >= 0){
		sol += dp[sve];
	}
	for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
		int o = *i;
		if(o == p || bio[o])
			continue;
		dfs3(o, x, sad, sve);
	}
}

void decompose(int root){
	br = 0;
	dfs1(root, -1);
	int centroid = dfs2(root, -1);
	bio[centroid] = 1;
	processdp(centroid, -1, 0, 0, 1);
	sol += dp[0];
	for(vector <int>::iterator i = ms[centroid].begin() ; i != ms[centroid].end() ; ++i){
		int o = *i;
		if(bio[o])
			continue;
		processdp(o, centroid, val[centroid], min(0, val[centroid]), -1);
		dfs3(o, centroid, 0, 0);
		processdp(o, centroid, val[centroid], min(0, val[centroid]), 1);
	}
	//cout << centroid << ' ' << sol << endl;
	processdp(centroid, -1, 0, 0, -1);
	for(vector <int>::iterator i = ms[centroid].begin() ; i != ms[centroid].end() ; ++i){
		int o = *i;
		if(bio[o])
			continue;
		decompose(o);
	}
}

int main(){
scanf("%d", &n);
scanf("%s", &s);
for(int i = 0 ; i < n ; ++i){
	if(s[i] == '('){
		val[i + 1] = 1;
	}
	else{
		val[i + 1] = -1;
	}
}
for(int i = 0 ; i < n - 1 ; ++i){
	int x, y;
	scanf("%d %d", &x, &y);
	ms[x].push_back(y);
	ms[y].push_back(x);
}
decompose(1);
printf("%lld\n", sol);

return 0;
}

Compilation message

zagrade.cpp: In function 'int main()':
zagrade.cpp:93:15: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[300005]' [-Wformat=]
 scanf("%s", &s);
             ~~^
zagrade.cpp:92:6: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d", &n);
 ~~~~~^~~~~~~~~~
zagrade.cpp:93:6: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%s", &s);
 ~~~~~^~~~~~~~~~
zagrade.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &x, &y);
  ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 11 ms 7424 KB Output is correct
4 Correct 10 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 11 ms 7424 KB Output is correct
7 Correct 10 ms 7424 KB Output is correct
8 Correct 10 ms 7424 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 600 ms 38536 KB Output is correct
2 Correct 765 ms 43272 KB Output is correct
3 Correct 648 ms 42712 KB Output is correct
4 Correct 620 ms 43072 KB Output is correct
5 Correct 633 ms 42872 KB Output is correct
6 Correct 690 ms 42872 KB Output is correct
7 Correct 607 ms 42744 KB Output is correct
8 Correct 566 ms 42868 KB Output is correct
9 Correct 586 ms 42872 KB Output is correct
10 Correct 581 ms 43932 KB Output is correct
11 Correct 478 ms 42776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 11 ms 7424 KB Output is correct
4 Correct 10 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 11 ms 7424 KB Output is correct
7 Correct 10 ms 7424 KB Output is correct
8 Correct 10 ms 7424 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 600 ms 38536 KB Output is correct
12 Correct 765 ms 43272 KB Output is correct
13 Correct 648 ms 42712 KB Output is correct
14 Correct 620 ms 43072 KB Output is correct
15 Correct 633 ms 42872 KB Output is correct
16 Correct 690 ms 42872 KB Output is correct
17 Correct 607 ms 42744 KB Output is correct
18 Correct 566 ms 42868 KB Output is correct
19 Correct 586 ms 42872 KB Output is correct
20 Correct 581 ms 43932 KB Output is correct
21 Correct 478 ms 42776 KB Output is correct
22 Correct 1455 ms 24284 KB Output is correct
23 Correct 1386 ms 24312 KB Output is correct
24 Correct 1455 ms 24084 KB Output is correct
25 Correct 1460 ms 24184 KB Output is correct
26 Correct 738 ms 30312 KB Output is correct
27 Correct 707 ms 27540 KB Output is correct
28 Correct 732 ms 26360 KB Output is correct
29 Correct 500 ms 43896 KB Output is correct
30 Correct 505 ms 43940 KB Output is correct
31 Correct 119 ms 23784 KB Output is correct
32 Correct 471 ms 42744 KB Output is correct