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>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)
const int N = 120'005;
vector<int> adj[N];
int st[N], en[N], h[N], anc[N][17], t;
void dfs(int i, int par, int d = 0) {
anc[i][0] = par;
for (int j = 1; j < 17; j++) {
anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
h[i] = d;
st[i] = t++;
for (auto nxt : adj[i]) {
if (nxt == par) continue;
dfs(nxt, i, d + 1);
}
en[i] = t - 1;
}
int getlca(int u, int v) {
if (st[u] <= st[v] && en[v] <= en[u]) {
return u;
}
for (int j = 16; j >= 0; j--) {
int nxtu = anc[u][j];
if (!(st[nxtu] <= st[v] && en[v] <= en[nxtu])) {
u = nxtu;
}
}
return anc[u][0];
}
struct path {
int s, e, lca;
} a[N];
const int inf = 1e9;
int n, m;
struct segtree {
array<int, 2> seg[1 << 18];
void build(int i, int l, int r) {
if (l == r) {
seg[i] = {-inf, -inf};
return;
}
build(i << 1, l, md);
build(i << 1 | 1, md + 1, r);
seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
void assign(int i, int l, int r, int x, array<int, 2> v) {
if (l == r) {
seg[i] = v;
return;
}
if (x <= md) {
assign(i << 1, l, md, x, v);
} else {
assign(i << 1 | 1, md + 1, r, x, v);
}
seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
void erase(int i, int l, int r, int x) {
if (l == r) {
seg[i] = {-inf, -inf};
return;
}
if (x <= md) {
erase(i << 1, l, md, x);
} else {
erase(i << 1 | 1, md + 1, r, x);
}
seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
}
array<int, 2> query(int i, int l, int r, int s, int e) {
if (s <= l && r <= e) {
return seg[i];
}
if (r < s || e < l) {
return {-inf, -inf};
}
return max(query(i << 1, l, md, s, e), query(i << 1 | 1, md + 1, r, s, e));
}
void assign(int i, array<int, 2> v) {
assign(1, 0, n - 1, i, v);
}
void erase(int i) {
erase(1, 0, n - 1, i);
}
array<int, 2> query(int l, int r) {
array<int, 2> ret = query(1, 0, n - 1, l, r);
return ret;
}
void init() {
build(1, 0, n - 1);
}
} seg1, seg2, seg3;
bool vis[N], instack[N];
void assign(int i) {
vis[i] = 0;
seg1.assign(st[a[i].s], {en[a[i].s], i});
seg2.assign(st[a[i].s], {-st[a[i].lca], i});
seg3.assign(st[a[i].e], {-st[a[i].lca], i});
}
void rem(int i) {
vis[i] = 1;
seg1.erase(st[a[i].s]);
seg2.erase(st[a[i].s]);
seg3.erase(st[a[i].e]);
}
bool dfs2(int i) {
instack[i] = 1;
bool ok = 1;
rem(i);
auto go = [&] (int j) {
if (instack[j]) {
ok = 0;
return;
}
assign(i);
bool ret = dfs2(j);
rem(i);
if (!ret) {
ok = 0;
}
};
array<int, 2> val;
val = seg1.query(st[a[i].lca], st[a[i].s]);
while (ok && val[0] >= st[a[i].s]) {
int j = val[1];
go(j);
val = seg1.query(st[a[i].lca], st[a[i].s]);
}
val = seg1.query(st[a[i].lca], st[a[i].e]);
while (ok && val[0] >= st[a[i].e]) {
int j = val[1];
go(j);
val = seg1.query(st[a[i].lca], st[a[i].e]);
}
val = seg2.query(st[a[i].e], en[a[i].e]);
val[0] *= -1;
while (ok && val[0] <= st[a[i].e]) {
int j = val[1];
go(j);
val = seg2.query(st[a[i].e], en[a[i].e]);
val[0] *= -1;
}
val = seg3.query(st[a[i].e], en[a[i].e]);
val[0] *= -1;
while (ok && val[0] <= st[a[i].e]) {
int j = val[1];
go(j);
val = seg3.query(st[a[i].e], en[a[i].e]);
val[0] *= -1;
}
instack[i] = 0;
return ok;
// for (int j = 0; j < m; j++) {
// if (j == i) continue;
// if (vis[j]) continue;
// bool gud = 0;
// gud |= st[a[i].lca] <= st[a[j].s] && st[a[j].s] <= st[a[i].s] && en[a[j].s] >= st[a[i].s]; //segtree 1
// gud |= st[a[i].lca] <= st[a[j].s] && st[a[j].s] <= st[a[i].e] && en[a[j].s] >= st[a[i].e]; //segtree 1
// gud |= st[a[i].e] <= st[a[j].s] && st[a[j].s] <= en[a[i].e] && st[a[j].lca] <= st[a[i].e]; //segtree 2
// gud |= st[a[i].e] <= st[a[j].e] && st[a[j].e] <= en[a[i].e] && st[a[j].lca] <= st[a[i].e]; //segtree 3
// if (gud) {
// if (instack[j]) {
// ok = 0;
// break;
// }
// if (!dfs2(j)) {
// ok = 0;
// break;
// }
// }
// }
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; i++) {
adj[i].clear();
vis[i] = 0;
instack[i] = 0;
}
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
t = 0;
dfs(0, 0);
cin >> m;
for (int i = 0; i < m; i++) {
cin >> a[i].s >> a[i].e;
a[i].s--, a[i].e--;
a[i].lca = getlca(a[i].s, a[i].e);
// cout << a[i].s + 1 << ' ' << a[i].e + 1 << ' ' << a[i].lca + 1 << '\n';
}
seg1.init();
seg2.init();
seg3.init();
for (int i = 0; i < m; i++) {
assign(i);
}
bool ok = 1;
for (int i = 0; i < m; i++) {
if (vis[i]) continue;
if (!dfs2(i)) {
ok = 0;
}
}
cout << (ok? "Yes\n" : "No\n");
}
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |