#include "race.h"
#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e9
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
int best_path(int N, int K, int H[][2], int L[]) {
vector<vll> adj; adj.resize(N);
vector<vlll> adjW; adjW.resize(N);
vector<bool> ignore; ignore.assign(N,0);
vll sz; sz.assign(N,0);
map<ll,vlll> mp;
ll ans = N+1;
RE(i,N-1) RE(j,2) adj [H[i][j]].push_back(H[i][!j]);
RE(i,N-1) RE(j,2) adjW[H[i][j]].push_back({H[i][!j], L[i]});
std::function<void(ll,ll)> dfsSize;
std::function<ll(ll,ll,ll)> dfsCentroid;
std::function<void(ll,ll,ll,ll,ll)> dfsMap;
std::function<void(ll)> centroidDecomp;
dfsSize = [&](ll u, ll p) {
sz[u] = 1;
FOR(v,adj[u]) {
if(ignore[v]) continue;
if(v == p) continue;
dfsSize(v,u);
sz[u] += sz[v];
}
};
dfsCentroid = [&](ll u, ll p, ll maxSize) {
FOR(v,adj[u]) {
if(ignore[v]) continue;
if(v == p) continue;
if(sz[v] > maxSize) return dfsCentroid(v,u,maxSize);
}
return u;
};
dfsMap = [&](ll u, ll p, ll root, ll len, ll depth) {
mp[depth].push_back({len,root});
FOR(v,adjW[u]) {
if(ignore[v.fi]) continue;
if(v.fi == p) continue;
dfsMap(v.fi,u,root,len+1,depth+v.se);
}
};
auto findCentroid = [&](ll u) {
dfsSize(u,-1);
return dfsCentroid(u,-1,sz[u]/2);
};
centroidDecomp = [&](ll u) {
ll cent = findCentroid(u);
mp.clear();
FOR(v,adjW[cent]) {
if(ignore[v.fi]) continue;
dfsMap(v.fi,cent,v.fi,1,v.se);
}
mp[0].push_back({0,-2});
FOR(p,mp) {
sort(all(p.se));
lll a={-1,-1}, b={-1,-1};
vlll newF;
FOR(np,p.se) {
if(a.se == np.se || b.se == np.se) continue;
if(a.fi == -1) {
a = np; newF.push_back(np);
} else if(b.fi == -1) {
b = np; newF.push_back(np);
}
}
p.se = newF;
}
FOR(p,mp) {
auto& f1 = p.se;
auto& f2 = mp[K - p.fi];
FOR(i,f1) FOR(j,f2) if(i.se != j.se) ans = min(ans, i.fi + j.fi);
}
// recursive
ignore[cent] = 1;
FOR(v,adj[cent]) {
if(ignore[v]) continue;
centroidDecomp(v);
}
};
centroidDecomp(0);
if(ans == N+1) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
4 ms |
460 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
4 ms |
588 KB |
Output is correct |
29 |
Correct |
4 ms |
588 KB |
Output is correct |
30 |
Correct |
4 ms |
592 KB |
Output is correct |
31 |
Correct |
4 ms |
588 KB |
Output is correct |
32 |
Correct |
4 ms |
592 KB |
Output is correct |
33 |
Correct |
5 ms |
588 KB |
Output is correct |
34 |
Correct |
5 ms |
588 KB |
Output is correct |
35 |
Correct |
4 ms |
588 KB |
Output is correct |
36 |
Correct |
4 ms |
588 KB |
Output is correct |
37 |
Correct |
4 ms |
588 KB |
Output is correct |
38 |
Correct |
4 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
500 ms |
18140 KB |
Output is correct |
20 |
Correct |
467 ms |
17976 KB |
Output is correct |
21 |
Correct |
448 ms |
17860 KB |
Output is correct |
22 |
Correct |
441 ms |
18124 KB |
Output is correct |
23 |
Correct |
599 ms |
19012 KB |
Output is correct |
24 |
Correct |
290 ms |
18148 KB |
Output is correct |
25 |
Correct |
401 ms |
25780 KB |
Output is correct |
26 |
Correct |
229 ms |
26160 KB |
Output is correct |
27 |
Correct |
670 ms |
40076 KB |
Output is correct |
28 |
Correct |
2344 ms |
88312 KB |
Output is correct |
29 |
Correct |
2228 ms |
87068 KB |
Output is correct |
30 |
Correct |
649 ms |
39876 KB |
Output is correct |
31 |
Correct |
669 ms |
39876 KB |
Output is correct |
32 |
Correct |
795 ms |
40004 KB |
Output is correct |
33 |
Correct |
940 ms |
37200 KB |
Output is correct |
34 |
Correct |
2113 ms |
70508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
4 ms |
460 KB |
Output is correct |
22 |
Correct |
4 ms |
588 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
4 ms |
588 KB |
Output is correct |
26 |
Correct |
4 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
460 KB |
Output is correct |
28 |
Correct |
4 ms |
588 KB |
Output is correct |
29 |
Correct |
4 ms |
588 KB |
Output is correct |
30 |
Correct |
4 ms |
592 KB |
Output is correct |
31 |
Correct |
4 ms |
588 KB |
Output is correct |
32 |
Correct |
4 ms |
592 KB |
Output is correct |
33 |
Correct |
5 ms |
588 KB |
Output is correct |
34 |
Correct |
5 ms |
588 KB |
Output is correct |
35 |
Correct |
4 ms |
588 KB |
Output is correct |
36 |
Correct |
4 ms |
588 KB |
Output is correct |
37 |
Correct |
4 ms |
588 KB |
Output is correct |
38 |
Correct |
4 ms |
588 KB |
Output is correct |
39 |
Correct |
500 ms |
18140 KB |
Output is correct |
40 |
Correct |
467 ms |
17976 KB |
Output is correct |
41 |
Correct |
448 ms |
17860 KB |
Output is correct |
42 |
Correct |
441 ms |
18124 KB |
Output is correct |
43 |
Correct |
599 ms |
19012 KB |
Output is correct |
44 |
Correct |
290 ms |
18148 KB |
Output is correct |
45 |
Correct |
401 ms |
25780 KB |
Output is correct |
46 |
Correct |
229 ms |
26160 KB |
Output is correct |
47 |
Correct |
670 ms |
40076 KB |
Output is correct |
48 |
Correct |
2344 ms |
88312 KB |
Output is correct |
49 |
Correct |
2228 ms |
87068 KB |
Output is correct |
50 |
Correct |
649 ms |
39876 KB |
Output is correct |
51 |
Correct |
669 ms |
39876 KB |
Output is correct |
52 |
Correct |
795 ms |
40004 KB |
Output is correct |
53 |
Correct |
940 ms |
37200 KB |
Output is correct |
54 |
Correct |
2113 ms |
70508 KB |
Output is correct |
55 |
Correct |
41 ms |
3032 KB |
Output is correct |
56 |
Correct |
31 ms |
2236 KB |
Output is correct |
57 |
Correct |
284 ms |
19772 KB |
Output is correct |
58 |
Correct |
82 ms |
18932 KB |
Output is correct |
59 |
Correct |
773 ms |
39484 KB |
Output is correct |
60 |
Correct |
2479 ms |
83284 KB |
Output is correct |
61 |
Correct |
733 ms |
43928 KB |
Output is correct |
62 |
Correct |
723 ms |
39904 KB |
Output is correct |
63 |
Correct |
861 ms |
39944 KB |
Output is correct |
64 |
Correct |
2613 ms |
55064 KB |
Output is correct |
65 |
Correct |
1982 ms |
72344 KB |
Output is correct |
66 |
Correct |
2529 ms |
85556 KB |
Output is correct |
67 |
Correct |
334 ms |
39136 KB |
Output is correct |
68 |
Correct |
1086 ms |
66916 KB |
Output is correct |
69 |
Correct |
1099 ms |
67756 KB |
Output is correct |
70 |
Correct |
1008 ms |
63984 KB |
Output is correct |