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>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 320;
int n, m, Q;
vector<int> ke[N];
pii qr[N];
int high[N], nC[N], pa[N], time_dfs;
int ein[N], Head[N];
int num_Ver = 0;
vector<int> ke2[N << 3];
int num[N << 3], low[N << 3 ], del[N << 3], time_dfs2 = 0;///aware
stack<int> st;
bool Ans;
struct Segment_Tree {
int m;
int st[N << 2], lab[N << 2];
void build (int id, int l, int r, int typ) {
++num_Ver;
lab[id] = num_Ver;
if (l == r) {
return;
}
int mid = l + r >> 1;
build (id << 1, l, mid, typ);
build (id << 1 | 1, mid + 1, r, typ);
if (typ == 0)
ke2[lab[id << 1]].pb(lab[id]), ke2[lab[id << 1 | 1]].pb(lab[id]);
else
ke2[lab[id]].pb(lab[id << 1]), ke2[lab[id]].pb(lab[id << 1 | 1]);
}
int get (int id, int l, int r, int pos) {
if (l > pos || r < pos) return -1;
if (l == r) return lab[id];
int mid = l + r >> 1;
return max(get (id << 1, l, mid, pos), get (id << 1 | 1, mid + 1, r, pos));
}
void in_add_edge (int id, int l, int r, int u, int v, int clab, int typ) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
if (typ == 0) ke2[lab[id]].pb(clab);
else ke2[clab].pb(lab[id]);
return;
}
int mid = l + r >> 1;
in_add_edge (id << 1, l, mid, u, v, clab, typ);
in_add_edge (id << 1 | 1, mid + 1, r, u, v, clab, typ);
}
void add_edge (int u, int v, int clab, int st, int ed, int typ) {
if (typ == 0) {
if (u == ein[st]) ++u;
if (v == ein[st]) --v;
}
else {
if (u == ein[ed]) ++u;
if (v == ein[ed]) --v;
}
in_add_edge (1, 1, m, u, v, clab, typ);
}
int get (int pos) {
return get (1, 1, m, pos);
}
void init (int n, int typ) {
m = n;
build (1, 1, m, typ);
}
}STs, STe;
void pdfs (int u, int p) {
high[u] = high[p] + 1;
pa[u] = p;
nC[u] = 1;
iter (&v, ke[u]) if (v != p) {
pdfs (v, u);
nC[u] += nC[v];
}
}
void hld (int u, int p) {
ein[u] = ++time_dfs;
Head[u] = p;
int mxV = -1;
iter (&v, ke[u]) if (v != pa[u] && (mxV == -1 || nC[v] > nC[mxV])) mxV = v;
if (mxV != -1) hld(mxV, p);
iter (&v, ke[u]) {
if (v == pa[u] || v == mxV) continue;
hld(v, v);
}
}
void query (int st, int ed) {
int u = st, v = ed, labs = STs.get(ein[st]), labe = STe.get(ein[ed]);
ke2[labe].pb(labs);
for (; Head[u] != Head[v]; v = pa[Head[v]]) {
if (high[Head[u]] > high[Head[v]]) swap(u, v);
STs.add_edge (ein[Head[v]], ein[v], labe, st, ed, 0);
STe.add_edge (ein[Head[v]], ein[v], labs, st, ed, 1);
}
if (high[u] > high[v]) swap(u, v);
STs.add_edge (ein[u], ein[v], labe, st, ed, 0);
STe.add_edge (ein[u], ein[v], labs, st, ed, 1);
}
void dfs (int u) {
num[u] = low[u] = ++time_dfs2;
st.push(u);
iter (&v, ke2[u]) {
if (del[v]) continue;
if (!num[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (num[u] == low[u]) {
int v;
int num_del = 0;
do {
v = st.top();
st.pop();
del[v] = 1;
++num_del;
}while (v != u);
if (num_del > 1) Ans = 0;
}
}
void solution () {
rep (i, 1, num_Ver) ke[i].clear(), ke2[i].clear(), num[i] = low[i] = del[i] = 0;
time_dfs = time_dfs2 = 0, Ans = 1, num_Ver = 0;
cin >> n;
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
}
pdfs(1, 0);
hld (1, 1);
STs.init(n, 0);
STe.init(n, 1);
cin >> m;
rep (i, 1, m) {
cin >> qr[i].fs >> qr[i].se;
query (qr[i].fs, qr[i].se);
}
rep (i, 1, num_Ver) {
if (!num[i]) dfs(i);
}
if (Ans) cout << "Yes\n";
else cout << "No\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +4
mình có muốn dùng mảng này không?
4 5
1 3
4 1
1 2
3 2
2 1
return->break
*/
Compilation message (stderr)
jail.cpp: In member function 'void Segment_Tree::build(int, int, int, int)':
jail.cpp:54:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int mid = l + r >> 1;
| ~~^~~
jail.cpp: In member function 'int Segment_Tree::get(int, int, int, int)':
jail.cpp:66:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = l + r >> 1;
| ~~^~~
jail.cpp: In member function 'void Segment_Tree::in_add_edge(int, int, int, int, int, int, int)':
jail.cpp:77:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | int mid = l + r >> 1;
| ~~^~~
# | 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... |