#include<bits/stdc++.h>
#include "factories.h"
using namespace std;
typedef long long ll;
const ll INF=1e18;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mo=998244353;
using pi=pair<ll,ll>;
using vi=vector<ll>;
using pii=pair<pair<ll,ll>,ll>;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI 3.14159265359
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
vector<pair<int,int>>G[500005];
int sz[500005];
int pa[500005];
int de[500005];
long long dis[500005][22];
long long ans[500005];
int killed[500005];
int dfsz(int u, int p) {
sz[u] = 1;
for(auto v : G[u]) if(v.first != p && killed[v.first]==0) {
sz[u] += dfsz(v.first, u);
}
return sz[u];
}
int centroid(int u, int p, int n) {
for(auto v : G[u]) if(v.first != p && killed[v.first]==0) {
if(sz[v.first] > n / 2) return centroid(v.first, u, n);
}
return u;
}
void dfs2(int u, int p, int d) {
for(auto v : G[u]) if(v.first != p && killed[v.first]==0) {
dis[v.first][d]=dis[u][d]+v.second;
dfs2(v.first,u,d);
}
}
void build(int u, int p,int d) {
int n1 = dfsz(u, p);
int c = centroid(u, p, n1);
pa[c] = p;
killed[c]=1;
dis[c][d]=0;
de[c]=d;
dfs2(c, -1, d);
for(auto v : G[c]) {
if(killed[v.first]==0){
build(v.first, c,d+1);
}
}
}
void upd(int u){
int v=u;
int d=de[u];
int cc=u;
while(d>=1){
ans[cc]=min(ans[cc],dis[u][d]);
cc=pa[cc];
d--;
}
}
void upd1(int u){
int v=u;
int d=de[u];
int cc=u;
while(d>=1){
ans[cc]=INF;
cc=pa[cc];
d--;
}
}
long long query(int u) {
long long mi = INF;
int cc=u;
int d=de[u];
while(d>=1){
mi=min(ans[cc]+dis[u][d],mi);
cc=pa[cc];
d--;
}
return mi;
}
void Init(int N,int A[],int B[],int D[]){
n=N;
for(int i=0;i<N-1;i++){
G[A[i]+1].pb({B[i]+1,D[i]});
G[B[i]+1].pb({A[i]+1,D[i]});
}
build(1,-1,1);
for(int i=1;i<=n;i++){
ans[i]=INF;
}
}
long long Query(int S,int X[],int T,int Y[]){
for(int i=0;i<S;i++){
upd(X[i]+1);
}
long long an=INF;
for(int i=0;i<T;i++){
an=min(an,query(Y[i]+1));
}
for(int i=0;i<S;i++){
upd1(X[i]+1);
}
return an;
}
/*
signed main(){
int q;
cin>>n>>q;
for(int i=1;i<=n-1;i++){
int x,y,d;
cin>>x>>y>>d;
x++;
y++;
G[x].push_back({y,d});
G[y].push_back({x,d});
}
build(1,-1,1);
for(int j=1;j<=q;j++){
int S,T;
cin>>S>>T;
for(int i=1;i<=n;i++){
ans[i]=INF;
}
for(int i=0;i<S;i++){
int x;
cin>>x;
upd(x+1);
}
long long an=INF;
for(int i=0;i<T;i++){
int y;
cin>>y;
an=min(an,query(y+1));
}
cout<<an<<"\n";
}
}
*/
Compilation message
factories.cpp: In function 'void upd(int)':
factories.cpp:67:9: warning: unused variable 'v' [-Wunused-variable]
67 | int v=u;
| ^
factories.cpp: In function 'void upd1(int)':
factories.cpp:78:9: warning: unused variable 'v' [-Wunused-variable]
78 | int v=u;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12500 KB |
Output is correct |
2 |
Correct |
225 ms |
21368 KB |
Output is correct |
3 |
Correct |
246 ms |
21256 KB |
Output is correct |
4 |
Correct |
272 ms |
21324 KB |
Output is correct |
5 |
Correct |
258 ms |
21540 KB |
Output is correct |
6 |
Correct |
189 ms |
21284 KB |
Output is correct |
7 |
Correct |
240 ms |
21252 KB |
Output is correct |
8 |
Correct |
251 ms |
21288 KB |
Output is correct |
9 |
Correct |
260 ms |
21604 KB |
Output is correct |
10 |
Correct |
186 ms |
21308 KB |
Output is correct |
11 |
Correct |
251 ms |
21292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
12244 KB |
Output is correct |
2 |
Correct |
1513 ms |
141424 KB |
Output is correct |
3 |
Correct |
2400 ms |
144008 KB |
Output is correct |
4 |
Correct |
576 ms |
143068 KB |
Output is correct |
5 |
Correct |
3204 ms |
166108 KB |
Output is correct |
6 |
Correct |
2457 ms |
144832 KB |
Output is correct |
7 |
Correct |
754 ms |
47400 KB |
Output is correct |
8 |
Correct |
318 ms |
48100 KB |
Output is correct |
9 |
Correct |
920 ms |
50772 KB |
Output is correct |
10 |
Correct |
711 ms |
48672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12500 KB |
Output is correct |
2 |
Correct |
225 ms |
21368 KB |
Output is correct |
3 |
Correct |
246 ms |
21256 KB |
Output is correct |
4 |
Correct |
272 ms |
21324 KB |
Output is correct |
5 |
Correct |
258 ms |
21540 KB |
Output is correct |
6 |
Correct |
189 ms |
21284 KB |
Output is correct |
7 |
Correct |
240 ms |
21252 KB |
Output is correct |
8 |
Correct |
251 ms |
21288 KB |
Output is correct |
9 |
Correct |
260 ms |
21604 KB |
Output is correct |
10 |
Correct |
186 ms |
21308 KB |
Output is correct |
11 |
Correct |
251 ms |
21292 KB |
Output is correct |
12 |
Correct |
8 ms |
12244 KB |
Output is correct |
13 |
Correct |
1513 ms |
141424 KB |
Output is correct |
14 |
Correct |
2400 ms |
144008 KB |
Output is correct |
15 |
Correct |
576 ms |
143068 KB |
Output is correct |
16 |
Correct |
3204 ms |
166108 KB |
Output is correct |
17 |
Correct |
2457 ms |
144832 KB |
Output is correct |
18 |
Correct |
754 ms |
47400 KB |
Output is correct |
19 |
Correct |
318 ms |
48100 KB |
Output is correct |
20 |
Correct |
920 ms |
50772 KB |
Output is correct |
21 |
Correct |
711 ms |
48672 KB |
Output is correct |
22 |
Correct |
1796 ms |
143456 KB |
Output is correct |
23 |
Correct |
1867 ms |
145676 KB |
Output is correct |
24 |
Correct |
2823 ms |
145136 KB |
Output is correct |
25 |
Correct |
2909 ms |
148564 KB |
Output is correct |
26 |
Correct |
2833 ms |
145456 KB |
Output is correct |
27 |
Correct |
3627 ms |
163908 KB |
Output is correct |
28 |
Correct |
738 ms |
146480 KB |
Output is correct |
29 |
Correct |
2765 ms |
145120 KB |
Output is correct |
30 |
Correct |
2745 ms |
144276 KB |
Output is correct |
31 |
Correct |
2799 ms |
144816 KB |
Output is correct |
32 |
Correct |
821 ms |
51284 KB |
Output is correct |
33 |
Correct |
334 ms |
47888 KB |
Output is correct |
34 |
Correct |
508 ms |
44940 KB |
Output is correct |
35 |
Correct |
511 ms |
44924 KB |
Output is correct |
36 |
Correct |
675 ms |
45524 KB |
Output is correct |
37 |
Correct |
722 ms |
45340 KB |
Output is correct |