QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#270598#2002. Racecciafrino0 146ms18012kbC++201.9kb2023-12-01 10:15:392023-12-01 10:15:40

Judging History

你现在查看的是最新测评结果

  • [2023-12-01 10:15:40]
  • 评测
  • 测评结果:0
  • 用时:146ms
  • 内存:18012kb
  • [2023-12-01 10:15:39]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int V = 2e5;

int H[V][2];
int L[V];

int best_path(int N, int K, int H[][2], int L[]) {
	const int INF = 1e9;
	int ans = INF;

	vector<vector<pair<int, int>>> adj(N);
	for (int i = 0; i+1 < N; ++i) {
		// cout << H[i][0] << ' ' << H[i][1] << endl;
		adj[H[i][0]].push_back({H[i][1], L[i]});
		adj[H[i][1]].push_back({H[i][0], L[i]});
	}

	vector<bool> deleted(N);
	vector<pair<int, int>> max_sub(N);
	auto dfs = [&](auto&& self, int cur, int prv) -> int {
		int sz = 1;
		max_sub[cur] = {0, -1};
		for (auto [nxt, weight] : adj[cur]) {
			if (nxt == prv || deleted[nxt]) continue;
			int cur_sz = self(self, nxt, cur);
			sz += cur_sz;
			max_sub[cur] = max(max_sub[cur], {cur_sz, nxt});
		}
		return sz;
	};

	auto get_dist = [&](auto& self, int cur, int prv, int64_t depth, int level, auto& path) -> void {
		path.push_back({depth, level});
		for (auto [nxt, weight] : adj[cur]) {
			if (nxt == prv || deleted[nxt]) continue;
			self(self, nxt, cur, depth + weight, level + 1, path);
		}
	};

	auto rec = [&](auto&& self, int cur) -> void {
		int total_sz = dfs(dfs, cur, -1);
		while (max_sub[cur].first * 2 > total_sz) cur = max_sub[cur].second;

		deleted[cur] = true;

		vector<int> vals(K + 1, INF); vals[0] = 0;
		for (auto [nxt, weight] : adj[cur]) {
			if (deleted[nxt]) continue;

			vector<pair<int64_t, int>> path; path.reserve(total_sz);
			get_dist(get_dist, cur, -1, 0, 0, path);
	
			for (auto [dist, level] : path) {
				if (dist > K) continue;
				ans = min(ans, vals[K - dist] + level);
			}

			for (auto [dist, level] : path) {
				if (dist > K) continue;
				vals[dist] = min(vals[dist], level);
			}
		}
		for (auto [nxt, weight] : adj[cur]) {
			if (deleted[nxt]) continue;
			self(self, nxt);
		}
	};

	rec(rec, 0);

	return (ans == INF ? -1 : ans);
}


詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 7768kb

input:

100 50
0 1 1
1 2 2
2 3 2
3 4 1
4 5 2
5 6 1
6 7 1
7 8 1
8 9 1
9 10 2
10 11 2
11 12 2
12 13 1
13 14 1
14 15 1
15 16 2
16 17 1
17 18 2
18 19 1
19 20 1
20 21 1
21 22 2
22 23 2
23 24 2
24 25 2
25 26 1
26 27 2
27 28 2
28 29 2
29 30 2
30 31 2
31 32 1
32 33 1
33 34 2
34 35 2
35 36 1
36 37 1
37 38 1
38 39 1
...

output:

Incorrect. Returned 28, Expected 30.

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #39:

score: 0
Wrong Answer
time: 146ms
memory: 18012kb

input:

100000 100
1 0 1
2 1 10
3 1 1
4 3 5
5 3 6
6 5 6
7 3 10
8 5 9
9 8 7
10 9 9
11 6 7
12 6 3
13 10 10
14 9 1
15 14 7
16 15 5
17 10 1
18 14 9
19 12 8
20 18 10
21 10 9
22 12 7
23 14 9
24 15 5
25 15 2
26 20 4
27 19 10
28 17 8
29 16 8
30 24 10
31 17 2
32 28 7
33 27 8
34 21 4
35 28 7
36 22 4
37 18 6
38 27 6
3...

output:

Incorrect. Returned 10, Expected 11.

Subtask #4:

score: 0
Skipped

Dependency #1:

0%