Submission #145309

# Submission time Handle Problem Language Result Execution time Memory
145309 2019-08-19T14:55:05 Z arayi Zagrade (COI17_zagrade) C++17
100 / 100
1238 ms 71508 KB
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <vector>
#include <cstring>
#include <ctime>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <ctime>
#define fr first
#define sc second
#define MP make_pair
#define PB push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lli long long int
#define y1 arayikhalatyan
using namespace std;

lli gcd(lli a, lli b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
lli cg(lli n) {
	return n ^ (n >> 1);
}
lli SUM(lli a)
{
	return (a * (a + 1) / 2);
}
bool CAN(int x, int y, int n, int m)
{
	if (x >= 0 && y >= 0 && x < n && y < m)
	{
		return true;
	}
	return false;
}
double her(double x1, double y1, double x2, double y2)
{
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
string strsum(string a, string b)
{
	int p = 0;
	string c;
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	if (b.length() < a.length())
	{
		for (int i = b.length(); i < a.length(); i++)
		{
			b += "0";
		}
	}
	else
	{
		for (int i = a.length(); i < b.length(); i++)
		{
			a += "0";
		}
	}

	a += "0", b += "0";
	for (int i = 0; i < a.length(); i++)
	{
		c += (a[i] - '0' + b[i] - '0' + p) % 10 + '0';
		p = (a[i] + b[i] - '0' - '0' + p) / 10;
	}
	if (c[c.length() - 1] == '0') c.erase(c.length() - 1, 1);
	reverse(c.begin(), c.end());
	return c;
}
string strmin(string a, string b)
{
	if (a.length() > b.length()) return b;
	if (b.length() > a.length()) return a;
	for (int i = 0; i < a.length(); i++)
	{
		if (a[i] > b[i]) return b;
		if (b[i] > a[i]) return a;
	}
	return a;
}

char vow[] = { 'a', 'e', 'i', 'o', 'u' };
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, 1, -1 };



const int N = 1e6 + 30;
const lli mod = 1000000007;
int mn(int a, int b)
{
	if (a < b) return a;
	return b;
}
int n, sm;
string s;
vector <int> g[N];
bool col[N];
int qan[N];
queue <int> h;
lli nerq[2 * N], ver[2 * N];
lli pat;
vector <int> ss;
void dfsqan(int v, int par)
{
	qan[v] = 1;
	for (auto p : g[v])
	{
		if (p != par && !col[p])
		{
			dfsqan(p, v);
			qan[v] += qan[p];
		}
	}
}

void dfshashvv(int v, int par, int min, int val)
{
	if (s[v - 1] == '(')
	{
		val++;
		min = mn(1, min + 1);
		if (min >= 0) ver[val + N]++, ss.PB(val + N);
	}
	else min = mn(-1, min - 1), val--;

	for (auto p : g[v])
	{
		if (p != par && !col[p])
		{
			dfshashvv(p, v, min, val);
		}
	}
}
void dfshashvn(int v, int par, int min, int val)
{
	if (s[v - 1] == '(') val++;
	else
	{
		val--;
		if (val <= min)  nerq[N + val]++, ss.PB(N + val);
		min = mn(val, min);
	}
	//cout << v << "_" << val << endl;
	for (auto p : g[v])
	{
		if (p != par && !col[p])
		{
			dfshashvn(p, v, min, val);
		}
	}
}
void dfspatn(int v, int par, int min, int val)
{
	if (s[v - 1] == '(') val++;
	else
	{
		val--;
		if (val <= min)  pat += ver[N - val];
		min = mn(val, min);
	}
	for (auto p : g[v])
	{
		if (p != par && !col[p])
		{
			dfspatn(p, v, min, val);
		}
	}
}
void dfspatv(int v, int par, int min, int val)
{
	if (s[v - 1] == '(')
	{
		val++;
		min = mn(1, min + 1);
		if (min >= 0) pat += nerq[N - val];
	}
	else min = mn(-1, min - 1), val--;
	for (auto p : g[v])
	{
		if (p != par && !col[p])
		{
			dfspatv(p, v, min, val);
		}
	}
}
void dfsc(int v, int par)
{
	bool flag = true;
	for (auto p : g[v])
	{
		if (p != par && !col[p])
		{
			if (qan[p] > sm)
			{
				dfsc(p, v);
				flag = false;
			}
		}
	}
	if (flag)
	{
		//cout << v << " ";
		col[v] = true;
		int val, min;
		if (s[v - 1] == '(') val = 1, min = 0;
		else val = -1, min = -1, nerq[N - 1]++, ss.PB(N - 1);
		for (auto p : g[v])
		{
			if (!col[p])
			{
				h.push(p);
				dfspatn(p, v, min, val);
				dfspatv(p, v, 0, 0);
				dfshashvn(p, v, min, val);
				dfshashvv(p, v, 0, 0);
			}
		}
		pat += nerq[N];
		if (val == 1) pat += ver[N - 1];
		for (auto p : ss) nerq[p] = 0, ver[p] = 0;
		ss.clear();
		//cout << pat << endl;
	}
}
int main()
{
	fastio;
	cin >> n >> s;
	for (int i = 0; i < n - 1; i++)
	{
		int a, b;
		cin >> a >> b;
		g[a].PB(b);
		g[b].PB(a);
	}
	h.push(1);
	while (!h.empty())
	{
		int k = h.front();
		h.pop();
		//cout << "k-" << k << endl;
		dfsqan(k, -1);
		sm = qan[k] / 2;
		dfsc(k, -1);
	}
	cout << pat;
	return 0;
}

Compilation message

zagrade.cpp: In function 'std::__cxx11::string strsum(std::__cxx11::string, std::__cxx11::string)':
zagrade.cpp:57:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = b.length(); i < a.length(); i++)
                            ~~^~~~~~~~~~~~
zagrade.cpp:64:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = a.length(); i < b.length(); i++)
                            ~~^~~~~~~~~~~~
zagrade.cpp:71:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < a.length(); i++)
                  ~~^~~~~~~~~~~~
zagrade.cpp: In function 'std::__cxx11::string strmin(std::__cxx11::string, std::__cxx11::string)':
zagrade.cpp:84:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < a.length(); i++)
                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 25 ms 23928 KB Output is correct
4 Correct 25 ms 23928 KB Output is correct
5 Correct 25 ms 23896 KB Output is correct
6 Correct 31 ms 23928 KB Output is correct
7 Correct 29 ms 23928 KB Output is correct
8 Correct 24 ms 23836 KB Output is correct
9 Correct 25 ms 23928 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 641 ms 62704 KB Output is correct
2 Correct 636 ms 63556 KB Output is correct
3 Correct 649 ms 62220 KB Output is correct
4 Correct 645 ms 63268 KB Output is correct
5 Correct 645 ms 62092 KB Output is correct
6 Correct 658 ms 62652 KB Output is correct
7 Correct 652 ms 61636 KB Output is correct
8 Correct 653 ms 62344 KB Output is correct
9 Correct 648 ms 61836 KB Output is correct
10 Correct 583 ms 67580 KB Output is correct
11 Correct 565 ms 63028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 23928 KB Output is correct
3 Correct 25 ms 23928 KB Output is correct
4 Correct 25 ms 23928 KB Output is correct
5 Correct 25 ms 23896 KB Output is correct
6 Correct 31 ms 23928 KB Output is correct
7 Correct 29 ms 23928 KB Output is correct
8 Correct 24 ms 23836 KB Output is correct
9 Correct 25 ms 23928 KB Output is correct
10 Correct 24 ms 23928 KB Output is correct
11 Correct 641 ms 62704 KB Output is correct
12 Correct 636 ms 63556 KB Output is correct
13 Correct 649 ms 62220 KB Output is correct
14 Correct 645 ms 63268 KB Output is correct
15 Correct 645 ms 62092 KB Output is correct
16 Correct 658 ms 62652 KB Output is correct
17 Correct 652 ms 61636 KB Output is correct
18 Correct 653 ms 62344 KB Output is correct
19 Correct 648 ms 61836 KB Output is correct
20 Correct 583 ms 67580 KB Output is correct
21 Correct 565 ms 63028 KB Output is correct
22 Correct 1238 ms 40416 KB Output is correct
23 Correct 1210 ms 40304 KB Output is correct
24 Correct 1213 ms 40364 KB Output is correct
25 Correct 1230 ms 40456 KB Output is correct
26 Correct 788 ms 48392 KB Output is correct
27 Correct 782 ms 44428 KB Output is correct
28 Correct 784 ms 43016 KB Output is correct
29 Correct 601 ms 71508 KB Output is correct
30 Correct 606 ms 71424 KB Output is correct
31 Correct 135 ms 41472 KB Output is correct
32 Correct 584 ms 67076 KB Output is correct