답안 #615572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
615572 2022-07-31T10:44:15 Z valerikk Designated Cities (JOI19_designated_cities) C++17
100 / 100
1271 ms 35884 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

struct Edge {
	int v, a, b;

	Edge() {}

	Edge(int v_, int a_, int b_) : v(v_), a(a_), b(b_) {}
};

int n;
vector<Edge> e[N];
bool used[N];
int sz[N];
long long ans[N];

void dfs(int u, int p = -1) {
	sz[u] = 1;
	for (auto [v, a, b] : e[u]) {
		if (!used[v] && v != p) {
			dfs(v, u);
			sz[u] += sz[v];
		}
	}
}

int centroid(int u, int all, int p = -1) {
	for (auto [v, a, b] : e[u]) {
		if (!used[v] && v != p && 2 * sz[v] > all) {
			return centroid(v, all, u);
		}
	}
	return u;
}

long long zhfs(int u, int p = -1) {
	long long res = 0;
	for (auto [v, a, b] : e[u]) {
		if (!used[v] && v != p) {
			res += zhfs(v, u);
			res += b;
		}
	}
	return res;
}

long long go(int u, int t, vector<pair<long long, int>> &d, int p = -1) {
	vector<long long> cur;
	for (auto [v, a, b] : e[u]) {
		if (!used[v] && v != p) {
			cur.push_back(go(v, t, d, u) + a);
		}
	}
	if (cur.empty()) return 0;
	swap(cur[0], cur[max_element(begin(cur), end(cur)) - begin(cur)]);
	for (int i = 1; i < (int)cur.size(); ++i) {
		d.emplace_back(cur[i], t);
	}
	return cur[0];
}

void solve(int u, long long bonus) {
	dfs(u);
	vector<pair<long long, int>> ch;
	ch.reserve(e[u].size());
	for (auto [v, a, b] : e[u]) {
		if (!used[v]) {
			long long z = zhfs(v, u);
			bonus += b;
			bonus += z;
			ch.emplace_back(a - z - b, v);
		}
	}
	ans[1] = max(ans[1], bonus);
	used[u] = true; 
	{
		vector<pair<long long, int>> d;
		d.reserve(sz[u]);
		for (auto [v, a, b] : e[u]) {
			if (!used[v]) d.emplace_back(go(v, v, d) + a, v);
		}	
		sort(rbegin(d), rend(d));
		int f = -1;
		for (int i = 1; i < (int)d.size(); ++i) {
			if (d[i].second != d[0].second) {
				f = i;
				break;
			}
		}
		long long cur = 0;
		for (int i = 0; i < (int)d.size(); ++i) {
			cur += d[i].first;
			ans[i + 2] = max(ans[i + 2], cur + bonus);
			if (f != -1) ans[i + 1 + (i < f)] = max(ans[i + 1 + (i < f)], cur + (i < f ? d[f].first : 0) + bonus);
		}
	}
	for (auto [delta, v] : ch) {
		bonus += delta;
		solve(centroid(v, sz[v]), bonus);
		bonus -= delta;
	}
}

int main() {
	scanf("%d", &n);
	long long sum = 0;
	for (int i = 0; i < n - 1; ++i) {
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		--a, --b;
		e[a].emplace_back(b, c, d);
		e[b].emplace_back(a, d, c);
		sum += c;
		sum += d;
	}
	dfs(0);
	solve(centroid(0, n), 0);
	for (int i = 1; i <= n; ++i) {
		ans[i] = sum - ans[i];
	}
	for (int i = 2; i <= n; ++i) {
		ans[i] = min(ans[i], ans[i - 1]);
	}
	int q;
	scanf("%d", &q);
	while (q--) {
		int x;
		scanf("%d", &x);
		cout << ans[x] << "\n";
	}
	return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
designated_cities.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |   scanf("%d%d%d%d", &a, &b, &c, &d);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:129:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
designated_cities.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |   scanf("%d", &x);
      |   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4992 KB Output is correct
2 Correct 4 ms 5012 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 658 ms 16596 KB Output is correct
3 Correct 1116 ms 27916 KB Output is correct
4 Correct 793 ms 16472 KB Output is correct
5 Correct 324 ms 16512 KB Output is correct
6 Correct 974 ms 18260 KB Output is correct
7 Correct 292 ms 18536 KB Output is correct
8 Correct 1271 ms 27544 KB Output is correct
9 Correct 214 ms 22200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 933 ms 16440 KB Output is correct
3 Correct 1161 ms 28716 KB Output is correct
4 Correct 829 ms 16428 KB Output is correct
5 Correct 383 ms 16468 KB Output is correct
6 Correct 972 ms 25228 KB Output is correct
7 Correct 197 ms 27632 KB Output is correct
8 Correct 1088 ms 31196 KB Output is correct
9 Correct 184 ms 28276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4992 KB Output is correct
2 Correct 4 ms 5012 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 5 ms 5060 KB Output is correct
14 Correct 6 ms 5224 KB Output is correct
15 Correct 5 ms 5076 KB Output is correct
16 Correct 6 ms 5176 KB Output is correct
17 Correct 6 ms 5076 KB Output is correct
18 Correct 8 ms 5136 KB Output is correct
19 Correct 6 ms 5128 KB Output is correct
20 Correct 5 ms 5076 KB Output is correct
21 Correct 8 ms 5152 KB Output is correct
22 Correct 8 ms 5076 KB Output is correct
23 Correct 6 ms 5156 KB Output is correct
24 Correct 4 ms 5204 KB Output is correct
25 Correct 6 ms 5204 KB Output is correct
26 Correct 4 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 658 ms 16596 KB Output is correct
3 Correct 1116 ms 27916 KB Output is correct
4 Correct 793 ms 16472 KB Output is correct
5 Correct 324 ms 16512 KB Output is correct
6 Correct 974 ms 18260 KB Output is correct
7 Correct 292 ms 18536 KB Output is correct
8 Correct 1271 ms 27544 KB Output is correct
9 Correct 214 ms 22200 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 933 ms 16440 KB Output is correct
12 Correct 1161 ms 28716 KB Output is correct
13 Correct 829 ms 16428 KB Output is correct
14 Correct 383 ms 16468 KB Output is correct
15 Correct 972 ms 25228 KB Output is correct
16 Correct 197 ms 27632 KB Output is correct
17 Correct 1088 ms 31196 KB Output is correct
18 Correct 184 ms 28276 KB Output is correct
19 Correct 3 ms 5008 KB Output is correct
20 Correct 748 ms 22956 KB Output is correct
21 Correct 1070 ms 35248 KB Output is correct
22 Correct 782 ms 21488 KB Output is correct
23 Correct 734 ms 22960 KB Output is correct
24 Correct 775 ms 21856 KB Output is correct
25 Correct 708 ms 22928 KB Output is correct
26 Correct 783 ms 22188 KB Output is correct
27 Correct 341 ms 22864 KB Output is correct
28 Correct 893 ms 24868 KB Output is correct
29 Correct 730 ms 22940 KB Output is correct
30 Correct 721 ms 23252 KB Output is correct
31 Correct 235 ms 25688 KB Output is correct
32 Correct 1010 ms 31532 KB Output is correct
33 Correct 181 ms 28764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4992 KB Output is correct
2 Correct 4 ms 5012 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 658 ms 16596 KB Output is correct
14 Correct 1116 ms 27916 KB Output is correct
15 Correct 793 ms 16472 KB Output is correct
16 Correct 324 ms 16512 KB Output is correct
17 Correct 974 ms 18260 KB Output is correct
18 Correct 292 ms 18536 KB Output is correct
19 Correct 1271 ms 27544 KB Output is correct
20 Correct 214 ms 22200 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 933 ms 16440 KB Output is correct
23 Correct 1161 ms 28716 KB Output is correct
24 Correct 829 ms 16428 KB Output is correct
25 Correct 383 ms 16468 KB Output is correct
26 Correct 972 ms 25228 KB Output is correct
27 Correct 197 ms 27632 KB Output is correct
28 Correct 1088 ms 31196 KB Output is correct
29 Correct 184 ms 28276 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 5 ms 5060 KB Output is correct
32 Correct 6 ms 5224 KB Output is correct
33 Correct 5 ms 5076 KB Output is correct
34 Correct 6 ms 5176 KB Output is correct
35 Correct 6 ms 5076 KB Output is correct
36 Correct 8 ms 5136 KB Output is correct
37 Correct 6 ms 5128 KB Output is correct
38 Correct 5 ms 5076 KB Output is correct
39 Correct 8 ms 5152 KB Output is correct
40 Correct 8 ms 5076 KB Output is correct
41 Correct 6 ms 5156 KB Output is correct
42 Correct 4 ms 5204 KB Output is correct
43 Correct 6 ms 5204 KB Output is correct
44 Correct 4 ms 5204 KB Output is correct
45 Correct 3 ms 5008 KB Output is correct
46 Correct 748 ms 22956 KB Output is correct
47 Correct 1070 ms 35248 KB Output is correct
48 Correct 782 ms 21488 KB Output is correct
49 Correct 734 ms 22960 KB Output is correct
50 Correct 775 ms 21856 KB Output is correct
51 Correct 708 ms 22928 KB Output is correct
52 Correct 783 ms 22188 KB Output is correct
53 Correct 341 ms 22864 KB Output is correct
54 Correct 893 ms 24868 KB Output is correct
55 Correct 730 ms 22940 KB Output is correct
56 Correct 721 ms 23252 KB Output is correct
57 Correct 235 ms 25688 KB Output is correct
58 Correct 1010 ms 31532 KB Output is correct
59 Correct 181 ms 28764 KB Output is correct
60 Correct 3 ms 4948 KB Output is correct
61 Correct 800 ms 25504 KB Output is correct
62 Correct 1129 ms 35884 KB Output is correct
63 Correct 761 ms 24048 KB Output is correct
64 Correct 804 ms 25456 KB Output is correct
65 Correct 813 ms 24104 KB Output is correct
66 Correct 812 ms 25424 KB Output is correct
67 Correct 772 ms 24144 KB Output is correct
68 Correct 387 ms 24752 KB Output is correct
69 Correct 913 ms 27560 KB Output is correct
70 Correct 842 ms 25372 KB Output is correct
71 Correct 744 ms 25432 KB Output is correct
72 Correct 306 ms 25468 KB Output is correct
73 Correct 1041 ms 33768 KB Output is correct
74 Correct 213 ms 28920 KB Output is correct