#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
const llo mod=1e9+7;
int q;
int ee;
vector<int> adj[200001];
int cc[200001];
int dd[200001];
int vis[200001];
vector<int> ss;
vector<int> tt;
vector<int> pre[501];
map<int,int> ind[501];
int ind2[200001];
int ind3[200001];
vector<int> adj2[200001];
int vis2[200001];
int vis3[200001];
void dfs(int no,int par2=-1){
ss.pb(no);
if(ee==no){
tt=ss;
}
for(auto j:adj[no]){
if(j!=par2){
dfs(j,no);
}
}
ss.pop_back();
}
int stt=1;
void dfs2(int no){
vis2[no]=1;
vis3[no]=1;
for(auto j:adj2[no]){
if(vis3[j]==1){
stt=0;
}
if(vis2[j]==0){
dfs2(j);
}
}
vis3[no]=0;
}
int par[120001][20];
int up[120001];
int lev[120001];
void dfs3(int no,int par2=-1,int xx=-1,int levv=0){
par[no][0]=par2;
up[no]=xx;
lev[no]=levv;
if(ind2[no]>=0 or ind3[no]>=0){
xx=no;
}
for(auto j:adj[no]){
if(j!=par2){
dfs3(j,no,xx,levv+1);
}
}
}
int lca(int aa,int bb){
if(lev[bb]<lev[aa]){
swap(aa,bb);
}
//cout<<lev[bb]<<",,"<<lev[aa]<<endl;
int dif=lev[bb]-lev[aa];
//cout<<dif<<",,"<<endl;
for(int j=19;j>=0;j--){
if((1<<j)&dif){
bb=par[bb][j];
}
}
/* cout<<bb<<",,"<<par[bb][0]<<endl;
cout<<dif<<",,"<<endl;
cout<<aa<<",,"<<bb<<endl;*/
if(aa==bb){
return aa;
}
for(int j=19;j>=0;j--){
if(par[aa][j]!=par[bb][j]){
aa=par[aa][j];
bb=par[bb][j];
}
}
return par[aa][0];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>q;
while(q--){
int n;
cin>>n;
for(int i=0;i<n;i++){
adj[i].clear();
adj2[i].clear();
}
for(int i=0;i<n-1;i++){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
adj[aa].pb(bb);
adj[bb].pb(aa);
}
int m;
cin>>m;
stt=1;
for(int i=0;i<n;i++){
ind2[i]=-1;
ind3[i]=-1;
}
for(int i=0;i<m;i++){
cin>>cc[i]>>dd[i];
ind[i].clear();
cc[i]--;
dd[i]--;
ind2[dd[i]]=i;
ind3[cc[i]]=i;
ee=dd[i];
/* dfs(cc[i]);
pre[i]=tt;
for(auto j:tt){
ind[i][j]++;
}*/
}
dfs3(0);
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1){
par[i][j]=-1;
}
else{
par[i][j]=par[par[i][j-1]][j-1];
}
}
}
for(int i=0;i<m;i++){
int x=lca(cc[i],dd[i]);
/// cout<<cc[i]<<":"<<dd[i]<<":"<<x<<endl;
int j=cc[i];
while(j!=-1){
if(lev[j]<lev[x]){
break;
}
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
//cout<<ind2[j]<<":"<<i<<endl;
}
j=up[j];
}
j=dd[i];
while(j!=-1){
if(lev[j]<lev[x]){
break;
}
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
//cout<<ind2[j]<<":"<<i<<endl;
}
j=up[j];
}
//cout<<endl;
/* for(auto j:pre[i]){
if(ind3[j]>=0 and ind3[j]!=i){
adj2[i].pb(ind3[j]);
//cout<<i<<":"<<ind3[j]<<endl;
}
if(ind2[j]>=0 and ind2[j]!=i){
adj2[ind2[j]].pb(i);
// cout<<ind2[j]<<":"<<i<<endl;
}
}*/
}
for(int i=0;i<m;i++){
vis2[i]=0;
}
for(int i=0;i<m;i++){
if(vis2[i]==0){
dfs2(i);
}
}
if(stt==0){
cout<<"No"<<endl;
}
else{
cout<<"Yes"<<endl;
}
/* string ans="No";
vector<int> ss;
for(int i=0;i<m;i++){
ss.pb(i);
}
while(true){
for(int j=0;j<n;j++){
vis[j]=0;
}
for(int j=0;j<m;j++){
vis[cc[j]]=1;
}
int ok=0;
for(auto j:ss){
vis[cc[j]]=0;
for(auto i:pre[j]){
if(vis[i]==1){
ok++;
}
}
vis[dd[j]]=1;
}
if(ok==0){
ans="Yes";
}
if(next_permutation(ss.begin(),ss.end())){
}
else{
break;
}
}
cout<<ans<<endl;*/
/*for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
int co=0;
if(i==j){
continue;
}
if(ind[j].find(cc[i])!=ind[j].end()){
if(ind[j].find(dd[i])!=ind[j].end()){
ans="No";
}
}
for(auto jj:pre[j]){
if(ind[i].find(dd[j]))
}
if(ind[j].find(dd[i])!=ind[j].end()){
if(ind[i].find(pre[j].back())!=ind[i].end()){
ans="No";
break;
}
}
}
}*/
// cout<<ans<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
16 ms |
9960 KB |
Output is correct |
5 |
Correct |
25 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9780 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
46 ms |
12900 KB |
Output is correct |
10 |
Correct |
61 ms |
34232 KB |
Output is correct |
11 |
Correct |
10 ms |
9964 KB |
Output is correct |
12 |
Correct |
41 ms |
10776 KB |
Output is correct |
13 |
Runtime error |
51 ms |
32020 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9808 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9772 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9840 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9808 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9772 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9840 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9796 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9772 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9844 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9808 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9772 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9840 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9796 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9772 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9844 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9788 KB |
Output is correct |
31 |
Correct |
9 ms |
9812 KB |
Output is correct |
32 |
Correct |
7 ms |
9848 KB |
Output is correct |
33 |
Correct |
6 ms |
9764 KB |
Output is correct |
34 |
Correct |
7 ms |
9904 KB |
Output is correct |
35 |
Correct |
8 ms |
9916 KB |
Output is correct |
36 |
Correct |
6 ms |
9812 KB |
Output is correct |
37 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9808 KB |
Output is correct |
3 |
Correct |
6 ms |
9816 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9772 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9840 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9796 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
6 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9772 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9844 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
5 ms |
9684 KB |
Output is correct |
24 |
Correct |
5 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
6 ms |
9812 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
29 |
Correct |
8 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9788 KB |
Output is correct |
31 |
Correct |
9 ms |
9812 KB |
Output is correct |
32 |
Correct |
7 ms |
9848 KB |
Output is correct |
33 |
Correct |
6 ms |
9764 KB |
Output is correct |
34 |
Correct |
7 ms |
9904 KB |
Output is correct |
35 |
Correct |
8 ms |
9916 KB |
Output is correct |
36 |
Correct |
6 ms |
9812 KB |
Output is correct |
37 |
Correct |
6 ms |
9812 KB |
Output is correct |
38 |
Correct |
48 ms |
12988 KB |
Output is correct |
39 |
Correct |
62 ms |
34488 KB |
Output is correct |
40 |
Correct |
52 ms |
13020 KB |
Output is correct |
41 |
Correct |
35 ms |
11492 KB |
Output is correct |
42 |
Correct |
43 ms |
13040 KB |
Output is correct |
43 |
Correct |
37 ms |
11944 KB |
Output is correct |
44 |
Correct |
13 ms |
10300 KB |
Output is correct |
45 |
Correct |
68 ms |
26188 KB |
Output is correct |
46 |
Correct |
69 ms |
26188 KB |
Output is correct |
47 |
Correct |
73 ms |
29848 KB |
Output is correct |
48 |
Correct |
66 ms |
29916 KB |
Output is correct |
49 |
Correct |
65 ms |
26364 KB |
Output is correct |
50 |
Correct |
62 ms |
26256 KB |
Output is correct |
51 |
Correct |
55 ms |
26448 KB |
Output is correct |
52 |
Correct |
59 ms |
27212 KB |
Output is correct |
53 |
Correct |
15 ms |
10836 KB |
Output is correct |
54 |
Correct |
76 ms |
26184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
10 ms |
9764 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9780 KB |
Output is correct |
10 |
Correct |
7 ms |
9824 KB |
Output is correct |
11 |
Correct |
6 ms |
9760 KB |
Output is correct |
12 |
Correct |
6 ms |
9812 KB |
Output is correct |
13 |
Correct |
23 ms |
9836 KB |
Output is correct |
14 |
Correct |
28 ms |
9812 KB |
Output is correct |
15 |
Correct |
26 ms |
9812 KB |
Output is correct |
16 |
Runtime error |
92 ms |
52236 KB |
Execution killed with signal 6 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9728 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
16 ms |
9960 KB |
Output is correct |
5 |
Correct |
25 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9780 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
46 ms |
12900 KB |
Output is correct |
10 |
Correct |
61 ms |
34232 KB |
Output is correct |
11 |
Correct |
10 ms |
9964 KB |
Output is correct |
12 |
Correct |
41 ms |
10776 KB |
Output is correct |
13 |
Runtime error |
51 ms |
32020 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |