답안 #244162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244162 2020-07-02T17:04:01 Z MatesV13 Zagrade (COI17_zagrade) C++11
10 / 100
2367 ms 1048580 KB
#include <bits/stdc++.h>
using namespace std;
long long n, a, b, ans;
char z[300005];
vector<int> c[300005];
multiset<int> zag[2][300005];
void help(int idx){
	cout << "Obraduje se " << idx << endl;
	cout << "0: (";
	for (multiset<int>::iterator it=zag[0][idx].begin(); it!=zag[0][idx].end(); it++)
		cout << *it << " ";
	cout << ")" << endl;
	cout << "1: (";
	for (multiset<int>::iterator it=zag[1][idx].begin(); it!=zag[1][idx].end(); it++)
		cout << *it << " ";
	cout << ")" << endl;
	cout << "Trenutni ans je " << ans << endl << endl;
}
void solve(int idx, int par){
	for (int i=0; i<c[idx].size(); i++)
		if (c[idx][i]!=par) 
			solve(c[idx][i], idx);
	int vrsta;
	if (z[idx]=='(') vrsta=1;
	else vrsta=-1;
	for (int i=0; i<c[idx].size()-1; i++){
		for (multiset<int>::iterator it=zag[0][c[idx][i]].begin(); it!=zag[0][c[idx][i]].end(); it++)
			for (int j=i+1; j<c[idx].size(); j++){
				ans+=(1LL*zag[1][c[idx][j]].count(-(*it+vrsta)));
			}
		for (multiset<int>::iterator it=zag[1][c[idx][i]].begin(); it!=zag[1][c[idx][i]].end(); it++)
			for (int j=i+1; j<c[idx].size(); j++){
				ans+=(1LL*zag[0][c[idx][j]].count(-(*it+vrsta)));
			}
	} 
	for (int i=0; i<c[idx].size(); i++){
		for (multiset<int>::iterator it=zag[1][c[idx][i]].begin(); it!=zag[1][c[idx][i]].end(); it++){
			if ((*it)+vrsta>=0) zag[1][idx].insert((*it)+vrsta);
		}
	} 
	for (int i=0; i<c[idx].size(); i++){
		for (multiset<int>::iterator it=zag[0][c[idx][i]].begin(); it!=zag[0][c[idx][i]].end(); it++){
			if ((*it)+vrsta<=0) zag[0][idx].insert((*it)+vrsta);
		}
	}
	ans+=zag[1][idx].count(0)+ zag[0][idx].count(0);
//	for (multiset<int>::iterator it=zag[0][idx].begin(); it!=zag[0][idx].end(); it++)
//		if (*it!=vrsta and *it!=0) ans += zag[1][idx].count(-(*it-vrsta));
	if (vrsta==1) zag[1][idx].insert(1);
	else zag[0][idx].insert(-1);
//	help(idx);
	return;
}
int main (){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i=1; i<=n; i++) cin >> z[i];
for (int i=1; i<n; i++){
	cin >> a >> b;
	c[a].push_back(b);
	c[b].push_back(a);
} solve(1, 0); 
cout << ans;
return 0;
}

Compilation message

zagrade.cpp: In function 'void solve(int, int)':
zagrade.cpp:20:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<c[idx].size(); i++)
                ~^~~~~~~~~~~~~~
zagrade.cpp:26:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<c[idx].size()-1; i++){
                ~^~~~~~~~~~~~~~~~
zagrade.cpp:28:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j=i+1; j<c[idx].size(); j++){
                    ~^~~~~~~~~~~~~~
zagrade.cpp:32:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j=i+1; j<c[idx].size(); j++){
                    ~^~~~~~~~~~~~~~
zagrade.cpp:36:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<c[idx].size(); i++){
                ~^~~~~~~~~~~~~~
zagrade.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<c[idx].size(); i++){
                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 35840 KB Output is correct
2 Correct 28 ms 35840 KB Output is correct
3 Correct 31 ms 35840 KB Output is correct
4 Correct 25 ms 35840 KB Output is correct
5 Correct 25 ms 35840 KB Output is correct
6 Correct 26 ms 35968 KB Output is correct
7 Correct 25 ms 35840 KB Output is correct
8 Correct 29 ms 35968 KB Output is correct
9 Correct 28 ms 35832 KB Output is correct
10 Correct 28 ms 35840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2367 ms 1048580 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 35840 KB Output is correct
2 Correct 28 ms 35840 KB Output is correct
3 Correct 31 ms 35840 KB Output is correct
4 Correct 25 ms 35840 KB Output is correct
5 Correct 25 ms 35840 KB Output is correct
6 Correct 26 ms 35968 KB Output is correct
7 Correct 25 ms 35840 KB Output is correct
8 Correct 29 ms 35968 KB Output is correct
9 Correct 28 ms 35832 KB Output is correct
10 Correct 28 ms 35840 KB Output is correct
11 Runtime error 2367 ms 1048580 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Halted 0 ms 0 KB -