#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define fi first
#define se second
// subtask 1,2,3
int const n=2e5;
unordered_map<int,int> adj[n];
int sub[n];
int const INF=1e9;
int mn=INF;
int k;
int getsub(int now, int par)
{
sub[now]=1;
trav(chi,adj[now])
{
if(chi.fi==par)continue;
sub[now]+=getsub(chi.fi,now);
}
return sub[now];
}
int centroid(int now, int par, int siz)
{
trav(chi,adj[now])
{
if(chi.fi==par)continue;
if(sub[chi.fi]>siz/2)return centroid(chi.fi,now,siz);
}
return now;
}
int const k_=1e6+3;
pair<pii,pii> path[k_];
vi pos;
bitset<k_> got;
void dfs(int now, int par, int cur, int id, int dep)
{
if(cur>k)return;
if(!got[cur])
{
pos.pb(cur);
got[cur]=1;
path[cur] = {{-1,INF},{-1,INF}};
}
if(path[cur].fi.fi==id)
{
path[cur].fi.se = min(path[cur].fi.se,dep);
}
else if(path[cur].se.fi==id)
{
path[cur].se.se = min(path[cur].se.se,dep);
if(path[cur].se.se < path[cur].fi.se)
{
swap(path[cur].se, path[cur].fi);
}
}
else if(dep < path[cur].fi.se)
{
path[cur].se = path[cur].fi;
path[cur].fi = {id,dep};
}
else if(dep < path[cur].se.se)
{
path[cur].se = {id,dep};
}
trav(chi,adj[now])
{
if(chi.fi==par)continue;
dfs(chi.fi,now,cur+chi.se,id,dep+1);
}
}
void solve(int now)
{
pos.clear();
trav(chi,adj[now])
{
dfs(chi.fi,now,chi.se,chi.fi,1);
}
pos.pb(0);
path[0].fi={now,0};
got[0]=1;
trav(i,pos)
{
if(!got[k-i])continue;
if(path[i].fi.fi==path[k-i].fi.fi)
{
if(path[i].se.fi!=-1)
{
mn=min(mn,path[i].se.se+path[k-i].fi.se);
}
if(path[k-i].se.fi!=-1)
{
mn=min(mn,path[i].fi.se+path[k-i].se.se);
}
}
else
{
mn=min(mn,path[i].fi.se+path[k-i].fi.se);
}
}
trav(e,pos)
{
got[e]=0;
}
}
void decomp(int now)
{
int siz = getsub(now,-1);
int u = centroid(now,-1,siz);
solve(u);
trav(chi,adj[u])
{
adj[chi.fi].erase(u);
decomp(chi.fi);
}
}
int best_path(int N, int K, int H[][2], int L[]){
k=K;
F0R(i,N-1)
{
int u = H[i][0];
int v = H[i][1];
adj[u].insert({v,L[i]});
adj[v].insert({u,L[i]});
}
decomp(0);
if(mn==INF)return -1;
return mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11304 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
7 ms |
11212 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
9 ms |
11212 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
8 ms |
11212 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11200 KB |
Output is correct |
10 |
Correct |
8 ms |
11240 KB |
Output is correct |
11 |
Correct |
7 ms |
11212 KB |
Output is correct |
12 |
Correct |
8 ms |
11212 KB |
Output is correct |
13 |
Correct |
7 ms |
11212 KB |
Output is correct |
14 |
Correct |
7 ms |
11212 KB |
Output is correct |
15 |
Correct |
7 ms |
11212 KB |
Output is correct |
16 |
Correct |
7 ms |
11328 KB |
Output is correct |
17 |
Correct |
7 ms |
11300 KB |
Output is correct |
18 |
Correct |
8 ms |
11292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11304 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
7 ms |
11212 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
9 ms |
11212 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
8 ms |
11212 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11200 KB |
Output is correct |
10 |
Correct |
8 ms |
11240 KB |
Output is correct |
11 |
Correct |
7 ms |
11212 KB |
Output is correct |
12 |
Correct |
8 ms |
11212 KB |
Output is correct |
13 |
Correct |
7 ms |
11212 KB |
Output is correct |
14 |
Correct |
7 ms |
11212 KB |
Output is correct |
15 |
Correct |
7 ms |
11212 KB |
Output is correct |
16 |
Correct |
7 ms |
11328 KB |
Output is correct |
17 |
Correct |
7 ms |
11300 KB |
Output is correct |
18 |
Correct |
8 ms |
11292 KB |
Output is correct |
19 |
Correct |
7 ms |
11212 KB |
Output is correct |
20 |
Correct |
8 ms |
11212 KB |
Output is correct |
21 |
Correct |
10 ms |
11468 KB |
Output is correct |
22 |
Correct |
11 ms |
15452 KB |
Output is correct |
23 |
Correct |
10 ms |
14924 KB |
Output is correct |
24 |
Correct |
11 ms |
16680 KB |
Output is correct |
25 |
Correct |
11 ms |
16968 KB |
Output is correct |
26 |
Correct |
10 ms |
14924 KB |
Output is correct |
27 |
Correct |
8 ms |
11468 KB |
Output is correct |
28 |
Correct |
10 ms |
14540 KB |
Output is correct |
29 |
Correct |
10 ms |
15820 KB |
Output is correct |
30 |
Correct |
12 ms |
16432 KB |
Output is correct |
31 |
Correct |
11 ms |
17100 KB |
Output is correct |
32 |
Correct |
11 ms |
17184 KB |
Output is correct |
33 |
Correct |
13 ms |
19604 KB |
Output is correct |
34 |
Correct |
11 ms |
17956 KB |
Output is correct |
35 |
Correct |
14 ms |
18984 KB |
Output is correct |
36 |
Correct |
11 ms |
19404 KB |
Output is correct |
37 |
Correct |
10 ms |
16584 KB |
Output is correct |
38 |
Correct |
10 ms |
15180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11304 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
7 ms |
11212 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
9 ms |
11212 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
8 ms |
11212 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11200 KB |
Output is correct |
10 |
Correct |
8 ms |
11240 KB |
Output is correct |
11 |
Correct |
7 ms |
11212 KB |
Output is correct |
12 |
Correct |
8 ms |
11212 KB |
Output is correct |
13 |
Correct |
7 ms |
11212 KB |
Output is correct |
14 |
Correct |
7 ms |
11212 KB |
Output is correct |
15 |
Correct |
7 ms |
11212 KB |
Output is correct |
16 |
Correct |
7 ms |
11328 KB |
Output is correct |
17 |
Correct |
7 ms |
11300 KB |
Output is correct |
18 |
Correct |
8 ms |
11292 KB |
Output is correct |
19 |
Correct |
208 ms |
30060 KB |
Output is correct |
20 |
Correct |
237 ms |
30048 KB |
Output is correct |
21 |
Correct |
218 ms |
30052 KB |
Output is correct |
22 |
Correct |
238 ms |
30020 KB |
Output is correct |
23 |
Correct |
147 ms |
30128 KB |
Output is correct |
24 |
Correct |
114 ms |
30020 KB |
Output is correct |
25 |
Correct |
194 ms |
32536 KB |
Output is correct |
26 |
Correct |
127 ms |
34752 KB |
Output is correct |
27 |
Correct |
354 ms |
49952 KB |
Output is correct |
28 |
Correct |
442 ms |
58120 KB |
Output is correct |
29 |
Correct |
434 ms |
57504 KB |
Output is correct |
30 |
Correct |
350 ms |
49948 KB |
Output is correct |
31 |
Correct |
332 ms |
49896 KB |
Output is correct |
32 |
Correct |
478 ms |
49860 KB |
Output is correct |
33 |
Correct |
368 ms |
48780 KB |
Output is correct |
34 |
Correct |
342 ms |
48676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11304 KB |
Output is correct |
2 |
Correct |
7 ms |
11212 KB |
Output is correct |
3 |
Correct |
7 ms |
11212 KB |
Output is correct |
4 |
Correct |
7 ms |
11212 KB |
Output is correct |
5 |
Correct |
9 ms |
11212 KB |
Output is correct |
6 |
Correct |
7 ms |
11212 KB |
Output is correct |
7 |
Correct |
8 ms |
11212 KB |
Output is correct |
8 |
Correct |
7 ms |
11212 KB |
Output is correct |
9 |
Correct |
8 ms |
11200 KB |
Output is correct |
10 |
Correct |
8 ms |
11240 KB |
Output is correct |
11 |
Correct |
7 ms |
11212 KB |
Output is correct |
12 |
Correct |
8 ms |
11212 KB |
Output is correct |
13 |
Correct |
7 ms |
11212 KB |
Output is correct |
14 |
Correct |
7 ms |
11212 KB |
Output is correct |
15 |
Correct |
7 ms |
11212 KB |
Output is correct |
16 |
Correct |
7 ms |
11328 KB |
Output is correct |
17 |
Correct |
7 ms |
11300 KB |
Output is correct |
18 |
Correct |
8 ms |
11292 KB |
Output is correct |
19 |
Correct |
7 ms |
11212 KB |
Output is correct |
20 |
Correct |
8 ms |
11212 KB |
Output is correct |
21 |
Correct |
10 ms |
11468 KB |
Output is correct |
22 |
Correct |
11 ms |
15452 KB |
Output is correct |
23 |
Correct |
10 ms |
14924 KB |
Output is correct |
24 |
Correct |
11 ms |
16680 KB |
Output is correct |
25 |
Correct |
11 ms |
16968 KB |
Output is correct |
26 |
Correct |
10 ms |
14924 KB |
Output is correct |
27 |
Correct |
8 ms |
11468 KB |
Output is correct |
28 |
Correct |
10 ms |
14540 KB |
Output is correct |
29 |
Correct |
10 ms |
15820 KB |
Output is correct |
30 |
Correct |
12 ms |
16432 KB |
Output is correct |
31 |
Correct |
11 ms |
17100 KB |
Output is correct |
32 |
Correct |
11 ms |
17184 KB |
Output is correct |
33 |
Correct |
13 ms |
19604 KB |
Output is correct |
34 |
Correct |
11 ms |
17956 KB |
Output is correct |
35 |
Correct |
14 ms |
18984 KB |
Output is correct |
36 |
Correct |
11 ms |
19404 KB |
Output is correct |
37 |
Correct |
10 ms |
16584 KB |
Output is correct |
38 |
Correct |
10 ms |
15180 KB |
Output is correct |
39 |
Correct |
208 ms |
30060 KB |
Output is correct |
40 |
Correct |
237 ms |
30048 KB |
Output is correct |
41 |
Correct |
218 ms |
30052 KB |
Output is correct |
42 |
Correct |
238 ms |
30020 KB |
Output is correct |
43 |
Correct |
147 ms |
30128 KB |
Output is correct |
44 |
Correct |
114 ms |
30020 KB |
Output is correct |
45 |
Correct |
194 ms |
32536 KB |
Output is correct |
46 |
Correct |
127 ms |
34752 KB |
Output is correct |
47 |
Correct |
354 ms |
49952 KB |
Output is correct |
48 |
Correct |
442 ms |
58120 KB |
Output is correct |
49 |
Correct |
434 ms |
57504 KB |
Output is correct |
50 |
Correct |
350 ms |
49948 KB |
Output is correct |
51 |
Correct |
332 ms |
49896 KB |
Output is correct |
52 |
Correct |
478 ms |
49860 KB |
Output is correct |
53 |
Correct |
368 ms |
48780 KB |
Output is correct |
54 |
Correct |
342 ms |
48676 KB |
Output is correct |
55 |
Correct |
20 ms |
13260 KB |
Output is correct |
56 |
Correct |
19 ms |
13168 KB |
Output is correct |
57 |
Correct |
117 ms |
30040 KB |
Output is correct |
58 |
Correct |
74 ms |
31472 KB |
Output is correct |
59 |
Correct |
142 ms |
36888 KB |
Output is correct |
60 |
Correct |
616 ms |
68384 KB |
Output is correct |
61 |
Correct |
374 ms |
53188 KB |
Output is correct |
62 |
Correct |
445 ms |
52940 KB |
Output is correct |
63 |
Correct |
550 ms |
53064 KB |
Output is correct |
64 |
Correct |
678 ms |
57144 KB |
Output is correct |
65 |
Correct |
445 ms |
52824 KB |
Output is correct |
66 |
Correct |
558 ms |
74896 KB |
Output is correct |
67 |
Correct |
350 ms |
54964 KB |
Output is correct |
68 |
Correct |
565 ms |
64380 KB |
Output is correct |
69 |
Correct |
557 ms |
64972 KB |
Output is correct |
70 |
Correct |
533 ms |
62272 KB |
Output is correct |