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 <bits/stdc++.h>
#include "swap.h"
using namespace std;
#define ll long long
#define sz(s) (int)s.size()
#define ff first
#define ss second
const int N = 400005;
vector <pair<int,pair<int,int>>> v;
int p[N], sp[N][40], p1[N], depth[N], cnt = 0, com[N];
pair <int,int> c[N], d[N];
vector <int> v1[N], v2[N];
int f(int z){
for(int i = 20; i >= 0; i--){
if(sp[z][i] != -1 and c[sp[z][i]].ff != 1){
z = sp[z][i];
}
}
if(c[z].ff == 1) return c[z].ss;
if(sp[z][0] == -1) return -1;
z = sp[z][0];
return c[z].ss;
}
void dfs(int x, int y){
com[x] = cnt;
for(auto i : v2[x]){
if(i != y){
depth[i] = depth[x]+1;
dfs(i,x);
}
}
}
int fnd(int x){
if(x == p[x]) return x;
return p[x] = fnd(p[x]);
}
int lca(int x, int y){
if(depth[x] < depth[y]) swap(x,y);
for(int i = 20; i >= 0; i--){
if(depth[x] - (1<<i) >= depth[y] and sp[x][i] != -1){
x = sp[x][i];
}
}
for(int i = 20; i >= 0; i--){
if(sp[x][i] != sp[y][i]){
x = sp[x][i];
y = sp[y][i];
}
}
if(sp[x][0] == -1 or sp[x][0] != sp[y][0]) return -1;
return sp[x][0];
}
void init(int n, int m, vector<int> u1, vector<int> v1, vector<int> w1) {
for(int i = 0; i < m; i++){
v.push_back({w1[i],{u1[i],v1[i]}});
}
sort(v.begin(), v.end());
for(int i = 0; i <= n+m; i++){
p[i] = i;
c[i] = {0,0};
d[i] = {i,i};
p1[i] = i;
}
int cn = n-1;
for(int i = 0; i < sz(v); i++){
auto x = v[i].ss;
int a = fnd(p[x.ff]), b = fnd(p[x.ss]);
cn++;
// cout << a << ' ' << b << ' ' << cn << ' ';
if(c[a].ff + c[b].ff == 2 and a == b){
v2[cn].push_back(a);
p[a] = cn;
p1[a] = cn;
c[cn] = c[a];
}
else if(c[a].ff + c[b].ff != 0){
p[a] = cn;
p1[a] = cn;
p[b] = cn;
p1[b] = cn;
c[cn] = {1,v[i].ff};
v2[cn].push_back(a);
if(a != b) v2[cn].push_back(b);
}
else {
if(a == b){
c[cn] = {1,v[i].ff};
p[a] = cn;
p1[a] = cn;
v2[cn].push_back(a);
}
else {
v2[cn].push_back(a);
v2[cn].push_back(b);
p[a] = cn;
p1[a] = cn;
p[b] = cn;
p1[b] = cn;
bool tr1 = (d[a].ff == x.ff or d[a].ff == x.ss), tr11 = (d[a].ss == x.ff or d[a].ss == x.ss);
bool tr2 = (d[b].ff == x.ff or d[b].ff == x.ss), tr22 = (d[b].ss == x.ff or d[b].ss == x.ss);
if((tr1+tr11) >= 1 and (tr2+tr22) >= 1){
c[cn] = {0,v[i].ff};
if(tr1 == 1) d[cn] .ff = d[a].ss;
else d[cn].ff = d[a].ff;
if(tr2 == 1) d[cn].ss = d[b].ss;
else d[cn].ss = d[b].ff;
}
else {
c[cn] = {1,v[i].ff};
}
}
}
// cout << c[cn].ff << ' ' << c[cn].ss << '\n';
}
for(int i = cn; i >= 0; i--){
if(depth[i] == 0){
cnt++;
dfs(i,-1);
}
}
for(int i = 0; i <= cn; i++){
sp[i][0] = p1[i];
if(p1[i] == i) sp[i][0] = -1;
}
for(int j = 1; j <= 20; j++){
for(int i = 0; i <= cn; i++){
if((1<<j) > depth[i]){
sp[i][j] = -1;
continue;
}
if(sp[i][j-1] != -1){
sp[i][j] = (sp[sp[i][j-1]][j-1]);
}
}
}
}
int getMinimumFuelCapacity(int x, int y) {
if(com[x] != com[y]) return -1;
int z = lca(x,y);
if(z == -1) return z;
// cout << z << '\n';
return f(z);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |