Submission #350157

# Submission time Handle Problem Language Result Execution time Memory
350157 2021-01-19T03:28:07 Z arnold518 Zagrade (COI17_zagrade) C++14
100 / 100
2047 ms 48480 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;

int N;
char S[MAXN+10];
vector<int> adj[MAXN+10];

int sz[MAXN+10], del[MAXN+10];

void getsz(int now, int bef)
{
	sz[now]=1;
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(del[nxt]) continue;
		getsz(nxt, now);
		sz[now]+=sz[nxt];
	}
}

int getcen(int now, int bef, int S)
{
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(del[nxt]) continue;
		if(sz[nxt]>S/2) return getcen(nxt, now, S);
	}
	return now;
}

vector<int> V;
int cnt[MAXN+10];
ll ans=0;

void dfs(int now, int bef, int p, int q)
{
	if(S[now]=='(') p++;
	else
	{
		p--;
		q=min(q, p);
	}
	if(p==q)
	{
		//printf("%d : %d %d\n", now, p, cnt[p+N]);
		ans+=cnt[p+N];
	}
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(del[nxt]) continue;
		dfs(nxt, now, p, q);
	}
}

void dfs2(int now, int bef, int p, int q)
{
	if(S[now]==')') p++;
	else
	{
		p--;
		q=min(q, p);
	}
	if(p==q)
	{
		V.push_back(p+N);
		cnt[p+N]++;
	}
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(del[nxt]) continue;
		dfs2(nxt, now, p, q);
	}
}

void dfs3(int now, int bef, int p, int q)
{
	if(S[now]==')') p++;
	else
	{
		p--;
		q=min(q, p);
	}
	if(p==q)
	{
		//printf("%d : %d %d\n", now, p, cnt[p+N]);
		ans+=cnt[p+N];
	}
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(del[nxt]) continue;
		dfs3(nxt, now, p, q);
	}
}

void dfs4(int now, int bef, int p, int q)
{
	if(S[now]=='(') p++;
	else
	{
		p--;
		q=min(q, p);
	}
	if(p==q)
	{
		V.push_back(p+N);
		cnt[p+N]++;
	}
	for(int nxt : adj[now])
	{
		if(nxt==bef) continue;
		if(del[nxt]) continue;
		dfs4(nxt, now, p, q);
	}
}

void decomp(int now)
{
	getsz(now, now);
	now=getcen(now, now, sz[now]);
	del[now]=true;

	//printf("CEN %d\n", now);

	{
		int p=0, q=0;
		if(S[now]=='(') p++;
		else
		{
			p--;
			q=min(q, p);
		}

		V.push_back(0+N);
		cnt[0+N]++;
		for(int i=0; i<adj[now].size(); i++)
		{
			int nxt=adj[now][i];
			if(del[nxt]) continue;
			dfs(nxt, now, p, q);
			dfs2(nxt, now, 0, 0);
		}
		for(auto it : V) cnt[it]=0;
		V.clear();

		V.push_back(0+N);
		cnt[0+N]++;
		for(int i=adj[now].size()-1; i>=0; i--)
		{
			int nxt=adj[now][i];
			if(del[nxt]) continue;
			dfs(nxt, now, p, q);
			dfs2(nxt, now, 0, 0);
		}
		for(auto it : V) cnt[it]=0;
		V.clear();
	}

	{
		int p=0, q=0;
		if(S[now]==')') p++;
		else
		{
			p--;
			q=min(q, p);
		}

		V.push_back(0+N);
		cnt[0+N]++;
		for(int i=0; i<adj[now].size(); i++)
		{
			int nxt=adj[now][i];
			if(del[nxt]) continue;
			dfs3(nxt, now, p, q);
			dfs4(nxt, now, 0, 0);
		}
		for(auto it : V) cnt[it]=0;
		V.clear();

		V.push_back(0+N);
		cnt[0+N]++;
		for(int i=adj[now].size()-1; i>=0; i--)
		{
			int nxt=adj[now][i];
			if(del[nxt]) continue;
			dfs3(nxt, now, p, q);
			dfs4(nxt, now, 0, 0);
		}
		for(auto it : V) cnt[it]=0;
		V.clear();
	}

	for(int nxt : adj[now])
	{
		if(del[nxt]) continue;
		decomp(nxt);
	}
}

int main()
{
	scanf("%d", &N);
	scanf(" %s", S+1);
	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	decomp(1);
	printf("%lld\n", ans/2);
}

Compilation message

zagrade.cpp: In function 'void decomp(int)':
zagrade.cpp:146:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |   for(int i=0; i<adj[now].size(); i++)
      |                ~^~~~~~~~~~~~~~~~
zagrade.cpp:180:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |   for(int i=0; i<adj[now].size(); i++)
      |                ~^~~~~~~~~~~~~~~~
zagrade.cpp: In function 'int main()':
zagrade.cpp:212:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  212 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
zagrade.cpp:213:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  213 |  scanf(" %s", S+1);
      |  ~~~~~^~~~~~~~~~~~
zagrade.cpp:217:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  217 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7404 KB Output is correct
2 Correct 7 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 7 ms 7532 KB Output is correct
6 Correct 7 ms 7404 KB Output is correct
7 Correct 7 ms 7404 KB Output is correct
8 Correct 6 ms 7404 KB Output is correct
9 Correct 8 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 805 ms 42988 KB Output is correct
2 Correct 787 ms 44172 KB Output is correct
3 Correct 841 ms 42988 KB Output is correct
4 Correct 809 ms 43736 KB Output is correct
5 Correct 805 ms 42860 KB Output is correct
6 Correct 812 ms 43364 KB Output is correct
7 Correct 844 ms 43024 KB Output is correct
8 Correct 818 ms 43492 KB Output is correct
9 Correct 813 ms 42988 KB Output is correct
10 Correct 687 ms 44256 KB Output is correct
11 Correct 685 ms 43616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7404 KB Output is correct
2 Correct 7 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 7 ms 7532 KB Output is correct
6 Correct 7 ms 7404 KB Output is correct
7 Correct 7 ms 7404 KB Output is correct
8 Correct 6 ms 7404 KB Output is correct
9 Correct 8 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
11 Correct 805 ms 42988 KB Output is correct
12 Correct 787 ms 44172 KB Output is correct
13 Correct 841 ms 42988 KB Output is correct
14 Correct 809 ms 43736 KB Output is correct
15 Correct 805 ms 42860 KB Output is correct
16 Correct 812 ms 43364 KB Output is correct
17 Correct 844 ms 43024 KB Output is correct
18 Correct 818 ms 43492 KB Output is correct
19 Correct 813 ms 42988 KB Output is correct
20 Correct 687 ms 44256 KB Output is correct
21 Correct 685 ms 43616 KB Output is correct
22 Correct 1871 ms 24552 KB Output is correct
23 Correct 1803 ms 24300 KB Output is correct
24 Correct 1936 ms 24328 KB Output is correct
25 Correct 2047 ms 24456 KB Output is correct
26 Correct 1012 ms 31752 KB Output is correct
27 Correct 1008 ms 28268 KB Output is correct
28 Correct 1027 ms 27016 KB Output is correct
29 Correct 709 ms 48348 KB Output is correct
30 Correct 741 ms 48480 KB Output is correct
31 Correct 147 ms 24192 KB Output is correct
32 Correct 683 ms 47856 KB Output is correct