답안 #144970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
144970 2019-08-18T10:01:06 Z emilem Zagrade (COI17_zagrade) C++14
0 / 100
3000 ms 18424 KB
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long ans;
int n;
int op, clos;
string path;
vector< vector<int> > nei;
string a;

long long Chain(const string& a)
{
	vector<int> pref(a.length());
	for (int i = 0; i < pref.size(); ++i)
	{
		if (a[i] == '(') ++pref[i];
		else --pref[i];
		if (i) pref[i] += pref[i - 1];
	}
	for (int i = 0; i < n; ++i)
	{
		int r = -1;
		int minElem = pref[i];
		for (int j = i; j < n; ++j)
		{
			minElem = min(minElem, pref[j]);
			if (minElem - (i ? pref[i - 1] : 0) < 0)
				break;
			else
				r = j;
		}
		if (r != -1)
			for (int j = i; j <= r; ++j)
				if (pref[j] == (i ? pref[i - 1] : 0))
					++ans;
	}
	return 0;
}
																		void Dfs(int v, int par)
{
if (op == clos && a[v] == ')')
	return;
if (a[v] == ')') ++clos;
else ++op;
path += a[v];
if (clos == op)
	++ans;
for (int i = 0; i < nei[v].size(); ++i)
{
	int to = nei[v][i];
	if (to == par) continue;
	Dfs(to, v);
}
path.pop_back();
if (a[v] == ')') --clos;
else --op;
}
int main()
{
	cin >> n;
	nei.resize(n);
	cin >> a;
	for (int i = 1; i < n; ++i)
	{
		int u, v;
		cin >> u >> v; --u; --v;
		nei[u].push_back(v);
		nei[v].push_back(u);
	}
	if (false/*n <= 1000*/)
	{
		for (int v = 0; v < n; ++v)
		{
			op = clos = 0;
			path = "";
			Dfs(v, -1);
		}
		cout << ans << endl;
		return 0;
	}
	else // Is a chain.
	{
		ans += Chain(a);
		reverse(a.begin(), a.end());
		ans += Chain(a);
		cout << ans << endl;

		char I;
		cin >> I;
	}

	char I;
	cin >> I;
}

Compilation message

zagrade.cpp: In function 'long long int Chain(const string&)':
zagrade.cpp:17:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < pref.size(); ++i)
                  ~~^~~~~~~~~~~~~
zagrade.cpp: In function 'void Dfs(int, int)':
zagrade.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for (int i = 0; i < nei[v].size(); ++i)
                 ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 975 ms 18424 KB Output is correct
2 Correct 397 ms 18320 KB Output is correct
3 Correct 1975 ms 18388 KB Output is correct
4 Correct 402 ms 18352 KB Output is correct
5 Correct 2979 ms 18292 KB Output is correct
6 Execution timed out 3010 ms 18176 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 376 KB Output isn't correct
2 Halted 0 ms 0 KB -