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 MX=1.2e5+5;
int Q,N,M;
int S[MX], T[MX];
vector<int> path[MX]; // store the path between Si and Ti
vector<int> adj[MX];
int par[MX];
void dfs(int v, int p) {
par[v]=p;
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u,v);
}
}
vector<int> ADJ[MX];
int deg[MX];
bool f(int i, int x) {
for(auto y:path[i])
if(y==x) return true;
return false;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>Q;
while(Q--) {
cin>>N;
for(int i=1;i<=N;i++) adj[i].clear();
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++) path[i].clear(), ADJ[i].clear(), deg[i]=0;
for(int i=0;i<M;i++) {
cin>>S[i]>>T[i];
dfs(S[i],S[i]);
int p=T[i];
while(p!=S[i]) {
path[i].push_back(p);
p=par[p];
}
path[i].push_back(S[i]);
}
for(int i=0;i<M;i++) {
for(int j=i+1;j<M;j++) {
// check start
if(f(i,S[j])) {
ADJ[j].push_back(i);
deg[i]+=1;
}
if(f(j,S[i])) {
ADJ[i].push_back(j);
deg[j]+=1;
}
// check end
if(f(i,T[j])) {
ADJ[i].push_back(j);
deg[j]+=1;
}
if(f(j,T[i])) {
ADJ[j].push_back(i);
deg[i]+=1;
}
}
}
queue<int> q;
for(int i=0;i<M;i++) {
if(!deg[i]) {
q.push(i);
}
}
int ans=0;
while(!q.empty()) {
auto v=q.front(); q.pop();
ans+=1;
for(auto u:ADJ[v]) {
deg[u]--;
if(!deg[u]) q.push(u);
}
}
cout<<(ans==M?"Yes":"No")<<'\n';
}
}
# | 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... |