QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#692715 | #5439. Meet in the Middle | mfeitveer | ML | 0ms | 0kb | C++14 | 6.4kb | 2024-10-31 14:56:48 | 2024-10-31 14:57:01 |
answer
/*
! 前途似海,来日方长。
! Created: 2024/10/31 08:41:56
*/
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
// #define int long long
#define mp(x, y) make_pair(x, y)
#define eb(...) emplace_back(__VA_ARGS__)
#define fro(i, x, y) for (int i = (x); i <= (y); i++)
#define pre(i, x, y) for (int i = (x); i >= (y); i--)
inline void JYFILE19();
using i64 = long long;
using pii = pair<int, int>;
bool ST;
const int N = 1e5 + 10;
const int mod = 998244353;
int n, q, tt, ct, tp, rt, rn, al, tl;
int h1[N], h2[N], h3[N << 1];
int dn[N], id[N], sz[N], st[20][N];
i64 ds[N], sd[N << 1];
int ff[N << 2], wl[N << 2], de[N << 2];
int la[N << 2], lb[N << 2], ls[N << 2];
int ra[N << 2], rb[N << 2], rs[N << 2];
int vs[N << 4];
int cr[N];
struct edge { int to, nxt, val; } e[N << 4];
vector<i64> ps[N];
inline void add(int x, int y, int z) {
e[++ct] = {y, h3[x], z}, h3[x] = ct;
e[++ct] = {x, h3[y], z}, h3[y] = ct;
}
inline void dfs1(int x, int fa) {
int z = 0;
for (int i = h1[x]; i; i = e[i].nxt) {
if (e[i].to == fa) continue;
int y = e[i].to;
int w = e[i].val;
dfs1(y, x);
if (!z) {
add(x, y, w);
z = x;
} else {
int u = ++tt;
add(u, z, 0);
add(y, u, w);
z = u;
}
}
}
inline void dfs2(int x, int fa) {
st[0][dn[x] = ++tp] = fa;
for (int i = h2[x]; i; i = e[i].nxt) {
if (e[i].to == fa) continue;
ds[e[i].to] = ds[x] + e[i].val;
dfs2(e[i].to, x);
}
}
inline int chk(int x, int y) {
return (dn[x] < dn[y] ? x : y);
}
inline void init() {
fro(i, 1, 19)
fro(j, 1, n - (1 << i) + 1)
st[i][j] = chk(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);
}
inline int lca(int x, int y) {
if (x == y) return x;
if((x = dn[x]) > (y = dn[y])) swap(x, y);
int k = __lg(y - x++);
return chk(st[k][x], st[k][y - (1 << k) + 1]);
}
inline i64 dis(int x, int y) {
return ds[x] + ds[y] - 2 * ds[lca(x, y)];
}
inline void dfs3(int x, int fa) {
sz[x] = 1;
for (int i = h3[x]; i; i = e[i].nxt) {
if (e[i].to == fa || vs[i]) continue;
dfs3(e[i].to, x);
sz[x] += sz[e[i].to];
}
}
inline void dfs4(int x, int fa) {
for (int i = h3[x]; i; i = e[i].nxt) {
if (e[i].to == fa || vs[i]) continue;
int siz = max(sz[e[i].to], al - sz[e[i].to]);
if (siz < rn) {
rn = siz;
rt = i;
}
dfs4(e[i].to, x);
}
}
inline void dfs5(int x, int fa) {
if (x <= n) cr[++tl] = x;
for (int i = h3[x]; i; i = e[i].nxt) {
if (e[i].to == fa || vs[i]) continue;
sd[e[i].to] = sd[x] + e[i].val;
dfs5(e[i].to, x);
}
}
inline int calc(int p, int o) {
rn = 1e9, rt = 0;
dfs3(p, 0);
al = sz[p];
dfs4(p, 0);
if (al == 1) return p;
vs[rt] = vs[rt ^ 1] = 1;
int x = e[rt].to;
int y = e[rt ^ 1].to;
int d = ++tt;
wl[d] = e[rt].val;
i64 vl;
vl = -1;
tl = 0, sd[x] = 0, dfs5(x, 0);
for (int j = 1; j <= tl; j++) {
int i = cr[j];
ps[i].eb(sd[i]);
if (la[d] == 0) { la[d] = i; continue; }
if (lb[d] == 0) { lb[d] = i; continue; }
i64 las = (vl == -1 ? dis(la[d], lb[d]) + sd[la[d]] + sd[lb[d]] : vl);
i64 cr1 = dis(la[d], i) + sd[la[d]] + sd[i];
i64 cr2 = dis(lb[d], i) + sd[lb[d]] + sd[i];
i64 mx = max({cr1, cr2, las});
if (cr1 == mx) lb[d] = i; else if (cr2 == mx) la[d] = i;
vl = mx;
}
vl = -1;
tl = 0, sd[y] = 0, dfs5(y, 0);
for (int j = 1; j <= tl; j++) {
int i = cr[j];
ps[i].eb(sd[i]);
if (ra[d] == 0) { ra[d] = i; continue; }
if (rb[d] == 0) { rb[d] = i; continue; }
i64 las = (vl == -1 ? dis(ra[d], rb[d]) + sd[ra[d]] + sd[rb[d]] : vl);
i64 cr1 = dis(ra[d], i) + sd[ra[d]] + sd[i];
i64 cr2 = dis(rb[d], i) + sd[rb[d]] + sd[i];
i64 mx = max({cr1, cr2, las});
if (cr1 == mx) rb[d] = i; else if (cr2 == mx) ra[d] = i;
vl = mx;
}
ff[ls[d] = calc(x, o + 1)] = d;
ff[rs[d] = calc(y, o + 1)] = d;
return de[d] = o, d;
}
namespace IO {
char buf[1<<21], s[1<<21], *p1, *p2;
#define gc() (p1==p2&&(p2=(p1=buf)+cin.rdbuf()->sgetn(buf,1<<21),p1==p2)?EOF:*p1++)
template<typename T> inline void read(T &x) {
x = 0; int q = 1; char z;
while(!isdigit(z = gc())) if(z == '-') q = -1;
while(isdigit(z)) x = x * 10 + (z ^ 48), z = gc(); x *= q;
}
template<typename T, typename ...Args> inline void read(T &x, Args &...args) { read(x), read(args...); }
template<typename T> inline void put(T a) {
int tp = 0; if(a < 0) cout.rdbuf()->sputc('-'), a = -a;
if(!a) return cout.rdbuf()->sputc('0'), void();
while(a) s[++tp] = a % 10 + 48 , a /= 10;
while(tp) cout.rdbuf()->sputc(s[tp--]);
}
inline void put(const char *a) { while(*a) { cout.rdbuf()->sputc(*(a++)); } }
template<typename T, typename ...Args> inline void put(T x, Args ...args) { put(x), put(args...); }
} using IO::put; using IO::read;
inline void solve() {
read(n), tt = n;
fro(i, 2, n) {
int u, v, w;
read(u, v, w);
e[++ct] = {u, h1[v], w}, h1[v] = ct;
e[++ct] = {v, h1[u], w}, h1[u] = ct;
}
fro(i, 2, n) {
int u, v, w;
read(u, v, w);
e[++ct] = {u, h2[v], w}, h2[v] = ct;
e[++ct] = {v, h2[u], w}, h2[u] = ct;
}
if (ct % 2 == 0) ct++;
dfs1(1, 0);
dfs2(1, 0);
init();
calc(1, 0);
read(q);
fro(i, 1, q) {
int a, b;
read(a, b);
i64 ns = dis(a, b);
for (int j = a; ff[j]; j = ff[j]) {
int f = ff[j], e = de[f];
if (ls[f] == j) {
if (ra[f]) ns = max(ns, dis(ra[f], b) + wl[f] + ps[a][e] + ps[ra[f]][e]);
if (rb[f]) ns = max(ns, dis(rb[f], b) + wl[f] + ps[a][e] + ps[rb[f]][e]);
} else {
if (la[f]) ns = max(ns, dis(la[f], b) + wl[f] + ps[a][e] + ps[la[f]][e]);
if (lb[f]) ns = max(ns, dis(lb[f], b) + wl[f] + ps[a][e] + ps[lb[f]][e]);
}
}
put(ns, "\n");
}
fro(i, 1, n) {
h1[i] = h2[i] = dn[i] = id[i] = ds[i] = 0;
ps[i].clear();
fro(j, 0, 19) st[j][i] = 0;
}
fro(i, 1, tt) {
la[i] = lb[i] = ra[i] = rb[i] = h3[i] = 0;
ls[i] = rs[i] = ff[i] = wl[i] = de[i] = 0;
}
fro(i, 1, ct) {
vs[i] = 0;
}
tt = ct = tp = 0;
}
signed main() {
JYFILE19();
int t;
cin >> t;
while (t--) solve();
return 0;
}
bool ED;
inline void JYFILE19() {
srand(random_device{}());
ios::sync_with_stdio(0), cin.tie(0);
double MIB = fabs((&ED - &ST) / 1048576.), LIM = 128;
cerr << "MEMORY: " << MIB << endl, assert(MIB <= LIM);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2