This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
#define clr(a, x) memset(a, x, sizeof(a))
#define dbg(x) cout<<"("<<#x<<"): "<<x<<endl;
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b)/__gcd(a, b)
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define endl '\n'
#define ll long long int
#define float long double
const int MOD = 1e9 + 7;
const ll INF = 2e15;
const int MAXN = 5e5 + 5;
const int LOGN = 23;
vector<array<ll, 2>> adj[MAXN];
ll par[MAXN], sz[MAXN], depth[MAXN], dist[LOGN][MAXN], ans[MAXN], nm;
bool rm[MAXN];
int dfs(int u, int p){
sz[u] = 1;
for(auto &[v, w]: adj[u]){
if(v != p && !rm[v]){
sz[u] += dfs(v, u);
}
}
return sz[u];
}
int dfs2(int u, int p, int l){
for(auto &[v, w]: adj[u]){
if(v != p && !rm[v]){
dist[l][v] = dist[l][u] + w;
dfs2(v, u, l);
}
}
}
int centroid(int u, int p, int n){
for(auto &[v, w]: adj[u]){
if(v != p && !rm[v]){
if(sz[v]*2 > n){
return centroid(v, u, n);
}
}
}
return u;
}
void decompose(int u, int p, int d){
int n = dfs(u, p);
int c = centroid(u, p, n);
par[c] = p;
depth[c] = d;
dist[d][c] = 0;
dfs2(c, -1, d);
rm[c] = 1;
for(auto &[v, w]: adj[c]){
if(!rm[v]){
decompose(v, c, d+1);
}
}
}
void update(int v, int s){
ans[v] = min(ans[v], dist[depth[v]][s]);
if(depth[v] != 0){
update(par[v], s);
}
}
ll qry(int v, int s){
if(depth[v] != 0){
return min(ans[v]+dist[depth[v]][s], qry(par[v], s));
} else {
return ans[v] + dist[depth[v]][s];
}
}
void Init(int N, int A[], int B[], int D[]) {
nm = N;
clr(rm, 0);
clr(sz, 1);
for(int i= 0;i<nm-1;i++){
A[i]++; B[i]++;
adj[A[i]].pb({B[i], D[i]});
adj[B[i]].pb({A[i], D[i]});
}
decompose(1, 0, 0);
for(int i=0;i<=nm;i++) ans[i]= INF;
}
ll Query(int S, int X[], int T, int Y[]){
for(int i=0;i<S;i++){
X[i]++;
update(X[i], X[i]);
}
ll res = INF;
for(int i = 0;i<T;i++){
Y[i]++;
res = min(res, qry(Y[i], Y[i]));
}
for(int i=0;i<=nm;i++) ans[i]= INF;
return res;
}
Compilation message (stderr)
factories.cpp: In function 'int dfs(int, int)':
factories.cpp:29:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for(auto &[v, w]: adj[u]){
| ^
factories.cpp: In function 'int dfs2(int, int, int)':
factories.cpp:38:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | for(auto &[v, w]: adj[u]){
| ^
factories.cpp:44:1: warning: no return statement in function returning non-void [-Wreturn-type]
44 | }
| ^
factories.cpp: In function 'int centroid(int, int, int)':
factories.cpp:47:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | for(auto &[v, w]: adj[u]){
| ^
factories.cpp: In function 'void decompose(int, int, int)':
factories.cpp:65:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for(auto &[v, w]: adj[c]){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |