#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[200001];
map<int,int> ind[200001];
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23740 KB |
Output is correct |
4 |
Correct |
19 ms |
23840 KB |
Output is correct |
5 |
Correct |
29 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23796 KB |
Output is correct |
8 |
Correct |
14 ms |
23892 KB |
Output is correct |
9 |
Correct |
57 ms |
26912 KB |
Output is correct |
10 |
Correct |
74 ms |
47620 KB |
Output is correct |
11 |
Correct |
18 ms |
23764 KB |
Output is correct |
12 |
Correct |
57 ms |
24024 KB |
Output is correct |
13 |
Correct |
147 ms |
77008 KB |
Output is correct |
14 |
Correct |
194 ms |
92504 KB |
Output is correct |
15 |
Execution timed out |
5078 ms |
1048576 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
17 ms |
23920 KB |
Output is correct |
5 |
Correct |
18 ms |
23848 KB |
Output is correct |
6 |
Correct |
16 ms |
23848 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23892 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
14 ms |
23880 KB |
Output is correct |
11 |
Correct |
15 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23892 KB |
Output is correct |
13 |
Correct |
17 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
17 ms |
23920 KB |
Output is correct |
5 |
Correct |
18 ms |
23848 KB |
Output is correct |
6 |
Correct |
16 ms |
23848 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23892 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
14 ms |
23880 KB |
Output is correct |
11 |
Correct |
15 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23892 KB |
Output is correct |
13 |
Correct |
17 ms |
23892 KB |
Output is correct |
14 |
Correct |
14 ms |
23756 KB |
Output is correct |
15 |
Correct |
15 ms |
23816 KB |
Output is correct |
16 |
Correct |
15 ms |
23892 KB |
Output is correct |
17 |
Correct |
16 ms |
23920 KB |
Output is correct |
18 |
Correct |
16 ms |
23848 KB |
Output is correct |
19 |
Correct |
13 ms |
23776 KB |
Output is correct |
20 |
Correct |
14 ms |
23860 KB |
Output is correct |
21 |
Correct |
15 ms |
23892 KB |
Output is correct |
22 |
Correct |
14 ms |
23864 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23824 KB |
Output is correct |
25 |
Correct |
17 ms |
23832 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
16 ms |
23924 KB |
Output is correct |
28 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
17 ms |
23920 KB |
Output is correct |
5 |
Correct |
18 ms |
23848 KB |
Output is correct |
6 |
Correct |
16 ms |
23848 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23892 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
14 ms |
23880 KB |
Output is correct |
11 |
Correct |
15 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23892 KB |
Output is correct |
13 |
Correct |
17 ms |
23892 KB |
Output is correct |
14 |
Correct |
14 ms |
23756 KB |
Output is correct |
15 |
Correct |
15 ms |
23816 KB |
Output is correct |
16 |
Correct |
15 ms |
23892 KB |
Output is correct |
17 |
Correct |
16 ms |
23920 KB |
Output is correct |
18 |
Correct |
16 ms |
23848 KB |
Output is correct |
19 |
Correct |
13 ms |
23776 KB |
Output is correct |
20 |
Correct |
14 ms |
23860 KB |
Output is correct |
21 |
Correct |
15 ms |
23892 KB |
Output is correct |
22 |
Correct |
14 ms |
23864 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23824 KB |
Output is correct |
25 |
Correct |
17 ms |
23832 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
16 ms |
23924 KB |
Output is correct |
28 |
Correct |
13 ms |
23764 KB |
Output is correct |
29 |
Correct |
17 ms |
23968 KB |
Output is correct |
30 |
Correct |
14 ms |
23912 KB |
Output is correct |
31 |
Correct |
14 ms |
23972 KB |
Output is correct |
32 |
Correct |
14 ms |
23804 KB |
Output is correct |
33 |
Correct |
14 ms |
23892 KB |
Output is correct |
34 |
Correct |
15 ms |
23892 KB |
Output is correct |
35 |
Correct |
18 ms |
23892 KB |
Output is correct |
36 |
Correct |
14 ms |
23876 KB |
Output is correct |
37 |
Correct |
14 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
17 ms |
23920 KB |
Output is correct |
5 |
Correct |
18 ms |
23848 KB |
Output is correct |
6 |
Correct |
16 ms |
23848 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23892 KB |
Output is correct |
9 |
Correct |
14 ms |
23836 KB |
Output is correct |
10 |
Correct |
14 ms |
23880 KB |
Output is correct |
11 |
Correct |
15 ms |
23800 KB |
Output is correct |
12 |
Correct |
13 ms |
23892 KB |
Output is correct |
13 |
Correct |
17 ms |
23892 KB |
Output is correct |
14 |
Correct |
14 ms |
23756 KB |
Output is correct |
15 |
Correct |
15 ms |
23816 KB |
Output is correct |
16 |
Correct |
15 ms |
23892 KB |
Output is correct |
17 |
Correct |
16 ms |
23920 KB |
Output is correct |
18 |
Correct |
16 ms |
23848 KB |
Output is correct |
19 |
Correct |
13 ms |
23776 KB |
Output is correct |
20 |
Correct |
14 ms |
23860 KB |
Output is correct |
21 |
Correct |
15 ms |
23892 KB |
Output is correct |
22 |
Correct |
14 ms |
23864 KB |
Output is correct |
23 |
Correct |
13 ms |
23764 KB |
Output is correct |
24 |
Correct |
14 ms |
23824 KB |
Output is correct |
25 |
Correct |
17 ms |
23832 KB |
Output is correct |
26 |
Correct |
13 ms |
23764 KB |
Output is correct |
27 |
Correct |
16 ms |
23924 KB |
Output is correct |
28 |
Correct |
13 ms |
23764 KB |
Output is correct |
29 |
Correct |
17 ms |
23968 KB |
Output is correct |
30 |
Correct |
14 ms |
23912 KB |
Output is correct |
31 |
Correct |
14 ms |
23972 KB |
Output is correct |
32 |
Correct |
14 ms |
23804 KB |
Output is correct |
33 |
Correct |
14 ms |
23892 KB |
Output is correct |
34 |
Correct |
15 ms |
23892 KB |
Output is correct |
35 |
Correct |
18 ms |
23892 KB |
Output is correct |
36 |
Correct |
14 ms |
23876 KB |
Output is correct |
37 |
Correct |
14 ms |
23928 KB |
Output is correct |
38 |
Correct |
60 ms |
27156 KB |
Output is correct |
39 |
Correct |
72 ms |
48656 KB |
Output is correct |
40 |
Correct |
53 ms |
26160 KB |
Output is correct |
41 |
Correct |
49 ms |
25064 KB |
Output is correct |
42 |
Correct |
44 ms |
26464 KB |
Output is correct |
43 |
Correct |
36 ms |
25072 KB |
Output is correct |
44 |
Correct |
20 ms |
24396 KB |
Output is correct |
45 |
Correct |
83 ms |
39280 KB |
Output is correct |
46 |
Correct |
97 ms |
39236 KB |
Output is correct |
47 |
Correct |
70 ms |
42900 KB |
Output is correct |
48 |
Correct |
67 ms |
43000 KB |
Output is correct |
49 |
Correct |
70 ms |
39432 KB |
Output is correct |
50 |
Correct |
74 ms |
39360 KB |
Output is correct |
51 |
Correct |
82 ms |
40432 KB |
Output is correct |
52 |
Correct |
67 ms |
40652 KB |
Output is correct |
53 |
Correct |
26 ms |
24964 KB |
Output is correct |
54 |
Correct |
95 ms |
39240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23804 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
17 ms |
23764 KB |
Output is correct |
4 |
Correct |
14 ms |
23796 KB |
Output is correct |
5 |
Correct |
19 ms |
24032 KB |
Output is correct |
6 |
Correct |
14 ms |
23884 KB |
Output is correct |
7 |
Correct |
14 ms |
23892 KB |
Output is correct |
8 |
Correct |
14 ms |
23796 KB |
Output is correct |
9 |
Correct |
16 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23764 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
15 ms |
23920 KB |
Output is correct |
13 |
Correct |
32 ms |
24108 KB |
Output is correct |
14 |
Correct |
39 ms |
24012 KB |
Output is correct |
15 |
Correct |
36 ms |
23916 KB |
Output is correct |
16 |
Correct |
89 ms |
39536 KB |
Output is correct |
17 |
Correct |
184 ms |
46640 KB |
Output is correct |
18 |
Correct |
360 ms |
72008 KB |
Output is correct |
19 |
Correct |
114 ms |
40312 KB |
Output is correct |
20 |
Correct |
119 ms |
39692 KB |
Output is correct |
21 |
Correct |
83 ms |
39676 KB |
Output is correct |
22 |
Correct |
137 ms |
48328 KB |
Output is correct |
23 |
Correct |
129 ms |
48080 KB |
Output is correct |
24 |
Correct |
135 ms |
46388 KB |
Output is correct |
25 |
Correct |
125 ms |
46768 KB |
Output is correct |
26 |
Correct |
122 ms |
48320 KB |
Output is correct |
27 |
Correct |
191 ms |
49872 KB |
Output is correct |
28 |
Correct |
187 ms |
51848 KB |
Output is correct |
29 |
Correct |
167 ms |
50112 KB |
Output is correct |
30 |
Correct |
151 ms |
46256 KB |
Output is correct |
31 |
Correct |
125 ms |
46144 KB |
Output is correct |
32 |
Correct |
130 ms |
46336 KB |
Output is correct |
33 |
Correct |
139 ms |
45888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23740 KB |
Output is correct |
4 |
Correct |
19 ms |
23840 KB |
Output is correct |
5 |
Correct |
29 ms |
23888 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
13 ms |
23796 KB |
Output is correct |
8 |
Correct |
14 ms |
23892 KB |
Output is correct |
9 |
Correct |
57 ms |
26912 KB |
Output is correct |
10 |
Correct |
74 ms |
47620 KB |
Output is correct |
11 |
Correct |
18 ms |
23764 KB |
Output is correct |
12 |
Correct |
57 ms |
24024 KB |
Output is correct |
13 |
Correct |
147 ms |
77008 KB |
Output is correct |
14 |
Correct |
194 ms |
92504 KB |
Output is correct |
15 |
Execution timed out |
5078 ms |
1048576 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |