This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
constexpr ll inf = 1e9, N = 2e5 + 10, mod = 1e9 + 7, Log = 20;
/*
val -> number of path
type -> (1 == bed) or (2 == work)
*/
int n, m;
int type[N << 2][4], val[N << 2][4];
int sz[N], st[N], fn[N], head[N], ptr[N], tim;
int par[N][Log], h[N << 2], cnt_t;
bool mark[N << 2];
map<int, int> mp, vis;
vector<int> adj[N << 2], G[N << 2], RG[N << 2], topol;
vector<pair<int, int> > path;
struct Node {
int num;
};
struct Seg {
Node x[N << 2];
int typ;
Seg(int type) {
typ = type;
}
void pre(int l, int r, int v) {
if(r - l < 2) {
if(type[ptr[l]][typ] == 0) x[v].num = v + (typ - 1) * N;
else {
if(mp[val[ptr[l]][typ]] != 0) x[v].num = mp[val[ptr[l]][typ]];
else x[v].num = mp[val[ptr[l]][typ]] = v;
vis[x[v].num] = 1;
}
return;
}
int mid = (l + r) >> 1;
pre(l, mid, 2 * v), pre(mid, r, 2 * v + 1);
x[v].num = v + (typ - 1) * N;
}
void build(int l, int r, int v) {
if(r - l < 2) {
return;
}
int mid = (l + r) >> 1;
build(l, mid, 2 * v), build(mid, r, 2 * v + 1);
if(typ == 1) {
if(mid - l >= 2 || (type[ptr[l]][typ])) {
/*wtf(typ);
cout<<"^^" <<x[2 * v].num <<" " <<x[v].num <<" " <<endl;*/
G[x[2 * v].num].push_back(x[v].num), RG[x[v].num].push_back(x[2 * v].num);
}
if(r - mid >= 2 || (type[ptr[mid]][typ])) {
/*wtf(typ);
cout<<"^^" <<x[2 * v + 1].num <<" " <<x[v].num <<endl;*/
G[x[2 * v + 1].num].push_back(x[v].num), RG[x[v].num].push_back(x[2 * v + 1].num);
}
} else {
if(mid - l >= 2 || (type[ptr[l]][typ])) {
/*wtf(typ);
cout<<"^^" <<" " <<x[v].num <<" " <<x[2 * v].num <<endl;*/
G[x[v].num].push_back(x[2 * v].num), RG[x[2 * v].num].push_back(x[v].num);
}
if(r - mid >= 2 || (type[ptr[mid]][typ])) {
/*wtf(typ);
cout<<"^^" <<" " <<x[v].num <<" " <<x[2 * v + 1].num <<endl;*/
G[x[v].num].push_back(x[2 * v + 1].num), RG[x[2 * v + 1].num].push_back(x[v].num);
}
}
}
void upd(int l, int r, int s, int e, int cnt, int v) {
if(r <= s || l >= e) return;
if(l >= s && r <= e) {
if(r - l < 2 && !type[ptr[l]][typ]) return;
if(typ == 1) {
G[x[v].num].push_back(cnt);
RG[cnt].push_back(x[v].num);
} else {
G[cnt].push_back(x[v].num);
RG[x[v].num].push_back(cnt);
}
return;
}
int mid = (l + r) >> 1;
upd(l, mid, s, e, cnt, 2 * v), upd(mid, r, s, e, cnt, 2 * v + 1);
}
};
void get_input() {
cin >>n;
for(int i = 0; i < n - 1; i++) {
int u, v; cin >>u >>v;
adj[u].push_back(v), adj[v].push_back(u);
}
cin >>m;
for(int i = 0; i < m; i++) {
int s, t; cin >>s >>t;
path.push_back({s, t});
type[s][1] = 1, type[t][2] = 1;
val[s][1] = val[t][2] = i;
}
}
void pre_dfs(int x = 1, int p = 0) {
sz[x] = 1;
for(auto j : adj[x]) {
if(j == p) continue;
pre_dfs(j, x);
sz[x] += sz[j];
}
}
void hld(int x = 1, int he = 1, int p = 0) {
ptr[tim] = x, st[x] = tim++, head[x] = he;
h[x] = h[p] + 1;
par[x][0] = p;
for(int i = 1; i < Log; i++) par[x][i] = par[par[x][i - 1]][i - 1];
int bs = 0;
for(auto j : adj[x]) {
if(j == p) continue;
if(sz[j] >= sz[bs]) bs = j;
}
fn[x] = tim;
if(bs == 0) return;
hld(bs, he, x);
for(auto j : adj[x]) {
if(j == p || j == bs) continue;
hld(j, j, x);
}
}
int get_par(int x, int y) {
for(int i = 0; i < Log; i++) if((y >> i) & 1) x = par[x][i];
return x;
}
int lca(int x, int y) {
if(h[x] > h[y]) swap(x, y);
y = get_par(y, h[y] - h[x]);
if(x == y) return x;
for(int i = Log - 1; i >= 0; i--) {
if(par[x][i] != par[y][i])
x = par[x][i], y = par[y][i];
}
return par[x][0];
}
void dfs_scc(int x) {
mark[x] = 1;
// cout<<x <<" ";
for(auto j : G[x]) {
if(mark[j]) continue;
dfs_scc(j);
}
topol.push_back(x);
}
void sfd_scc(int x) {
mark[x] = 1;
if(vis[x]) {
cnt_t++;
}
for(auto j : RG[x]) {
if(mark[j]) continue;
sfd_scc(j);
}
}
void kill() {
tim = 0;
path.clear();
for(int i = 1; i <= 4 * n; i++) {
G[i].clear(), RG[i].clear(), G[i + N].clear(), RG[i + N].clear();
adj[i].clear();
mark[i] = 0, mark[i + N] = 0;
type[i][1] = type[i][2] = 0;
val[i][1] = val[i][2] = 0;
}
mp.clear();
vis.clear();
topol.clear();
}
int main() {
ios :: sync_with_stdio(0), cin.tie(0);
int q; cin >>q;
while(q--) {
kill();
get_input();
Seg X(1), Y(2);
pre_dfs();
hld();
X.pre(0, n, 1), Y.pre(0, n, 1);
X.build(0, n, 1), Y.build(0, n, 1);
int mx = 0;
for(int j = 0; j < Sz(path); j++) {
int s = path[j].first, t = path[j].second;
if(h[s] < h[t]) swap(s, t);
int lc = lca(s, t);
while(h[head[s]] >= h[lc]) {
//cout<<"^^" <<head[s] <<" " <<s <<" " <<mp[j] <<endl;
X.upd(0, n, st[head[s]], st[s] + 1, mp[j], 1);
Y.upd(0, n, st[head[s]], st[s] + 1, mp[j], 1);
s = par[head[s]][0];
}
if(h[s] >= h[lc]) {
//cout<<"^^" <<lc <<" " <<s <<" " <<mp[j] <<endl;
X.upd(0, n, st[lc], st[s] + 1, mp[j], 1);
Y.upd(0, n, st[lc], st[s] + 1, mp[j], 1);
}
while(h[head[t]] >= h[lc]) {
//cout<<"^^" <<head[t] <<" " <<t <<" " <<mp[j] <<endl;
X.upd(0, n, st[head[t]], st[t] + 1, mp[j], 1);
Y.upd(0, n, st[head[t]], st[t] + 1, mp[j], 1);
t = par[head[t]][0];
}
if(h[t] >= h[lc]) {
//cout<<"^^" <<lc <<" " <<t <<" " <<mp[j] <<endl;
X.upd(0, n, st[lc], st[t] + 1, mp[j], 1);
Y.upd(0, n, st[lc], st[t] + 1, mp[j], 1);
}
mx = max(mx, mp[j]);
}
for(int i = 1; i <= mx; i++) {
if(!mark[i]) dfs_scc(i);
//cout<<endl <<"_____" <<endl;
}
memset(mark, 0, sizeof(mark));
reverse(All(topol));
bool flag = false;
for(auto i : topol) {
if(!mark[i]) {
cnt_t = 0;
sfd_scc(i);
if(cnt_t > 1) {
flag = true;
break;
}
}
}
(flag) ? (cout<<"No\n") : (cout<<"Yes\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... |