#include <bits/stdc++.h>
using namespace std;
#define int long long
const int modulo = 1e9+7;
void solve(){
int n;
cin >> n;
vector<vector<int>> adj(n+1);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
vector<int> depth(n+1);
vector<vector<int>> p(n+1,vector<int>(17));
function<void(int,int,int)> dfs = [&](int x,int par,int dep){
depth[x]=dep;
p[x][0]=par;
for(int&i:adj[x])if(i!=par)dfs(i,x,dep+1);
};
dfs(1,0,1);
for(int bit=1;bit<17;bit++){
for(int i=1;i<=n;i++){
p[i][bit] = p[p[i][bit-1]][bit-1];
}
}
auto lift = [&](int x,int tar){
for(int bit=0;bit<17;bit++)if(tar&(1<<bit))x=p[x][bit];
return x;
};
int m;
cin >> m;
vector<vector<int>> secadj(m+1);
vector<int> s(m+1);
vector<int> t(m+1);
auto collides = [&](int curr,int v){
int a = s[curr];
int b = t[curr];
if(a==v or b==v)return true;
bool worksa = false;
if(depth[a]>=depth[v] and lift(a,depth[a]-depth[v])==v)worksa=true;
bool worksb = false;
if(depth[b]>=depth[v] and lift(b,depth[b]-depth[v])==v)worksb=true;
if(worksa and worksb){
return lift(a,depth[a]-depth[v]-1)!=lift(b,depth[b]-depth[v]-1);
} else return worksa or worksb;
};
for(int i=1;i<=m;i++)cin>>s[i]>>t[i];
if(m<=500){
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++){
if(i==j)continue;
if(collides(i,t[j]))secadj[j].emplace_back(i);
if(collides(i,s[j]))secadj[i].emplace_back(j);
}
}
vector<int> visited(m+1);
bool works = true;
function<void(int)> secdfs = [&](int x){
if(visited[x]==2)return;
if(visited[x]==1){
works = false;
return;
}
visited[x]=1;
for(int&i:secadj[x])secdfs(i);
visited[x]=2;
};
for(int i=1;i<=m;i++)secdfs(i);
if(works){
cout << "Yes\n";
} else cout << "No\n";
} else {
vector<pair<int,int>> ranges;
vector<int> prefl(n+2);
vector<int> prefr(n+2);
for(int i=1;i<=m;i++){
if(s[i]<t[i]){
prefr[s[i]]++;
prefr[t[i]+1]--;
} else {
prefl[t[i]]++;
prefl[s[i]+1]--;
}
}
for(int i=1;i<=n;i++)prefr[i]+=prefr[i-1];
for(int i=1;i<=n;i++)prefl[i]+=prefl[i-1];
for(int i=1;i<=m;i++)ranges.emplace_back(min(s[i],t[i]),max(s[i],t[i]));
sort(ranges.begin(),ranges.end());
int r = 0;
bool works = true;
for(auto[a,b]:ranges){
if(b<=r)works=false;
r = b;
}
for(int i=1;i<=m;i++){
if(s[i]<t[i]){
if(prefl[s[i]] or prefl[t[i]])works=false;
} else {
if(prefr[t[i]] or prefr[s[i]])works=false;
}
}
if(!works)cout<<"No\n";
else cout << "Yes\n";
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
488 KB |
Output is correct |
5 |
Correct |
23 ms |
488 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
26 ms |
624 KB |
Output is correct |
9 |
Correct |
1018 ms |
4876 KB |
Output is correct |
10 |
Correct |
85 ms |
41552 KB |
Output is correct |
11 |
Correct |
11 ms |
348 KB |
Output is correct |
12 |
Correct |
329 ms |
520 KB |
Output is correct |
13 |
Correct |
52 ms |
43732 KB |
Output is correct |
14 |
Correct |
52 ms |
43732 KB |
Output is correct |
15 |
Correct |
55 ms |
43712 KB |
Output is correct |
16 |
Correct |
68 ms |
47576 KB |
Output is correct |
17 |
Correct |
56 ms |
44244 KB |
Output is correct |
18 |
Correct |
61 ms |
47564 KB |
Output is correct |
19 |
Correct |
52 ms |
44244 KB |
Output is correct |
20 |
Correct |
53 ms |
44240 KB |
Output is correct |
21 |
Correct |
53 ms |
44220 KB |
Output is correct |
22 |
Correct |
53 ms |
44244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
1 ms |
552 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
1 ms |
552 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
1 ms |
552 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
27 ms |
592 KB |
Output is correct |
30 |
Correct |
7 ms |
348 KB |
Output is correct |
31 |
Correct |
10 ms |
604 KB |
Output is correct |
32 |
Correct |
5 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
11 ms |
344 KB |
Output is correct |
35 |
Correct |
15 ms |
600 KB |
Output is correct |
36 |
Correct |
10 ms |
508 KB |
Output is correct |
37 |
Correct |
12 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
1 ms |
552 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
27 ms |
592 KB |
Output is correct |
30 |
Correct |
7 ms |
348 KB |
Output is correct |
31 |
Correct |
10 ms |
604 KB |
Output is correct |
32 |
Correct |
5 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
348 KB |
Output is correct |
34 |
Correct |
11 ms |
344 KB |
Output is correct |
35 |
Correct |
15 ms |
600 KB |
Output is correct |
36 |
Correct |
10 ms |
508 KB |
Output is correct |
37 |
Correct |
12 ms |
348 KB |
Output is correct |
38 |
Correct |
1020 ms |
6108 KB |
Output is correct |
39 |
Correct |
78 ms |
42944 KB |
Output is correct |
40 |
Correct |
608 ms |
4160 KB |
Output is correct |
41 |
Correct |
358 ms |
2864 KB |
Output is correct |
42 |
Correct |
379 ms |
3668 KB |
Output is correct |
43 |
Correct |
42 ms |
3328 KB |
Output is correct |
44 |
Correct |
229 ms |
884 KB |
Output is correct |
45 |
Correct |
72 ms |
29552 KB |
Output is correct |
46 |
Correct |
91 ms |
29708 KB |
Output is correct |
47 |
Correct |
96 ms |
35256 KB |
Output is correct |
48 |
Correct |
95 ms |
35160 KB |
Output is correct |
49 |
Correct |
82 ms |
29780 KB |
Output is correct |
50 |
Correct |
80 ms |
29780 KB |
Output is correct |
51 |
Correct |
49 ms |
31312 KB |
Output is correct |
52 |
Correct |
52 ms |
31312 KB |
Output is correct |
53 |
Correct |
247 ms |
2644 KB |
Output is correct |
54 |
Correct |
75 ms |
30288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
11 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
22 ms |
348 KB |
Output is correct |
13 |
Correct |
142 ms |
856 KB |
Output is correct |
14 |
Correct |
104 ms |
1108 KB |
Output is correct |
15 |
Correct |
119 ms |
1108 KB |
Output is correct |
16 |
Correct |
60 ms |
32596 KB |
Output is correct |
17 |
Correct |
67 ms |
35884 KB |
Output is correct |
18 |
Correct |
85 ms |
39880 KB |
Output is correct |
19 |
Incorrect |
60 ms |
33448 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
488 KB |
Output is correct |
5 |
Correct |
23 ms |
488 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
26 ms |
624 KB |
Output is correct |
9 |
Correct |
1018 ms |
4876 KB |
Output is correct |
10 |
Correct |
85 ms |
41552 KB |
Output is correct |
11 |
Correct |
11 ms |
348 KB |
Output is correct |
12 |
Correct |
329 ms |
520 KB |
Output is correct |
13 |
Correct |
52 ms |
43732 KB |
Output is correct |
14 |
Correct |
52 ms |
43732 KB |
Output is correct |
15 |
Correct |
55 ms |
43712 KB |
Output is correct |
16 |
Correct |
68 ms |
47576 KB |
Output is correct |
17 |
Correct |
56 ms |
44244 KB |
Output is correct |
18 |
Correct |
61 ms |
47564 KB |
Output is correct |
19 |
Correct |
52 ms |
44244 KB |
Output is correct |
20 |
Correct |
53 ms |
44240 KB |
Output is correct |
21 |
Correct |
53 ms |
44220 KB |
Output is correct |
22 |
Correct |
53 ms |
44244 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
688 KB |
Output is correct |
32 |
Correct |
1 ms |
552 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
2 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
2 ms |
556 KB |
Output is correct |
43 |
Correct |
1 ms |
464 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
51 |
Correct |
27 ms |
592 KB |
Output is correct |
52 |
Correct |
7 ms |
348 KB |
Output is correct |
53 |
Correct |
10 ms |
604 KB |
Output is correct |
54 |
Correct |
5 ms |
348 KB |
Output is correct |
55 |
Correct |
2 ms |
348 KB |
Output is correct |
56 |
Correct |
11 ms |
344 KB |
Output is correct |
57 |
Correct |
15 ms |
600 KB |
Output is correct |
58 |
Correct |
10 ms |
508 KB |
Output is correct |
59 |
Correct |
12 ms |
348 KB |
Output is correct |
60 |
Correct |
1020 ms |
6108 KB |
Output is correct |
61 |
Correct |
78 ms |
42944 KB |
Output is correct |
62 |
Correct |
608 ms |
4160 KB |
Output is correct |
63 |
Correct |
358 ms |
2864 KB |
Output is correct |
64 |
Correct |
379 ms |
3668 KB |
Output is correct |
65 |
Correct |
42 ms |
3328 KB |
Output is correct |
66 |
Correct |
229 ms |
884 KB |
Output is correct |
67 |
Correct |
72 ms |
29552 KB |
Output is correct |
68 |
Correct |
91 ms |
29708 KB |
Output is correct |
69 |
Correct |
96 ms |
35256 KB |
Output is correct |
70 |
Correct |
95 ms |
35160 KB |
Output is correct |
71 |
Correct |
82 ms |
29780 KB |
Output is correct |
72 |
Correct |
80 ms |
29780 KB |
Output is correct |
73 |
Correct |
49 ms |
31312 KB |
Output is correct |
74 |
Correct |
52 ms |
31312 KB |
Output is correct |
75 |
Correct |
247 ms |
2644 KB |
Output is correct |
76 |
Correct |
75 ms |
30288 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
344 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
11 ms |
348 KB |
Output is correct |
82 |
Correct |
2 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
1 ms |
344 KB |
Output is correct |
86 |
Correct |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
22 ms |
348 KB |
Output is correct |
89 |
Correct |
142 ms |
856 KB |
Output is correct |
90 |
Correct |
104 ms |
1108 KB |
Output is correct |
91 |
Correct |
119 ms |
1108 KB |
Output is correct |
92 |
Correct |
60 ms |
32596 KB |
Output is correct |
93 |
Correct |
67 ms |
35884 KB |
Output is correct |
94 |
Correct |
85 ms |
39880 KB |
Output is correct |
95 |
Incorrect |
60 ms |
33448 KB |
Output isn't correct |
96 |
Halted |
0 ms |
0 KB |
- |