///breaker
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
//#include "factories.h"
using namespace std;
#define ll long long
#define fi first
#define se second
const ll maxn = 5e5 + 7;
const ll inf = 1e18+7;
ll minn[maxn];
int sz[maxn];
bool used[maxn];
vector<pair<int, ll>>adj[maxn], ancestors[maxn];
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;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
45648 KB |
Output is correct |
2 |
Correct |
203 ms |
50760 KB |
Output is correct |
3 |
Correct |
206 ms |
51276 KB |
Output is correct |
4 |
Correct |
219 ms |
51372 KB |
Output is correct |
5 |
Correct |
247 ms |
51784 KB |
Output is correct |
6 |
Correct |
161 ms |
50356 KB |
Output is correct |
7 |
Correct |
205 ms |
51376 KB |
Output is correct |
8 |
Correct |
224 ms |
51376 KB |
Output is correct |
9 |
Correct |
235 ms |
51788 KB |
Output is correct |
10 |
Correct |
148 ms |
50352 KB |
Output is correct |
11 |
Correct |
210 ms |
51344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
45648 KB |
Output is correct |
2 |
Correct |
1641 ms |
206772 KB |
Output is correct |
3 |
Correct |
2373 ms |
278344 KB |
Output is correct |
4 |
Correct |
537 ms |
100784 KB |
Output is correct |
5 |
Correct |
2926 ms |
351984 KB |
Output is correct |
6 |
Correct |
2371 ms |
277500 KB |
Output is correct |
7 |
Correct |
589 ms |
86600 KB |
Output is correct |
8 |
Correct |
250 ms |
64516 KB |
Output is correct |
9 |
Correct |
607 ms |
96944 KB |
Output is correct |
10 |
Correct |
592 ms |
85424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
45648 KB |
Output is correct |
2 |
Correct |
203 ms |
50760 KB |
Output is correct |
3 |
Correct |
206 ms |
51276 KB |
Output is correct |
4 |
Correct |
219 ms |
51372 KB |
Output is correct |
5 |
Correct |
247 ms |
51784 KB |
Output is correct |
6 |
Correct |
161 ms |
50356 KB |
Output is correct |
7 |
Correct |
205 ms |
51376 KB |
Output is correct |
8 |
Correct |
224 ms |
51376 KB |
Output is correct |
9 |
Correct |
235 ms |
51788 KB |
Output is correct |
10 |
Correct |
148 ms |
50352 KB |
Output is correct |
11 |
Correct |
210 ms |
51344 KB |
Output is correct |
12 |
Correct |
8 ms |
45648 KB |
Output is correct |
13 |
Correct |
1641 ms |
206772 KB |
Output is correct |
14 |
Correct |
2373 ms |
278344 KB |
Output is correct |
15 |
Correct |
537 ms |
100784 KB |
Output is correct |
16 |
Correct |
2926 ms |
351984 KB |
Output is correct |
17 |
Correct |
2371 ms |
277500 KB |
Output is correct |
18 |
Correct |
589 ms |
86600 KB |
Output is correct |
19 |
Correct |
250 ms |
64516 KB |
Output is correct |
20 |
Correct |
607 ms |
96944 KB |
Output is correct |
21 |
Correct |
592 ms |
85424 KB |
Output is correct |
22 |
Correct |
1796 ms |
203288 KB |
Output is correct |
23 |
Correct |
1813 ms |
210088 KB |
Output is correct |
24 |
Correct |
2606 ms |
275956 KB |
Output is correct |
25 |
Correct |
3015 ms |
278224 KB |
Output is correct |
26 |
Correct |
2872 ms |
277316 KB |
Output is correct |
27 |
Correct |
3303 ms |
348504 KB |
Output is correct |
28 |
Correct |
664 ms |
100784 KB |
Output is correct |
29 |
Correct |
2559 ms |
276304 KB |
Output is correct |
30 |
Correct |
2547 ms |
276620 KB |
Output is correct |
31 |
Correct |
2590 ms |
276604 KB |
Output is correct |
32 |
Correct |
606 ms |
97208 KB |
Output is correct |
33 |
Correct |
213 ms |
60612 KB |
Output is correct |
34 |
Correct |
413 ms |
77232 KB |
Output is correct |
35 |
Correct |
439 ms |
78152 KB |
Output is correct |
36 |
Correct |
534 ms |
83016 KB |
Output is correct |
37 |
Correct |
512 ms |
83116 KB |
Output is correct |