///breaker
#include<bits/stdc++.h>
#include "factories.h"
using namespace std;
#define ll long long
#define fi first
#define se second
#define ii pair<int,int>
#define mp make_pair
#define in(x) freopen(x,"r",stdin)
#define out(x) freopen(x,"w",stdout)
#define bit(x,i) ((x>>i)&1)
#define lc (id<<1)
#define rc ((id<<1)^1)
const int maxn=5e5+7;
const ll inf=1e18+7;
vector<pair<int,ll>>adj[maxn];
int sz[maxn];
bool used[maxn];
int p[25][maxn];
ll minn[maxn];
int h[maxn];
vector<pair<int,ll>>ancestors[maxn];
int dfs(int u,int par)
{
sz[u]=1;
for(ii pp:adj[u]){
int v=pp.fi;
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(ii pp:adj[u]){
int v=pp.fi;
int w=pp.se;
if(v==par||used[v])continue;
dfs2(v,u,cen,depth+w);
}
}
int getcen(int u,int par,int treesize)
{
for(ii 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));
used[C]=1;
dfs2(C,-1,C,0);
for(ii pp:adj[C]){
int v=pp.fi;
if(v==par||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({B[i],D[i]});
adj[B[i]].push_back({A[i],D[i]});
}
buildcen(0,-1);
for(int i=0;i<N;i++){
minn[i]=inf;
}
}
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;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47696 KB |
Output is correct |
2 |
Correct |
194 ms |
52948 KB |
Output is correct |
3 |
Correct |
214 ms |
53432 KB |
Output is correct |
4 |
Correct |
200 ms |
53320 KB |
Output is correct |
5 |
Correct |
222 ms |
53832 KB |
Output is correct |
6 |
Correct |
144 ms |
52296 KB |
Output is correct |
7 |
Correct |
206 ms |
53320 KB |
Output is correct |
8 |
Correct |
215 ms |
53320 KB |
Output is correct |
9 |
Correct |
236 ms |
53836 KB |
Output is correct |
10 |
Correct |
141 ms |
52360 KB |
Output is correct |
11 |
Correct |
219 ms |
53428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
47708 KB |
Output is correct |
2 |
Correct |
1343 ms |
209992 KB |
Output is correct |
3 |
Correct |
2060 ms |
281740 KB |
Output is correct |
4 |
Correct |
499 ms |
104360 KB |
Output is correct |
5 |
Correct |
2558 ms |
355500 KB |
Output is correct |
6 |
Correct |
2247 ms |
281348 KB |
Output is correct |
7 |
Correct |
536 ms |
85832 KB |
Output is correct |
8 |
Correct |
240 ms |
62504 KB |
Output is correct |
9 |
Correct |
612 ms |
99000 KB |
Output is correct |
10 |
Correct |
580 ms |
86344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47696 KB |
Output is correct |
2 |
Correct |
194 ms |
52948 KB |
Output is correct |
3 |
Correct |
214 ms |
53432 KB |
Output is correct |
4 |
Correct |
200 ms |
53320 KB |
Output is correct |
5 |
Correct |
222 ms |
53832 KB |
Output is correct |
6 |
Correct |
144 ms |
52296 KB |
Output is correct |
7 |
Correct |
206 ms |
53320 KB |
Output is correct |
8 |
Correct |
215 ms |
53320 KB |
Output is correct |
9 |
Correct |
236 ms |
53836 KB |
Output is correct |
10 |
Correct |
141 ms |
52360 KB |
Output is correct |
11 |
Correct |
219 ms |
53428 KB |
Output is correct |
12 |
Correct |
10 ms |
47708 KB |
Output is correct |
13 |
Correct |
1343 ms |
209992 KB |
Output is correct |
14 |
Correct |
2060 ms |
281740 KB |
Output is correct |
15 |
Correct |
499 ms |
104360 KB |
Output is correct |
16 |
Correct |
2558 ms |
355500 KB |
Output is correct |
17 |
Correct |
2247 ms |
281348 KB |
Output is correct |
18 |
Correct |
536 ms |
85832 KB |
Output is correct |
19 |
Correct |
240 ms |
62504 KB |
Output is correct |
20 |
Correct |
612 ms |
99000 KB |
Output is correct |
21 |
Correct |
580 ms |
86344 KB |
Output is correct |
22 |
Correct |
1665 ms |
207016 KB |
Output is correct |
23 |
Correct |
1728 ms |
210384 KB |
Output is correct |
24 |
Correct |
2554 ms |
279668 KB |
Output is correct |
25 |
Correct |
2667 ms |
281976 KB |
Output is correct |
26 |
Correct |
2569 ms |
281156 KB |
Output is correct |
27 |
Correct |
3233 ms |
352172 KB |
Output is correct |
28 |
Correct |
602 ms |
104616 KB |
Output is correct |
29 |
Correct |
2613 ms |
280208 KB |
Output is correct |
30 |
Correct |
2482 ms |
280244 KB |
Output is correct |
31 |
Correct |
2321 ms |
280264 KB |
Output is correct |
32 |
Correct |
635 ms |
99248 KB |
Output is correct |
33 |
Correct |
214 ms |
62648 KB |
Output is correct |
34 |
Correct |
387 ms |
79432 KB |
Output is correct |
35 |
Correct |
360 ms |
80192 KB |
Output is correct |
36 |
Correct |
476 ms |
85164 KB |
Output is correct |
37 |
Correct |
568 ms |
85320 KB |
Output is correct |