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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const ll inf = 1e18 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n, q;
vector<pii> g[maxn];
int par[maxn];
int cnt[maxn];
bool bio[maxn];
void dfs_size(int v, int p) {
cnt[v] = 1;
for(auto c : g[v]) {
int u = c.fi;
int w = c.se;
if(u == p || bio[u])continue;
dfs_size(u, v);
cnt[v] += cnt[u];
}
}
int centroid(int v, int p, int vel) {
for(auto c : g[v]) {
int u = c.fi;
int w = c.se;
if(u == p || bio[u])continue;
if(cnt[u] > vel / 2)return centroid(u, v, vel);
}
return v;
}
void decompose(int v, int p) {
dfs_size(v, p);
int cen = centroid(v, p, cnt[v]);
bio[cen] = 1;
par[cen] = p;
for(auto c : g[cen]) {
int u = c.fi;
int w = c.se;
if(u == p || bio[u])continue;
decompose(u, cen);
}
}
vector<int> euler;
int d[maxn];
int in[maxn];
ll dist[maxn];
void dfs(int v, int p){
in[v] = sz(euler);
euler.pb(v);
for(auto c : g[v]){
int u = c.fi;
int w = c.se;
if(u != p){
d[u] = d[v] + 1;
dist[u] = dist[v] + w;
dfs(u, v);
euler.pb(v);
}
}
}
int cmb(int X, int Y){
return (d[X] > d[Y] ? Y : X);
}
int sp[10 * maxn][20];
void init_sp(){
int N = sz(euler);
ff(i,0,N - 1)sp[i][0] = euler[i];
ff(j,1,19){
for(int i = 0;i + (1 << j) - 1 < N; i++){
sp[i][j] = cmb(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
}
int rmq(int l, int r){
int x = 31 - __builtin_clz(r - l + 1);
return cmb(sp[l][x], sp[r - (1 << x) + 1][x]);
}
int lca(int x, int y){
x = in[x], y = in[y];
if(x > y)swap(x, y);
return rmq(x, y);
}
// treba mi lca u O(1)
ll distance(int x, int y) {
return dist[x] + dist[y] - 2 * dist[lca(x, y)];
}
ll best[maxn];
void upd(int X, int Y) {
for(int A = X ; A != 0 ; A = par[A]) {
if(Y == 1)best[A] = min(best[A], distance(A, X));
else best[A] = inf;
}
}
ll kve(int X) {
ll ans = inf;
for(int A = X ; A != 0 ; A = par[A]) {
ans = min(ans, best[A] + distance(A, X));
}
return ans;
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
ff(i,0,n - 2) {
int u = A[i] + 1;
int v = B[i] + 1;
int w = D[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
decompose(1, 0);
dfs(1, 0);
init_sp();
ff(i,1,n)best[i] = inf;
}
ll Query(int S, int X[], int T, int Y[]) {
ff(i,0,S - 1) {
int A = X[i] + 1;
upd(A, 1);
}
ll ans = inf;
ff(i,0,T - 1) {
int B = Y[i] + 1;
ans = min(ans, kve(B));
}
ff(i,0,S - 1){
int A = X[i] + 1;
upd(A, -1);
}
return ans;
}
//int main()
//{
// ios::sync_with_stdio(false);
// cout.tie(nullptr);
// cin.tie(nullptr);
// cin >> n >> q;
// ff(i,1,n - 1){
// int u, v, w;
// cin >> u >> v >> w;
// u++, v++;
// g[u].pb({v, w});
// g[v].pb({u, w});
// }
//
// decompose(1, 0);
// dfs(1, 0);
// init_sp();
//
// ff(i,1,n)best[i] = inf;
//
// while(q--){
// int S, T;
// cin >> S >> T;
//
// ff(i,1,S){
// int X;
// cin >> X;
// X++;
// upd(X, 1);
// }
//
// ll ans = inf;
// ff(i,1,T){
// int X;
// cin >> X;
// X++;
// ans = min(ans, kve(X));
// }
// cout << ans << endl;
//
// }
//
// return 0;
//}
/**
7 1
0 1 4
1 2 4
2 3 5
2 4 6
4 5 5
1 6 3
2 2
0 6
3 4
// probati bojenje sahovski ili slicno
**/
Compilation message (stderr)
factories.cpp: In function 'void dfs_size(int, int)':
factories.cpp:41:7: warning: unused variable 'w' [-Wunused-variable]
41 | int w = c.se;
| ^
factories.cpp: In function 'int centroid(int, int, int)':
factories.cpp:51:7: warning: unused variable 'w' [-Wunused-variable]
51 | int w = c.se;
| ^
factories.cpp: In function 'void decompose(int, int)':
factories.cpp:67:7: warning: unused variable 'w' [-Wunused-variable]
67 | int w = c.se;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |