#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
using ll = long long;
const int mxN = (int)5e5+10;
const int mxLg = (int)22;
const ll LINF = (ll)3e18;
int n, sub[mxN], par[mxN], vis[mxN], lev[mxN];
ll best[mxN], dis[mxN][mxLg];
vector<pair<int,ll>> adj[mxN];
int fcen(int s, int p, int n){
for(auto x : adj[s]){
int u = x.fi;
if(u!=p and sub[u]>n/2 and !vis[u])
return fcen(u,s,n);
}
return s;
}
int dfs(int s, int p, int S, bool ok){
sub[s]=1;
for(auto x : adj[s]){
int u = x.fi; ll w = x.se; if(u==p or vis[u]) continue;
if(ok) dis[u][lev[u]-lev[S]]=dis[s][lev[s]-lev[S]]+w;
sub[s]+=dfs(u,s, S,ok);
}
return sub[s];
}
void cen_decomp1(int s, int p){
int n = dfs(s,p,s,0);
int cen = fcen(s,p,n);
vis[cen] = 1;
if(p!=-1) lev[cen]=lev[p]+1;
par[cen]=p;
for(auto x : adj[cen]){
int u = x.fi;
if(u!=p and !vis[u])
cen_decomp1(u,cen);
}
}
void cen_decomp(int s, int p){
int n = dfs(s,p,s,0);
int cen = fcen(s,p,n);
vis[cen] = 1; dfs(cen,p,cen,1);
for(auto x : adj[cen]){
int u = x.fi, w = x.se;
if(u!=p and !vis[u]) cen_decomp(u,cen);
}
}
void Init(int N, int a[], int b[], int d[]) {
n = N; memset(par,-1,sizeof(par));
for(int i = 0; i <= n; i++) best[i]=LINF;
for(int i = 0; i < n-1; i++){
adj[a[i]].pb({b[i],d[i]});
adj[b[i]].pb({a[i],d[i]});
}
cen_decomp1(0,-1); fill(vis,vis+n,0); cen_decomp(0,-1);
}
ll Query(int s, int x[], int t, int y[]) {
for(int i = 0; i < s; i++){
int a = x[i], j = 0;
while(a!=-1){
ll DD = dis[x[i]][j];
//cout << x[i] << " " << a << " " << DD << "\n";
best[a]=min(best[a],DD), a = par[a], j++;
}
}
ll ans = LINF;
for(int i = 0; i < t; i++){
int x = y[i], j = 0;
while(x!=-1){
ans = min(ans, dis[y[i]][j]+best[x]);
x=par[x], j++;
}
}
for(int i = 0; i < s; i++){
int a = x[i];
while(a!=-1) best[a]=LINF, a = par[a];
}
return ans;
}
/*
7 3
0 1 4
1 2 4
2 3 5
2 4 6
4 5 5
1 6 3
2 2
0 6
3 4
3 2
0 1 3
4 6
1 1
2
5
10 2
0 1 2
0 5 3
1 2 4
1 3 1
5 6 7
5 8 5
5 9 6
3 4 2
6 7 4
5 5
0 1 2 3 4
5 6 7 8 9
5 5
5 6 7 8 9
0 1 2 3 4
*/
Compilation message
factories.cpp: In function 'void cen_decomp(int, int)':
factories.cpp:52:23: warning: unused variable 'w' [-Wunused-variable]
52 | int u = x.fi, w = x.se;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
14420 KB |
Output is correct |
2 |
Correct |
244 ms |
23236 KB |
Output is correct |
3 |
Correct |
273 ms |
23236 KB |
Output is correct |
4 |
Correct |
251 ms |
23392 KB |
Output is correct |
5 |
Correct |
269 ms |
23776 KB |
Output is correct |
6 |
Correct |
182 ms |
28032 KB |
Output is correct |
7 |
Correct |
269 ms |
28128 KB |
Output is correct |
8 |
Correct |
269 ms |
27776 KB |
Output is correct |
9 |
Correct |
280 ms |
27996 KB |
Output is correct |
10 |
Correct |
193 ms |
27664 KB |
Output is correct |
11 |
Correct |
271 ms |
26356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14164 KB |
Output is correct |
2 |
Correct |
2087 ms |
148372 KB |
Output is correct |
3 |
Correct |
3636 ms |
153104 KB |
Output is correct |
4 |
Correct |
595 ms |
146368 KB |
Output is correct |
5 |
Correct |
4620 ms |
189788 KB |
Output is correct |
6 |
Correct |
3409 ms |
154580 KB |
Output is correct |
7 |
Correct |
776 ms |
51592 KB |
Output is correct |
8 |
Correct |
284 ms |
50968 KB |
Output is correct |
9 |
Correct |
1068 ms |
56764 KB |
Output is correct |
10 |
Correct |
830 ms |
52624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
14420 KB |
Output is correct |
2 |
Correct |
244 ms |
23236 KB |
Output is correct |
3 |
Correct |
273 ms |
23236 KB |
Output is correct |
4 |
Correct |
251 ms |
23392 KB |
Output is correct |
5 |
Correct |
269 ms |
23776 KB |
Output is correct |
6 |
Correct |
182 ms |
28032 KB |
Output is correct |
7 |
Correct |
269 ms |
28128 KB |
Output is correct |
8 |
Correct |
269 ms |
27776 KB |
Output is correct |
9 |
Correct |
280 ms |
27996 KB |
Output is correct |
10 |
Correct |
193 ms |
27664 KB |
Output is correct |
11 |
Correct |
271 ms |
26356 KB |
Output is correct |
12 |
Correct |
8 ms |
14164 KB |
Output is correct |
13 |
Correct |
2087 ms |
148372 KB |
Output is correct |
14 |
Correct |
3636 ms |
153104 KB |
Output is correct |
15 |
Correct |
595 ms |
146368 KB |
Output is correct |
16 |
Correct |
4620 ms |
189788 KB |
Output is correct |
17 |
Correct |
3409 ms |
154580 KB |
Output is correct |
18 |
Correct |
776 ms |
51592 KB |
Output is correct |
19 |
Correct |
284 ms |
50968 KB |
Output is correct |
20 |
Correct |
1068 ms |
56764 KB |
Output is correct |
21 |
Correct |
830 ms |
52624 KB |
Output is correct |
22 |
Correct |
2434 ms |
150244 KB |
Output is correct |
23 |
Correct |
2494 ms |
152660 KB |
Output is correct |
24 |
Correct |
3988 ms |
155060 KB |
Output is correct |
25 |
Correct |
3958 ms |
158620 KB |
Output is correct |
26 |
Correct |
4038 ms |
155176 KB |
Output is correct |
27 |
Correct |
5023 ms |
184884 KB |
Output is correct |
28 |
Correct |
754 ms |
150244 KB |
Output is correct |
29 |
Correct |
3891 ms |
154760 KB |
Output is correct |
30 |
Correct |
3850 ms |
154416 KB |
Output is correct |
31 |
Correct |
3953 ms |
154916 KB |
Output is correct |
32 |
Correct |
959 ms |
59812 KB |
Output is correct |
33 |
Correct |
303 ms |
53368 KB |
Output is correct |
34 |
Correct |
568 ms |
50940 KB |
Output is correct |
35 |
Correct |
580 ms |
51532 KB |
Output is correct |
36 |
Correct |
797 ms |
52684 KB |
Output is correct |
37 |
Correct |
793 ms |
52624 KB |
Output is correct |