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;
const int maxn = 120'001, mbit = 18;
int n, m;
int dth[maxn];
int S[maxn], T[maxn];
vector <vector <int> > G;
vector <int> w;
vector <vector <int> > g;
vector <vector <int> > stp;
void dfs_lca(int u = 1){
for(int i = 1; i < mbit; i++){
stp[u][i] = stp[stp[u][i - 1]][i - 1];
}
for(auto v : G[u]){
if(v != stp[u][0]){
stp[v][0] = u;
dth[v] = dth[u] + 1;
dfs_lca(v);
}
}
}
int LCA(int u, int v){
if(dth[u] < dth[v]) swap(u, v);
for(int i = mbit - 1; i >= 0; i--){
if(dth[u] - dth[v] >= (1 << i)) u = stp[u][i];
}
if(u == v) return u;
for(int i = mbit - 1; i >= 0; i--){
if(stp[u][i] != stp[v][i]){
u = stp[u][i];
v = stp[v][i];
}
}
return stp[u][0];
}
bool mid(int u, int v, int M){
return ((LCA(u, M) == M) || (LCA(v, M) == M)) && (dth[M] >= dth[LCA(u, v)]);
}
bool dfs_loop(int u){
if(w[u] == 1) return 1;
if(w[u] == 2) return 0;
w[u] = 1;
for(auto v : g[u]){
if(dfs_loop(v)) return 1;
}
w[u] = 2;
return 0;
}
bool sol(){
cin >> n;
G.assign(n + 1, vector<int>());
stp.assign(n + 1, vector<int>(mbit));
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dth[1] = 0;
dfs_lca();
//===============
cin >> m;
g.assign(m + 1, vector<int>());
for(int i = 0; i < m; i++){
cin >> S[i] >> T[i];
}
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
if(i == j) continue;
if(mid(S[i], T[i], S[j])) g[i].push_back(j);
if(mid(S[i], T[i], T[j])) g[j].push_back(i);
}
}
w.assign(m + 1, 0);
for(int i = 0; i < m; i++){
if(dfs_loop(i)) return 0;
}
return 1;
}
int main(){
int Q;
cin >> Q;
while(Q--){
cout << (sol() ? "Yes" : "No") << endl;
}
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... |