#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<ll,int>P;
#include "factories.h"
vector<P>E[600000];
vector<ll>d[600000];
bool used[600000];
int sz[600000];
int par[600000];
ll dist[600000];
int szdfs(int v,int p){
sz[v]=1;
for(auto u:E[v]){
if(u.second==p||used[u.second])continue;
sz[v]+=szdfs(u.second,v);
}
return sz[v];
}
int search_centroid(int v,int p,int s){
int m=0;
for(auto u:E[v]){
if(u.second==p||used[u.second])continue;
if(sz[u.second]>s/2)return search_centroid(u.second,v,s);
}
return v;
}
void dfs(int v,int p,ll w){
d[v].push_back(w);
for(auto u:E[v]){
if(u.second==p||used[u.second])continue;
dfs(u.second,v,w+u.first);
}
}
void build(int v,int p){
szdfs(v,-1);
int s=search_centroid(v,-1,sz[v]);
used[s]=true;
par[s]=p;
dfs(s,-1,0);
for(auto u:E[s]){
if(used[u.second])continue;
par[u.second]=s;
build(u.second,s);
}
}
void Init(int N, int A[], int B[], int D[]) {
rep(i,N-1){
E[A[i]].push_back(P(D[i],B[i]));
E[B[i]].push_back(P(D[i],A[i]));
}
build(0,-1);
memset(dist,0x3f,sizeof(dist));
}
long long Query(int S, int X[], int T, int Y[]) {
rep(i,S){
int v=X[i];
for(int j=d[X[i]].size()-1;j>=0;j--){
dist[v]=min(dist[v],d[X[i]][j]);
if(j)v=par[v];
}
}
ll ans=INF;
rep(i,T){
int v=Y[i];
for(int j=d[Y[i]].size()-1;j>=0;j--){
ans=min(ans,dist[v]+d[Y[i]][j]);
if(j)v=par[v];
}
}
rep(i,S){
int v=X[i];
for(int j=d[X[i]].size()-1;j>=0;j--){
dist[v]=INF;
if(j)v=par[v];
}
}
return ans;
}
Compilation message
factories.cpp: In function 'int search_centroid(int, int, int)':
factories.cpp:30:6: warning: unused variable 'm' [-Wunused-variable]
int m=0;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
33536 KB |
Output is correct |
2 |
Correct |
350 ms |
42092 KB |
Output is correct |
3 |
Correct |
436 ms |
42280 KB |
Output is correct |
4 |
Correct |
359 ms |
42360 KB |
Output is correct |
5 |
Correct |
539 ms |
42592 KB |
Output is correct |
6 |
Correct |
328 ms |
41780 KB |
Output is correct |
7 |
Correct |
401 ms |
42300 KB |
Output is correct |
8 |
Correct |
437 ms |
42316 KB |
Output is correct |
9 |
Correct |
432 ms |
42544 KB |
Output is correct |
10 |
Correct |
316 ms |
41848 KB |
Output is correct |
11 |
Correct |
460 ms |
42492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
33372 KB |
Output is correct |
2 |
Correct |
3565 ms |
142728 KB |
Output is correct |
3 |
Correct |
5123 ms |
178724 KB |
Output is correct |
4 |
Correct |
1280 ms |
89424 KB |
Output is correct |
5 |
Correct |
6369 ms |
223524 KB |
Output is correct |
6 |
Correct |
5518 ms |
180000 KB |
Output is correct |
7 |
Correct |
1461 ms |
64632 KB |
Output is correct |
8 |
Correct |
634 ms |
53476 KB |
Output is correct |
9 |
Correct |
1715 ms |
72844 KB |
Output is correct |
10 |
Correct |
1481 ms |
65924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
33536 KB |
Output is correct |
2 |
Correct |
350 ms |
42092 KB |
Output is correct |
3 |
Correct |
436 ms |
42280 KB |
Output is correct |
4 |
Correct |
359 ms |
42360 KB |
Output is correct |
5 |
Correct |
539 ms |
42592 KB |
Output is correct |
6 |
Correct |
328 ms |
41780 KB |
Output is correct |
7 |
Correct |
401 ms |
42300 KB |
Output is correct |
8 |
Correct |
437 ms |
42316 KB |
Output is correct |
9 |
Correct |
432 ms |
42544 KB |
Output is correct |
10 |
Correct |
316 ms |
41848 KB |
Output is correct |
11 |
Correct |
460 ms |
42492 KB |
Output is correct |
12 |
Correct |
33 ms |
33372 KB |
Output is correct |
13 |
Correct |
3565 ms |
142728 KB |
Output is correct |
14 |
Correct |
5123 ms |
178724 KB |
Output is correct |
15 |
Correct |
1280 ms |
89424 KB |
Output is correct |
16 |
Correct |
6369 ms |
223524 KB |
Output is correct |
17 |
Correct |
5518 ms |
180000 KB |
Output is correct |
18 |
Correct |
1461 ms |
64632 KB |
Output is correct |
19 |
Correct |
634 ms |
53476 KB |
Output is correct |
20 |
Correct |
1715 ms |
72844 KB |
Output is correct |
21 |
Correct |
1481 ms |
65924 KB |
Output is correct |
22 |
Correct |
4226 ms |
143468 KB |
Output is correct |
23 |
Correct |
4337 ms |
146592 KB |
Output is correct |
24 |
Correct |
6354 ms |
180612 KB |
Output is correct |
25 |
Correct |
6528 ms |
184112 KB |
Output is correct |
26 |
Correct |
5959 ms |
181100 KB |
Output is correct |
27 |
Correct |
6468 ms |
223332 KB |
Output is correct |
28 |
Correct |
1568 ms |
93520 KB |
Output is correct |
29 |
Correct |
5495 ms |
180676 KB |
Output is correct |
30 |
Correct |
5261 ms |
180428 KB |
Output is correct |
31 |
Correct |
5323 ms |
180988 KB |
Output is correct |
32 |
Correct |
1544 ms |
73948 KB |
Output is correct |
33 |
Correct |
633 ms |
54628 KB |
Output is correct |
34 |
Correct |
1018 ms |
61212 KB |
Output is correct |
35 |
Correct |
967 ms |
61432 KB |
Output is correct |
36 |
Correct |
1217 ms |
64052 KB |
Output is correct |
37 |
Correct |
1527 ms |
64124 KB |
Output is correct |