/////////////////////////////// _LeMur_
#define _CRT_SECURE_NO_WARNINGS
#include "race.h"
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
const int N = 200005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
vector < pair<int, int> > g[N];
int answ = -1;
bool used[N];
int sz[N];
int nn = 0;
int k;
void dfs_sz(int v, int p) {
nn++;
sz[v] = 1;
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i].first;
if (to == p) continue;
if (used[to]) continue;
dfs_sz(to, v);
sz[v] += sz[to];
}
}
int find_center(int v, int p) {
int mx_sz = -1, gag;
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i].first;
if (to == p) continue;
if (used[to]) continue;
if (sz[to] > mx_sz) {
mx_sz = sz[to];
gag = to;
}
}
if (mx_sz * 2 <= nn) return v;
return find_center(gag, v);
}
int find_centroid(int v) {
nn = 0;
dfs_sz(v, -1);
int center = find_center(v, -1);
dfs_sz(center, -1);
return center;
}
vector < pair<int, int> > mas;
map <int, int> mp;
void update(int &answ, int x) {
if (answ == -1 || x < answ) {
answ = x;
}
}
void solve(int v, int p, int L, int r) {
if (p != -1) {
mas.push_back(make_pair(L, r));
}
for (int i = 0; i < (int)g[v].size(); i++) {
int to = g[v][i].first;
int len = g[v][i].second;
if (to == p || used[to]) continue;
solve(to, v, L + len, r + 1);
if (p == -1) {
for (int j = 0; j < (int)mas.size(); j++) {
int L = mas[j].first;
int d = mas[j].second;
if (L > k) continue;
if (L == k) {
update(answ, d);
} else {
if (mp.find(k - L) != mp.end()) {
update(answ, d + mp[k - L]);
}
}
}
for (int j = 0; j < (int)mas.size(); j++) {
int L = mas[j].first;
int d = mas[j].second;
if (L > k) continue;
if (mp.find(L) != mp.end()) {
mp[L] = min(mp[L], d);
} else {
mp[L] = d;
}
}
mas.clear();
}
}
}
void centroid_decomposition() {
queue <int> q;
q.push(1);
while (!q.empty()) {
int v = q.front();
q.pop();
int center = find_centroid(v);
mp.clear();
solve(center, -1, 0, 0);
used[center] = true;
for (int i = 0; i < (int)g[center].size(); i++) {
int to = g[center][i].first;
if (used[to]) continue;
if (sz[to] == 1) continue;
q.push(to);
}
}
}
int best_path(int n, int K, int h[][2], int l[]) {
k = K;
for (int i = 0; i < n - 1; i++) {
int a = h[i][0];
int b = h[i][1];
int c = l[i];
g[a].push_back(make_pair(b, c));
g[b].push_back(make_pair(a, c));
}
centroid_decomposition();
return answ;
}
Compilation message
race.cpp: In function 'int find_center(int, int)':
race.cpp:55:5: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | if (to == p) continue;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
5008 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
5008 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5020 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5120 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
5 ms |
5148 KB |
Output is correct |
33 |
Correct |
5 ms |
5088 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
5 ms |
5068 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
5008 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
314 ms |
12248 KB |
Output is correct |
20 |
Correct |
357 ms |
12276 KB |
Output is correct |
21 |
Correct |
304 ms |
12228 KB |
Output is correct |
22 |
Correct |
314 ms |
12244 KB |
Output is correct |
23 |
Correct |
180 ms |
12560 KB |
Output is correct |
24 |
Correct |
103 ms |
12176 KB |
Output is correct |
25 |
Correct |
222 ms |
18732 KB |
Output is correct |
26 |
Correct |
157 ms |
19496 KB |
Output is correct |
27 |
Correct |
370 ms |
18844 KB |
Output is correct |
28 |
Correct |
629 ms |
33344 KB |
Output is correct |
29 |
Correct |
700 ms |
32808 KB |
Output is correct |
30 |
Correct |
364 ms |
18884 KB |
Output is correct |
31 |
Correct |
339 ms |
18836 KB |
Output is correct |
32 |
Correct |
467 ms |
18988 KB |
Output is correct |
33 |
Correct |
545 ms |
18740 KB |
Output is correct |
34 |
Correct |
444 ms |
19516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
5008 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
15 |
Correct |
4 ms |
4940 KB |
Output is correct |
16 |
Correct |
4 ms |
5008 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5020 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
5 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
5 ms |
5120 KB |
Output is correct |
30 |
Correct |
5 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
5 ms |
5148 KB |
Output is correct |
33 |
Correct |
5 ms |
5088 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
5 ms |
5068 KB |
Output is correct |
37 |
Correct |
5 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
314 ms |
12248 KB |
Output is correct |
40 |
Correct |
357 ms |
12276 KB |
Output is correct |
41 |
Correct |
304 ms |
12228 KB |
Output is correct |
42 |
Correct |
314 ms |
12244 KB |
Output is correct |
43 |
Correct |
180 ms |
12560 KB |
Output is correct |
44 |
Correct |
103 ms |
12176 KB |
Output is correct |
45 |
Correct |
222 ms |
18732 KB |
Output is correct |
46 |
Correct |
157 ms |
19496 KB |
Output is correct |
47 |
Correct |
370 ms |
18844 KB |
Output is correct |
48 |
Correct |
629 ms |
33344 KB |
Output is correct |
49 |
Correct |
700 ms |
32808 KB |
Output is correct |
50 |
Correct |
364 ms |
18884 KB |
Output is correct |
51 |
Correct |
339 ms |
18836 KB |
Output is correct |
52 |
Correct |
467 ms |
18988 KB |
Output is correct |
53 |
Correct |
545 ms |
18740 KB |
Output is correct |
54 |
Correct |
444 ms |
19516 KB |
Output is correct |
55 |
Correct |
25 ms |
5836 KB |
Output is correct |
56 |
Correct |
20 ms |
5768 KB |
Output is correct |
57 |
Correct |
168 ms |
12496 KB |
Output is correct |
58 |
Correct |
63 ms |
11524 KB |
Output is correct |
59 |
Correct |
333 ms |
22888 KB |
Output is correct |
60 |
Correct |
1170 ms |
40940 KB |
Output is correct |
61 |
Correct |
428 ms |
19140 KB |
Output is correct |
62 |
Correct |
406 ms |
19056 KB |
Output is correct |
63 |
Correct |
599 ms |
19092 KB |
Output is correct |
64 |
Correct |
1366 ms |
24496 KB |
Output is correct |
65 |
Correct |
674 ms |
20620 KB |
Output is correct |
66 |
Correct |
962 ms |
32936 KB |
Output is correct |
67 |
Correct |
142 ms |
19472 KB |
Output is correct |
68 |
Correct |
671 ms |
24072 KB |
Output is correct |
69 |
Correct |
643 ms |
24140 KB |
Output is correct |
70 |
Correct |
558 ms |
23240 KB |
Output is correct |