This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef UncleGrandpa
#include "factories.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define bit(x,y) ((x>>y)&1LL)
ostream& operator << (ostream &os, vector<int>&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
cout << x.fi << sp << x.se << sp;
return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
return os;
}
#define ll long long
vector<vector<pair<int, int> > > a(500005);
int n;
int par[500005][22];
int depth[500005];
ll dis[500005];
int sta[500005];
int Time = 0;
void dfs(int u, int p) {
par[u][0] = p; Time++;
sta[u] = Time;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i].fi;
if (v != p) {
depth[v] = depth[u] + 1; dis[v] = dis[u] + a[u][i].se;
dfs(v, u);
}
}
}
int lca(int p, int q) {
if (depth[p] < depth[q]) swap(p, q);
for (int i = 20; i >= 0; i--) {
if (depth[par[p][i]] >= depth[q]) {
p = par[p][i];
}
}
if (p == q) return p;
for (int i = 20; i >= 0; i--) {
if (par[p][i] != par[q][i]) {
p = par[p][i];
q = par[q][i];
}
}
return par[p][0];
}
ll distance(int u, int v) {
int p = lca(u, v);
return dis[u] - dis[p] + dis[v] - dis[p];
}
void makelca() {
dfs(1, 1);
for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= n; j++) {
par[j][i] = par[par[j][i - 1]][i - 1];
}
}
}
void Init(int _N, int _A[], int _B[], int _D[]) {
n = _N;
for (int i = 0; i < n; i++) {
int u = _A[i]; int v = _B[i]; int c = _D[i];
u++; v++;
a[u].push_back(mp(v, c));
a[v].push_back(mp(u, c));
}
makelca();
}
ll Query(int S, int X[], int T, int Y[]) {
// turn on the factories.h
ll ret = 1e18;
for (int i = 0; i < S; i++) X[i]++;
for (int i = 0; i < T; i++) Y[i]++;
for (int i = 0; i < S; i++) {
for (int j = 0; j < T; j++) {
ret = min(ret, distance(X[i], Y[j]));
}
}
return ret;
}
#ifdef UncleGrandpa
#define MAX_N 500000
#define MAX_Q 100000
#define MAX_SUM_ST 1000000
#define MAX_VALUE 1000000000
static int _N, _Q;
static int _A[MAX_N], _B[MAX_N], _D[MAX_N];
static int _S[MAX_N];
static int _T[MAX_N];
static int _X[MAX_SUM_ST];
static int _Y[MAX_SUM_ST];
static int _Qx[MAX_N];
static int _Qy[MAX_N];
#endif
#ifdef UncleGrandpa
int main() {
freopen("sample-in-01.txt", "r", stdin);
int i, j, k;
int STop, TTop;
if (2 != scanf("%d%d", &_N, &_Q)) {
fprintf(stderr, "error: cannot read _N and _Q.\n");
exit(1);
}
if (!(2 <= _N && _N <= MAX_N)) {
fprintf(stderr, "error: _N is out of bounds.\n");
exit(1);
}
if (!(1 <= _Q && _Q <= MAX_Q)) {
fprintf(stderr, "error: _Q is out of bounds.\n");
exit(1);
}
for (i = 0; i < _N - 1; ++i) {
if (1 != scanf("%d", &_A[i])) {
fprintf(stderr, "error: cannot read _A[%d].\n", i);
exit(1);
}
if (!(0 <= _A[i] && _A[i] <= _N - 1)) {
fprintf(stderr, "error: _A[%d] is out of bounds.\n", i);
exit(1);
}
if (1 != scanf("%d", &_B[i])) {
fprintf(stderr, "error: cannot read _B[%d].\n", i);
exit(1);
}
if (!(0 <= _B[i] && _B[i] <= _N - 1)) {
fprintf(stderr, "error: _B[%d] is out of bounds.\n", i);
exit(1);
}
if (_A[i] == _B[i]) {
fprintf(stderr, "error: _B[%d] is equal to _A[%d].\n", i, i);
exit(1);
}
if (1 != scanf("%d", &_D[i])) {
fprintf(stderr, "error: cannot read _D[%d].\n", i);
exit(1);
}
if (!(1 <= _D[i] && _D[i] <= MAX_VALUE)) {
fprintf(stderr, "error: _D[%d] is out of bounds.\n", i);
exit(1);
}
}
STop = 0;
TTop = 0;
for (j = 0; j < _Q; ++j) {
if (2 != scanf("%d%d", &_S[j], &_T[j])) {
fprintf(stderr, "error: cannot read L[%d] and R[%d].\n", j, j);
exit(1);
}
if (STop + _S[j] > MAX_SUM_ST) {
fprintf(stderr, "error: _S[0] + _S[1] + ... + _S[%d] is out of bounds.\n", j);
exit(1);
}
if (TTop + _T[j] > MAX_SUM_ST) {
fprintf(stderr, "error: _T[0] + _T[1] + ... + _T[%d] is out of bounds.\n", j);
exit(1);
}
for (k = 0; k < _S[j]; ++k, ++STop) {
if (1 != scanf("%d", &_X[STop])) {
fprintf(stderr, "error: cannot read _X[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= _X[STop] && _X[STop] <= _N - 1)) {
fprintf(stderr, "error: cannot read _X[%d][%d].\n", j, k);
exit(1);
}
}
for (k = 0; k < _T[j]; ++k, ++TTop) {
if (1 != scanf("%d", &_Y[TTop])) {
fprintf(stderr, "error: cannot read _Y[%d][%d].\n", j, k);
exit(1);
}
if (!(0 <= _Y[TTop] && _Y[TTop] <= _N - 1)) {
fprintf(stderr, "error: cannot read _Y[%d][%d].\n", j, k);
exit(1);
}
}
}
STop = 0;
TTop = 0;
Init(_N, _A, _B, _D);
for (j = 0; j < _Q; ++j) {
for (k = 0; k < _S[j]; k++) {
_Qx[k] = _X[STop++];
}
for (k = 0; k < _T[j]; k++) {
_Qy[k] = _Y[TTop++];
}
printf("%lld\n", Query(_S[j], _Qx, _T[j], _Qy));
}
return 0;
}
#endif
Compilation message (stderr)
factories.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<int>&)':
factories.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << sp;
^
factories.cpp: In function 'std::ostream& operator<<(std::ostream&, std::vector<std::pair<int, int> >&)':
factories.cpp:22:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < x.size(); i++) os << x[i] << endl;
^
factories.cpp: In function 'void dfs(int, int)':
factories.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[u].size(); i++) {
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |