답안 #235308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
235308 2020-05-27T16:25:28 Z ArshiaDadras Zagrade (COI17_zagrade) C++14
100 / 100
382 ms 119548 KB
/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;
map<int, int> mp1[N], mp2[N];
int n, cost1[N], cost2[N];
vector<int> adj[N];
long long ans;

void Swap(int u, int v) {
	swap(mp1[u], mp1[v]);
	swap(mp2[u], mp2[v]);
	swap(cost1[u], cost1[v]);
	swap(cost2[u], cost2[v]);
}

void dfs(int u, int par) {
	int cost = cost1[u];
	for (auto v: adj[u])
		if (v ^ par) {
			dfs(v, u);
			bool rev = mp1[u].size() + mp2[u].size() < mp1[v].size() + mp2[v].size();
			if (rev)
				Swap(u, v);
			for (auto [x, k]: mp1[v])
				if (mp2[u].count(x + cost1[v] - cost2[u]))
					ans += 1LL * k * mp2[u][x + cost1[v] - cost2[u]];
			for (auto [x, k]: mp2[v])
				if (mp1[u].count(x + cost2[v] - cost1[u]))
					ans += 1LL * k * mp1[u][x + cost2[v] - cost1[u]];
			if (rev)
				Swap(u, v);
			cost1[v] += cost, cost2[v] -= cost;
			mp1[v].erase(-cost1[v] - 1);
			mp2[v].erase(-cost2[v] - 1);
			if (rev)
				Swap(u, v);
			for (auto [x, k]: mp1[v])
				mp1[u][x + cost1[v] - cost1[u]] += k;
			for (auto [x, k]: mp2[v])
				mp2[u][x + cost2[v] - cost2[u]] += k;
		}
	if (mp1[u].count(-cost1[u]))
		ans += mp1[u][-cost1[u]];
	if (mp2[u].count(-cost2[u]))
		ans += mp2[u][-cost2[u]];
	if (~cost)
		mp1[u][1 - cost1[u]]++;
	else
		mp2[u][1 - cost2[u]]++;
}

void read_input() {
	cin >> n;
	for (int u = 0; u < n; u++) {
		char c;
		cin >> c;
		if (c == '(') {
			cost1[u]++;
			cost2[u]--;
		}
		else {
			cost1[u]--;
			cost2[u]++;
		}
	}
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		adj[--u].push_back(--v);
		adj[v].push_back(u);
	}
}

void write_output() {
	dfs(0, 0);
	cout << ans;
}

int main() {
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	read_input(), write_output();
	return 0;
}

Compilation message

zagrade.cpp: In function 'void dfs(int, int)':
zagrade.cpp:26:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
    for (auto [x, k]: mp1[v])
              ^
zagrade.cpp:29:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
    for (auto [x, k]: mp2[v])
              ^
zagrade.cpp:39:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
    for (auto [x, k]: mp1[v])
              ^
zagrade.cpp:41:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
    for (auto [x, k]: mp2[v])
              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 35712 KB Output is correct
2 Correct 26 ms 35712 KB Output is correct
3 Correct 25 ms 35680 KB Output is correct
4 Correct 25 ms 35712 KB Output is correct
5 Correct 26 ms 35712 KB Output is correct
6 Correct 27 ms 35712 KB Output is correct
7 Correct 26 ms 35708 KB Output is correct
8 Correct 26 ms 35712 KB Output is correct
9 Correct 25 ms 35712 KB Output is correct
10 Correct 25 ms 35704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 248 ms 112504 KB Output is correct
2 Correct 309 ms 118264 KB Output is correct
3 Correct 243 ms 112632 KB Output is correct
4 Correct 301 ms 116856 KB Output is correct
5 Correct 245 ms 112732 KB Output is correct
6 Correct 290 ms 114552 KB Output is correct
7 Correct 242 ms 112608 KB Output is correct
8 Correct 297 ms 114552 KB Output is correct
9 Correct 245 ms 112632 KB Output is correct
10 Correct 328 ms 119544 KB Output is correct
11 Correct 197 ms 112504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 35712 KB Output is correct
2 Correct 26 ms 35712 KB Output is correct
3 Correct 25 ms 35680 KB Output is correct
4 Correct 25 ms 35712 KB Output is correct
5 Correct 26 ms 35712 KB Output is correct
6 Correct 27 ms 35712 KB Output is correct
7 Correct 26 ms 35708 KB Output is correct
8 Correct 26 ms 35712 KB Output is correct
9 Correct 25 ms 35712 KB Output is correct
10 Correct 25 ms 35704 KB Output is correct
11 Correct 248 ms 112504 KB Output is correct
12 Correct 309 ms 118264 KB Output is correct
13 Correct 243 ms 112632 KB Output is correct
14 Correct 301 ms 116856 KB Output is correct
15 Correct 245 ms 112732 KB Output is correct
16 Correct 290 ms 114552 KB Output is correct
17 Correct 242 ms 112608 KB Output is correct
18 Correct 297 ms 114552 KB Output is correct
19 Correct 245 ms 112632 KB Output is correct
20 Correct 328 ms 119544 KB Output is correct
21 Correct 197 ms 112504 KB Output is correct
22 Correct 368 ms 63804 KB Output is correct
23 Correct 378 ms 64504 KB Output is correct
24 Correct 353 ms 64484 KB Output is correct
25 Correct 382 ms 64504 KB Output is correct
26 Correct 240 ms 79608 KB Output is correct
27 Correct 244 ms 71800 KB Output is correct
28 Correct 250 ms 68600 KB Output is correct
29 Correct 337 ms 119544 KB Output is correct
30 Correct 330 ms 119548 KB Output is correct
31 Correct 151 ms 65384 KB Output is correct
32 Correct 198 ms 112632 KB Output is correct