#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN = 1e6+5, M = 1e9+7;
int n, k, sz[mxN], mxtree[mxN], ans;
vector<pair<int, int>> adj[mxN];
vector<int> new_vis;
bool used[mxN];
int dfs(int u, int p = -1){
sz[u] = 1;
for(pair<int, int> v : adj[u]){
if(v.second != p && !used[v.second]){
sz[u] += dfs(v.second, u);
}
}
return sz[u];
}
int get_centroid(int u, int ms, int p = -1){ // u = node, ms = size of the current tree delimited by previous centroids, p = parent
for(pair<int, int> v : adj[u]){
if(v.second != p && !used[v.second]){
if(sz[v.second]*2 > ms){
return get_centroid(v.second, ms, u);
}
}
}
return u;
}
int flag[mxN], stamp[mxN];
int timestamp;
void solve(int u){
++timestamp;
int sz_curr_tree = dfs(u);
int centroid = get_centroid(u, sz_curr_tree);
used[centroid] = 1;
flag[0] = 0;
stamp[0] = timestamp;
// do something with the centroid
for(pair<int, int> v : adj[centroid]){
if(used[v.second]) continue;
if(v.first > k) continue;
queue<int> q, d, p, l;
vector<int> cd1, cd2;
q.push(v.second);
d.push(v.first);
l.push(1);
p.push(centroid);
while(q.size()){
int qf = q.front();
int df = d.front();
int lf = l.front();
int pf = p.front();
cd1.push_back(df);
cd2.push_back(lf);
if(stamp[k-df] == timestamp) ans = min(flag[k-df] + lf, ans);
q.pop();
d.pop();
l.pop();
p.pop();
for(pair<int, int> v2 : adj[qf]){
int pos = v2.second;
if(used[pos] || pos == pf) continue;
if(df+v2.first > k) continue;
q.push(v2.second);
d.push(df+v2.first);
l.push(lf+1);
p.push(qf);
}
}
for(int i = 0; i<cd1.size(); ++i){
if(stamp[cd1[i]] != timestamp){
flag[cd1[i]] = cd2[i];
stamp[cd1[i]] = timestamp;
}else{
flag[cd1[i]] = min(flag[cd1[i]], cd2[i]);
}
}
}
for(pair<int, int> v : adj[centroid]){
if(used[v.second]) continue;
solve(v.second);
}
// end that something with the centroid xdd
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
for(int i = 0; i<N-1; ++i){
adj[H[i][0]].push_back({L[i], H[i][1]});
adj[H[i][1]].push_back({L[i], H[i][0]});
}
ans = 1e9;
solve(0);
if(ans > 1e8) return -1;
return ans;
}
/*
int main(){
int _N, _K;
cin >> _N >> _K;
int _H[_N][2], _L[_N];
for(int i = 0; i<_N-1; ++i){
cin >> _H[i][0] >> _H[i][1];
}
for(int i = 0; i<_N-1; ++i){
cin >> _L[i];
}
cout << best_path(_N, _K, _H, _L) << "\n";
}
*/
//https://oj.uz/problem/view/IOI11_race
Compilation message
race.cpp: In function 'void solve(int)':
race.cpp:76:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 0; i<cd1.size(); ++i){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23796 KB |
Output is correct |
4 |
Correct |
14 ms |
23752 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23792 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23816 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23844 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23796 KB |
Output is correct |
4 |
Correct |
14 ms |
23752 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23792 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23816 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23844 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23772 KB |
Output is correct |
19 |
Correct |
12 ms |
23768 KB |
Output is correct |
20 |
Correct |
14 ms |
23840 KB |
Output is correct |
21 |
Correct |
15 ms |
23892 KB |
Output is correct |
22 |
Correct |
17 ms |
28812 KB |
Output is correct |
23 |
Correct |
17 ms |
28116 KB |
Output is correct |
24 |
Correct |
18 ms |
29624 KB |
Output is correct |
25 |
Correct |
16 ms |
27348 KB |
Output is correct |
26 |
Correct |
16 ms |
26516 KB |
Output is correct |
27 |
Correct |
15 ms |
23880 KB |
Output is correct |
28 |
Correct |
15 ms |
25428 KB |
Output is correct |
29 |
Correct |
14 ms |
26324 KB |
Output is correct |
30 |
Correct |
16 ms |
26700 KB |
Output is correct |
31 |
Correct |
15 ms |
27476 KB |
Output is correct |
32 |
Correct |
17 ms |
27608 KB |
Output is correct |
33 |
Correct |
18 ms |
29148 KB |
Output is correct |
34 |
Correct |
18 ms |
28244 KB |
Output is correct |
35 |
Correct |
18 ms |
29652 KB |
Output is correct |
36 |
Correct |
20 ms |
30304 KB |
Output is correct |
37 |
Correct |
17 ms |
27604 KB |
Output is correct |
38 |
Correct |
19 ms |
26196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23796 KB |
Output is correct |
4 |
Correct |
14 ms |
23752 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23792 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23816 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23844 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23772 KB |
Output is correct |
19 |
Correct |
158 ms |
29248 KB |
Output is correct |
20 |
Correct |
153 ms |
29264 KB |
Output is correct |
21 |
Correct |
177 ms |
29408 KB |
Output is correct |
22 |
Correct |
157 ms |
29512 KB |
Output is correct |
23 |
Correct |
109 ms |
29516 KB |
Output is correct |
24 |
Correct |
88 ms |
29264 KB |
Output is correct |
25 |
Correct |
157 ms |
31932 KB |
Output is correct |
26 |
Correct |
155 ms |
35520 KB |
Output is correct |
27 |
Correct |
217 ms |
34716 KB |
Output is correct |
28 |
Correct |
328 ms |
45792 KB |
Output is correct |
29 |
Correct |
357 ms |
44956 KB |
Output is correct |
30 |
Correct |
219 ms |
34688 KB |
Output is correct |
31 |
Correct |
218 ms |
34612 KB |
Output is correct |
32 |
Correct |
269 ms |
34620 KB |
Output is correct |
33 |
Correct |
281 ms |
33580 KB |
Output is correct |
34 |
Correct |
214 ms |
33560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23796 KB |
Output is correct |
4 |
Correct |
14 ms |
23752 KB |
Output is correct |
5 |
Correct |
13 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23840 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
14 ms |
23792 KB |
Output is correct |
9 |
Correct |
13 ms |
23764 KB |
Output is correct |
10 |
Correct |
17 ms |
23764 KB |
Output is correct |
11 |
Correct |
13 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23816 KB |
Output is correct |
13 |
Correct |
13 ms |
23764 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23844 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23772 KB |
Output is correct |
19 |
Correct |
12 ms |
23768 KB |
Output is correct |
20 |
Correct |
14 ms |
23840 KB |
Output is correct |
21 |
Correct |
15 ms |
23892 KB |
Output is correct |
22 |
Correct |
17 ms |
28812 KB |
Output is correct |
23 |
Correct |
17 ms |
28116 KB |
Output is correct |
24 |
Correct |
18 ms |
29624 KB |
Output is correct |
25 |
Correct |
16 ms |
27348 KB |
Output is correct |
26 |
Correct |
16 ms |
26516 KB |
Output is correct |
27 |
Correct |
15 ms |
23880 KB |
Output is correct |
28 |
Correct |
15 ms |
25428 KB |
Output is correct |
29 |
Correct |
14 ms |
26324 KB |
Output is correct |
30 |
Correct |
16 ms |
26700 KB |
Output is correct |
31 |
Correct |
15 ms |
27476 KB |
Output is correct |
32 |
Correct |
17 ms |
27608 KB |
Output is correct |
33 |
Correct |
18 ms |
29148 KB |
Output is correct |
34 |
Correct |
18 ms |
28244 KB |
Output is correct |
35 |
Correct |
18 ms |
29652 KB |
Output is correct |
36 |
Correct |
20 ms |
30304 KB |
Output is correct |
37 |
Correct |
17 ms |
27604 KB |
Output is correct |
38 |
Correct |
19 ms |
26196 KB |
Output is correct |
39 |
Correct |
158 ms |
29248 KB |
Output is correct |
40 |
Correct |
153 ms |
29264 KB |
Output is correct |
41 |
Correct |
177 ms |
29408 KB |
Output is correct |
42 |
Correct |
157 ms |
29512 KB |
Output is correct |
43 |
Correct |
109 ms |
29516 KB |
Output is correct |
44 |
Correct |
88 ms |
29264 KB |
Output is correct |
45 |
Correct |
157 ms |
31932 KB |
Output is correct |
46 |
Correct |
155 ms |
35520 KB |
Output is correct |
47 |
Correct |
217 ms |
34716 KB |
Output is correct |
48 |
Correct |
328 ms |
45792 KB |
Output is correct |
49 |
Correct |
357 ms |
44956 KB |
Output is correct |
50 |
Correct |
219 ms |
34688 KB |
Output is correct |
51 |
Correct |
218 ms |
34612 KB |
Output is correct |
52 |
Correct |
269 ms |
34620 KB |
Output is correct |
53 |
Correct |
281 ms |
33580 KB |
Output is correct |
54 |
Correct |
214 ms |
33560 KB |
Output is correct |
55 |
Correct |
24 ms |
24464 KB |
Output is correct |
56 |
Correct |
24 ms |
24460 KB |
Output is correct |
57 |
Correct |
168 ms |
30132 KB |
Output is correct |
58 |
Correct |
78 ms |
29408 KB |
Output is correct |
59 |
Correct |
166 ms |
36196 KB |
Output is correct |
60 |
Correct |
517 ms |
51024 KB |
Output is correct |
61 |
Correct |
264 ms |
34640 KB |
Output is correct |
62 |
Correct |
254 ms |
34744 KB |
Output is correct |
63 |
Correct |
314 ms |
34764 KB |
Output is correct |
64 |
Correct |
457 ms |
37464 KB |
Output is correct |
65 |
Correct |
271 ms |
34636 KB |
Output is correct |
66 |
Correct |
475 ms |
50660 KB |
Output is correct |
67 |
Correct |
197 ms |
34964 KB |
Output is correct |
68 |
Correct |
293 ms |
40136 KB |
Output is correct |
69 |
Correct |
315 ms |
40336 KB |
Output is correct |
70 |
Correct |
307 ms |
39620 KB |
Output is correct |