Submission #785165

# Submission time Handle Problem Language Result Execution time Memory
785165 2023-07-17T06:34:50 Z 박상훈(#10022) Zagrade (COI17_zagrade) C++17
100 / 100
335 ms 104816 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

struct DS{
	map<int, ll> mp[2];
	int ofs[2];

	ll operator ()(int x, int y){
		if (mp[x].find(y-ofs[x])==mp[x].end()) return 0;
		return mp[x][y-ofs[x]];
	}

	int size(){
		return mp[0].size() + mp[1].size();
	}

	void insert(int x, int y, ll val){
		y -= ofs[x];
		mp[x][y] += val;
	}

	void refresh(){
		for (int x=0;x<2;x++){
			for (auto iter=mp[x].begin();iter!=mp[x].end();){
				if (iter->first + ofs[x] >= 0) break;
				iter = mp[x].erase(iter);
			}
		}
	}
}dp[300300];

int a[300300];
vector<int> adj[300300];
ll ans;

void merge(int s, int v){
	if (dp[s].size() >= dp[v].size()){
		for (int x=0;x<2;x++){
			for (auto &[_y, val]:dp[v].mp[x]){
				int y = _y + dp[v].ofs[x];

				ans += dp[s](x^1, y) * val;
			}
		}

		for (int x=0;x<2;x++){
			for (auto &[_y, val]:dp[v].mp[x]){
				int y = _y + dp[v].ofs[x];

				if (x==0) y += a[s];
				else y -= a[s];
				if (y<0) continue;

				dp[s].insert(x, y, val);
			}
		}
	}

	else{
		for (int x=0;x<2;x++){
			for (auto &[_y, val]:dp[s].mp[x]){
				int y = _y + dp[s].ofs[x];

				ans += dp[v](x^1, y) * val;
			}
		}

		dp[v].ofs[0] += a[s];
		dp[v].ofs[1] -= a[s];

		dp[v].refresh();

		for (int x=0;x<2;x++){
			for (auto &[_y, val]:dp[s].mp[x]){
				int y = _y + dp[s].ofs[x];

				dp[v].insert(x, y, val);
			}
		}

		swap(dp[s], dp[v]);
	}
}

void dfs(int s, int pa = 0){
	if (a[s]==1) dp[s].insert(0, 1, 1);
	else dp[s].insert(1, 1, 1);

	for (auto &v:adj[s]) if (v!=pa){
		dfs(v, s);
		merge(s, v);
	}
}

int main(){
	int n;
	scanf("%d", &n);
	for (int i=1;i<=n;i++){
		char x;
		scanf(" %c", &x);
		if (x=='(') a[i] = 1;
		else a[i] = -1;
	}

	for (int i=1;i<=n-1;i++){
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	dfs(1);
	printf("%lld\n", ans);
}

Compilation message

zagrade.cpp: In function 'int main()':
zagrade.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
zagrade.cpp:102:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |   scanf(" %c", &x);
      |   ~~~~~^~~~~~~~~~~
zagrade.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 37972 KB Output is correct
2 Correct 21 ms 37972 KB Output is correct
3 Correct 20 ms 37924 KB Output is correct
4 Correct 20 ms 37972 KB Output is correct
5 Correct 21 ms 37980 KB Output is correct
6 Correct 20 ms 37972 KB Output is correct
7 Correct 21 ms 37968 KB Output is correct
8 Correct 21 ms 38044 KB Output is correct
9 Correct 20 ms 38044 KB Output is correct
10 Correct 21 ms 37964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 95348 KB Output is correct
2 Correct 201 ms 102836 KB Output is correct
3 Correct 168 ms 95468 KB Output is correct
4 Correct 204 ms 100984 KB Output is correct
5 Correct 171 ms 95528 KB Output is correct
6 Correct 200 ms 97956 KB Output is correct
7 Correct 184 ms 95592 KB Output is correct
8 Correct 200 ms 98048 KB Output is correct
9 Correct 170 ms 95468 KB Output is correct
10 Correct 219 ms 104772 KB Output is correct
11 Correct 150 ms 95416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 37972 KB Output is correct
2 Correct 21 ms 37972 KB Output is correct
3 Correct 20 ms 37924 KB Output is correct
4 Correct 20 ms 37972 KB Output is correct
5 Correct 21 ms 37980 KB Output is correct
6 Correct 20 ms 37972 KB Output is correct
7 Correct 21 ms 37968 KB Output is correct
8 Correct 21 ms 38044 KB Output is correct
9 Correct 20 ms 38044 KB Output is correct
10 Correct 21 ms 37964 KB Output is correct
11 Correct 169 ms 95348 KB Output is correct
12 Correct 201 ms 102836 KB Output is correct
13 Correct 168 ms 95468 KB Output is correct
14 Correct 204 ms 100984 KB Output is correct
15 Correct 171 ms 95528 KB Output is correct
16 Correct 200 ms 97956 KB Output is correct
17 Correct 184 ms 95592 KB Output is correct
18 Correct 200 ms 98048 KB Output is correct
19 Correct 170 ms 95468 KB Output is correct
20 Correct 219 ms 104772 KB Output is correct
21 Correct 150 ms 95416 KB Output is correct
22 Correct 330 ms 76464 KB Output is correct
23 Correct 328 ms 77436 KB Output is correct
24 Correct 335 ms 77496 KB Output is correct
25 Correct 328 ms 77560 KB Output is correct
26 Correct 180 ms 83660 KB Output is correct
27 Correct 189 ms 80752 KB Output is correct
28 Correct 196 ms 79528 KB Output is correct
29 Correct 207 ms 104816 KB Output is correct
30 Correct 216 ms 104816 KB Output is correct
31 Correct 122 ms 68376 KB Output is correct
32 Correct 152 ms 95488 KB Output is correct