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 int long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
const int mxN = (int)1.2e5+10;
const int mxM = (int)510;
int n, m, Cycle;
int ind[mxN], dis[mxM*2][mxN];
int s[mxM], t[mxM], vis[mxM];
vector<int> adj[mxN], adj2[mxM];
void dfs(int s, int p, int i){
if(p==-1) dis[i][s] = 0;
else dis[i][s] = dis[i][p]+1;
for(auto u : adj[s])
if(u!=p) dfs(u,s,i);
}
bool inPath(int x, int s, int t){
return (dis[ind[s]][x]+dis[ind[t]][x]==dis[ind[s]][t]);
}
void dfs2(int s){
vis[s]=1;
for(auto u : adj2[s]){
if(!vis[u]) dfs2(u);
else if(vis[u]==1) { Cycle=1; return; }
}
vis[s]=2;
}
void solve(){
cin >> n; Cycle = 0;
for(int i = 1; i <= n; i++) adj[i].clear();
for(int i = 0; i < m; i++) adj2[i].clear();
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b;
adj[a].pb(b), adj[b].pb(a);
}
for(int i = 1; i <= n; i++) dfs(i,-1,i);
for(int i = 0; i < m; i++) vis[i]=0;
cin >> m;
vector<int> v; v.clear();
for(int i = 0; i < m; i++)
cin >> s[i] >> t[i], v.pb(s[i]), v.pb(t[i]);
sort(all(v)); v.erase(unique(all(v)),end(v));
for(int i = 0; auto u : v) ind[u]=i, dfs(u,-1,i), i++;
for(int i = 0; i < m; i++){
for(int j = 0; j < m; j++){
if(i!=j and inPath(s[j],s[i],t[i])) adj2[j].pb(i);
if(i!=j and inPath(t[j],s[i],t[i])) adj2[i].pb(j);
}
}
for(int i = 0; i < m; i++) if(!vis[i]) dfs2(i);
cout << (!Cycle?"Yes":"No") << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int t = 1; cin >> t; while(t--) solve();
}
Compilation message (stderr)
jail.cpp: In function 'void solve()':
jail.cpp:50:17: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
50 | for(int i = 0; auto u : v) ind[u]=i, dfs(u,-1,i), i++;
| ^~~~
# | 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... |