# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
233655 | almogwald | Factories (JOI14_factories) | C++14 | 8048 ms | 118940 KiB |
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 <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define forr(i,a,b,c) for(int i=a;i<b;i+=c)
#define fort(i,a,b) forr(i,a,b,1)
#define forn(i,n) fort(i,1,n)
#define fori(i,n) fort(i,0,n)
#define forrb(i,a,b,c) for(int i=b-1;i>=a;i-=c)
#define fortb(i,a,b) forrb(i,a,b,1)
#define fornb(i,n) fortb(i,1,n)
#define forib(i,n) fortb(i,0,n)
using namespace std;
#define into(x) cin >> x;
typedef vector<int> veci;
typedef long long lol;
typedef pair<lol,lol> point;
#define def(x) lol x; into(x)
vector<veci> f;
#define MAX_N 500000
#define logn 19
vector<vector<point>> cons;
lol depth[MAX_N];
int dep[MAX_N];
void Init(int N, int A[], int B[], int D[]) {
cons.resize(N);
f.resize(N);
fori(i, N - 1) {
cons[A[i]].push_back({ B[i] , D[i]});
cons[B[i]].push_back({ A[i] , D[i] });
}
fori(i, N) {
f[i].resize(logn);
}
dep[0] = 0;
depth[0] = 0;
veci visited(N);
veci stk(1);
while (!stk.empty()) {
int cur = stk.back();
stk.pop_back();
visited[cur] = 1;
forn(i, logn) {
f[cur][i] = f[f[cur][i - 1]][i - 1];
}
fori(i, cons[cur].size()) {
if (visited[cons[cur][i].first] == 0) {
f[cons[cur][i].first][0] = cur;
dep[cons[cur][i].first] = dep[cur] + 1;
depth[cons[cur][i].first] = depth[cur] + cons[cur][i].second;
stk.push_back(cons[cur][i].first);
}
}
}
}
inline int lca(int l, int r) {
int k = logn-1;
while (dep[l] != dep[r]) {
if (dep[l] + (1 << k) <= dep[r]) {
r = f[r][k];
}
if (dep[r] + (1 << k) <= dep[l]) {
l = f[l][k];
}
k--;
}
if (l == r) {
return l;
}
k = logn - 1;
while (k>=0) {
if (f[r][k]!= f[l][k]) {
r = f[r][k];
l = f[l][k];
}
k--;
}
return f[l][0];
}
long long Query(int S, int X[], int T, int Y[]) {
if (true) {
lol minmium = 1298719928229189219;
fori(i, S) {
fori(j, T) {
minmium = min(minmium, depth[X[i]] + depth[Y[j]] - 2 * depth[lca(X[i], Y[j])]);
}
}
return minmium;
} else {
veci visited(f.size());
set<point> stk;
veci ender(f.size());
fori(i, S) {
stk.insert({ 0,X[i] });
}
fori(j, T) {
ender[Y[j]] = 1;
}
while (!stk.empty()) {
auto it = stk.begin();
lol dis = it->first;
int cur = it->second;
stk.erase(it);
if (visited[cur]) {
continue;
}
visited[cur] = 1;
if (ender[cur]) {
return dis;
}
fori(i, cons[cur].size()) {
stk.insert({ cons[cur][i].second+dis,cons[cur][i].first });
}
}
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |