답안 #615551

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
615551 2022-07-31T10:33:42 Z valerikk Designated Cities (JOI19_designated_cities) C++17
23 / 100
2000 ms 35164 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, vector<long long> &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, 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.push_back(cur[i]);
	}
	return cur[0];
}

void solve(int u, long long bonus) {
	vector<pair<long long, int>> ch;
	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);
		}
	}
	{
		vector<long long> d;
		long long dd = go(u, d);
		d.push_back(dd);
		sort(rbegin(d), rend(d));
		long long cur = 0;
		ans[1] = max(ans[1], bonus);
		for (int i = 0; i < (int)d.size(); ++i) {
			cur += d[i];
			ans[i + 2] = max(ans[i + 2], bonus + cur);
		}
	}
	used[u] = true; 
	{
		vector<pair<long long, int>> d;
		for (auto [v, a, b] : e[u]) {
			vector<long long> cur_d;
			d.emplace_back(go(v, cur_d) + a, v);
			for (auto dd : cur_d) {
				d.emplace_back(dd, 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;
			}
		}
		if (f != -1) {
			long long cur = 0;
			for (int i = 0; i < (int)d.size(); ++i) {
				cur += d[i].first;
				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;
		dfs(v);
		solve(centroid(v, sz[v]), bonus);
		bonus -= delta;
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	long long sum = 0;
	for (int i = 0; i < n - 1; ++i) {
		int a, b, c, d;
		cin >> 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;
	cin >> q;
	while (q--) {
		int x;
		cin >> x;
		cout << ans[x] << "\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5024 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 4 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 1409 ms 25048 KB Output is correct
3 Correct 1630 ms 34252 KB Output is correct
4 Correct 1311 ms 24268 KB Output is correct
5 Execution timed out 2062 ms 26580 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5020 KB Output is correct
2 Correct 1463 ms 25668 KB Output is correct
3 Correct 1540 ms 35164 KB Output is correct
4 Correct 1366 ms 24360 KB Output is correct
5 Execution timed out 2077 ms 26744 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5024 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 4 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 5028 KB Output is correct
13 Correct 9 ms 5236 KB Output is correct
14 Correct 7 ms 5332 KB Output is correct
15 Correct 8 ms 5160 KB Output is correct
16 Correct 8 ms 5160 KB Output is correct
17 Correct 8 ms 5204 KB Output is correct
18 Correct 9 ms 5204 KB Output is correct
19 Correct 8 ms 5160 KB Output is correct
20 Correct 32 ms 5232 KB Output is correct
21 Correct 8 ms 5204 KB Output is correct
22 Correct 9 ms 5204 KB Output is correct
23 Correct 12 ms 5204 KB Output is correct
24 Correct 127 ms 5312 KB Output is correct
25 Correct 8 ms 5292 KB Output is correct
26 Correct 148 ms 5332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 1409 ms 25048 KB Output is correct
3 Correct 1630 ms 34252 KB Output is correct
4 Correct 1311 ms 24268 KB Output is correct
5 Execution timed out 2062 ms 26580 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5024 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 4 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 1409 ms 25048 KB Output is correct
14 Correct 1630 ms 34252 KB Output is correct
15 Correct 1311 ms 24268 KB Output is correct
16 Execution timed out 2062 ms 26580 KB Time limit exceeded
17 Halted 0 ms 0 KB -