#include <bits/stdc++.h>
//#include "factories.h"
using namespace std;
#define ll long long
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll N = 5e5 + 2;
const ll inf = 1e14 + 2;
vector<ll> minn(N);
vector<int> sz(N), used(N, 0);
vector<vector<pair<int, ll>>> adj(N), ancestors(N);
int n;
int dfs(int u,int par)
{
sz[u]=1;
for(pair<int,ll>pp:adj[u]){
int v=pp.fi;
int w=pp.se;
if(v==par||used[v])continue;
sz[u]+=dfs(v,u);
}
return sz[u];
}
void dfs2(int u,int par,int cen,ll depth)
{
ancestors[u].push_back({cen,depth});
for(pair<int,ll> pp:adj[u]){
int v=pp.fi;
ll w=pp.se;
if(v==par||used[v])continue;
dfs2(v,u,cen,depth+w);
}
}
int getcen(int u,int par,int treesize)
{
for(pair<int,ll> pp:adj[u]){
int v=pp.fi;
if(v==par||used[v])continue;
if((sz[v]<<1)>treesize)return getcen(v,u,treesize);
}
return u;
}
void buildcen(int u,int par)
{
int C=getcen(u,-1,dfs(u,-1));
dfs2(C,-1,C,0);
used[C]=1;
for(pair<int,ll> pp:adj[C]){
int v=pp.fi;
if(used[v])continue;
buildcen(v,C);
}
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N-1; i++) {
adj[A[i]].push_back(make_pair(B[i], D[i]));
adj[B[i]].push_back(make_pair(A[i], D[i]));
}
buildcen(0, -1);
}
long long Query(int S, int X[], int T, int Y[])
{
for(int i=0;i<T;i++){
for(pair<int,long long> y:ancestors[Y[i]]){
minn[y.fi]=inf;
}
}
for(int i=0;i<S;i++){
for(pair<int,ll> y:ancestors[X[i]]){
minn[y.fi]=min(minn[y.fi],y.se);
}
}
ll ans=inf;
for(int i=0;i<T;i++){
for(pair<int,ll> y:ancestors[Y[i]]){
ans=min(ans,minn[y.fi]+y.se);
}
}
return ans;
}
Compilation message
factories.cpp: In function 'int dfs(int, int)':
factories.cpp:21:13: warning: unused variable 'w' [-Wunused-variable]
21 | int w=pp.se;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
48208 KB |
Output is correct |
2 |
Correct |
188 ms |
53284 KB |
Output is correct |
3 |
Correct |
212 ms |
53828 KB |
Output is correct |
4 |
Correct |
223 ms |
55872 KB |
Output is correct |
5 |
Correct |
225 ms |
54096 KB |
Output is correct |
6 |
Correct |
144 ms |
52876 KB |
Output is correct |
7 |
Correct |
210 ms |
53832 KB |
Output is correct |
8 |
Correct |
217 ms |
53832 KB |
Output is correct |
9 |
Correct |
218 ms |
54572 KB |
Output is correct |
10 |
Correct |
147 ms |
52772 KB |
Output is correct |
11 |
Correct |
229 ms |
53836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
48208 KB |
Output is correct |
2 |
Correct |
1649 ms |
208584 KB |
Output is correct |
3 |
Correct |
2398 ms |
281336 KB |
Output is correct |
4 |
Correct |
651 ms |
102312 KB |
Output is correct |
5 |
Correct |
3600 ms |
354536 KB |
Output is correct |
6 |
Correct |
2549 ms |
280932 KB |
Output is correct |
7 |
Correct |
587 ms |
90388 KB |
Output is correct |
8 |
Correct |
310 ms |
67000 KB |
Output is correct |
9 |
Correct |
686 ms |
99664 KB |
Output is correct |
10 |
Correct |
521 ms |
89164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
48208 KB |
Output is correct |
2 |
Correct |
188 ms |
53284 KB |
Output is correct |
3 |
Correct |
212 ms |
53828 KB |
Output is correct |
4 |
Correct |
223 ms |
55872 KB |
Output is correct |
5 |
Correct |
225 ms |
54096 KB |
Output is correct |
6 |
Correct |
144 ms |
52876 KB |
Output is correct |
7 |
Correct |
210 ms |
53832 KB |
Output is correct |
8 |
Correct |
217 ms |
53832 KB |
Output is correct |
9 |
Correct |
218 ms |
54572 KB |
Output is correct |
10 |
Correct |
147 ms |
52772 KB |
Output is correct |
11 |
Correct |
229 ms |
53836 KB |
Output is correct |
12 |
Correct |
12 ms |
48208 KB |
Output is correct |
13 |
Correct |
1649 ms |
208584 KB |
Output is correct |
14 |
Correct |
2398 ms |
281336 KB |
Output is correct |
15 |
Correct |
651 ms |
102312 KB |
Output is correct |
16 |
Correct |
3600 ms |
354536 KB |
Output is correct |
17 |
Correct |
2549 ms |
280932 KB |
Output is correct |
18 |
Correct |
587 ms |
90388 KB |
Output is correct |
19 |
Correct |
310 ms |
67000 KB |
Output is correct |
20 |
Correct |
686 ms |
99664 KB |
Output is correct |
21 |
Correct |
521 ms |
89164 KB |
Output is correct |
22 |
Correct |
1730 ms |
204840 KB |
Output is correct |
23 |
Correct |
1803 ms |
211508 KB |
Output is correct |
24 |
Correct |
2710 ms |
277068 KB |
Output is correct |
25 |
Correct |
2904 ms |
279596 KB |
Output is correct |
26 |
Correct |
2732 ms |
278716 KB |
Output is correct |
27 |
Correct |
3424 ms |
346820 KB |
Output is correct |
28 |
Correct |
779 ms |
105672 KB |
Output is correct |
29 |
Correct |
3079 ms |
277724 KB |
Output is correct |
30 |
Correct |
3019 ms |
277956 KB |
Output is correct |
31 |
Correct |
2688 ms |
280136 KB |
Output is correct |
32 |
Correct |
719 ms |
101424 KB |
Output is correct |
33 |
Correct |
243 ms |
63812 KB |
Output is correct |
34 |
Correct |
409 ms |
79700 KB |
Output is correct |
35 |
Correct |
426 ms |
81468 KB |
Output is correct |
36 |
Correct |
603 ms |
85600 KB |
Output is correct |
37 |
Correct |
581 ms |
85576 KB |
Output is correct |