#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define ll unsigned long long
using namespace __gnu_pbds;
#define modu 1000000007
#define sizeofA 130001
int sum(ll a,ll b){
return ((a % modu) + (b % modu))% modu;
}
typedef tree<long,null_type,less<long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
long max_ = 0;
ordered_set higher;
long N,M;
vector<long> adj[sizeofA];
vector<long> hyper_adj[sizeofA];
bool the_path[sizeofA] = {};
bool filling[sizeofA] = {};
//hyper_adj[a] -> b then (a before b)
bool check_valid(long now){
if(the_path[now]){return 0;}
the_path[now] = 1;
filling[now] = 1;
for (auto godo: hyper_adj[now]){
//cout<<godo<<" "<<now<<endl;
if(!check_valid(godo)){
the_path[now] = 0;
return 0;
}
}
the_path[now] = 0;
return 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t;
cin>>t;
while(t--){
cin>>N;
//memset(the_path, 0, sizeof(the_path));
memset(filling, 0, sizeof(N+1));
for(long i = 1;i<=N+1;i++){
adj[i].clear();
hyper_adj[i].clear();
}
long start[N+1]={};
long work[N+1] ={};
for(long i = 1;i<=N-1;i++){
long temp1, temp2;
cin>>temp1>>temp2;
adj[temp1].push_back(temp2);
adj[temp2].push_back(temp1);
}
cin>>M;
for (long i = 1;i<=M;i++){
long temp1, temp2;
cin>>temp1>>temp2;
start[temp1]=i;
work[temp2]=i;
}
vector<long> children[sizeofA];
stack<long> tovisit;
stack<long> dfs;
bool visited[sizeofA] = {};
dfs.push(1);
while(!dfs.empty()){
long current_node = dfs.top();
dfs.pop();
visited[current_node] = 1;
tovisit.push(current_node);
for (auto togo : adj[current_node]){
if (!visited[togo]){
children[current_node].push_back(togo);
dfs.push(togo);
}
}
}
set<long> beg[sizeofA];
set<long> en[sizeofA];
stack<long> to_remove;
while(!tovisit.empty()){
long current_node = tovisit.top();
for(auto child : children[current_node]){
if (beg[current_node].size() < beg[child].size()) {
swap(beg[current_node], beg[child]);
}
for (auto item : beg[child]) {
beg[current_node].insert(item);
}
if (en[current_node].size() < en[child].size()) {
swap(en[current_node], en[child]);
}
for (auto item : en[child]) {
en[current_node].insert(item);
}
}
if(start[current_node]!=0){
long begin_ = start[current_node];
for(auto item: beg[current_node]){
if (begin_ !=item) hyper_adj[begin_].push_back(item);
}
for(auto item: en[current_node]){
if (begin_ !=item) hyper_adj[begin_].push_back(item);
}
}
if(work[current_node]!=0){
long end_ = work[current_node];
for(auto item: beg[current_node]){
if(end_ != item) hyper_adj[item].push_back(end_);
}
for(auto item: en[current_node]){
if(end_ != item) hyper_adj[item].push_back(end_);
}
}
if((start[current_node]>0)&&(work[current_node]>0)){
hyper_adj[start[current_node]].push_back(work[current_node]);
}
if(start[current_node]!=0){
beg[current_node].insert(start[current_node]);
}
if(work[current_node]!=0){
en[current_node].insert(work[current_node]);
}
for(auto item:beg[current_node]){
long num = en[current_node].erase(item);
if(num>0){
to_remove.push(item);
}
}
while(!to_remove.empty()){
beg[current_node].erase(to_remove.top());
to_remove.pop();
}
tovisit.pop();
//cout<<current_node<<" "<<en[current_node].size()<<" "<<beg[current_node].size()<<endl;
//cout<<current_node<<" "<<start[current_node]<<" "<<work[current_node]<<endl;
}
bool checking = 1;
for (long i = 1;i<=N;i++){
if(!filling[i]){
checking = checking & check_valid(i);
if (checking == 0){
break;
}
}
}
if (checking){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21828 KB |
Output is correct |
2 |
Correct |
17 ms |
21824 KB |
Output is correct |
3 |
Correct |
14 ms |
21848 KB |
Output is correct |
4 |
Correct |
1105 ms |
22060 KB |
Output is correct |
5 |
Correct |
2292 ms |
22448 KB |
Output is correct |
6 |
Correct |
57 ms |
21844 KB |
Output is correct |
7 |
Correct |
60 ms |
21840 KB |
Output is correct |
8 |
Execution timed out |
5042 ms |
21824 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21828 KB |
Output is correct |
2 |
Correct |
14 ms |
21820 KB |
Output is correct |
3 |
Correct |
61 ms |
21832 KB |
Output is correct |
4 |
Correct |
58 ms |
21844 KB |
Output is correct |
5 |
Correct |
60 ms |
21876 KB |
Output is correct |
6 |
Correct |
58 ms |
21844 KB |
Output is correct |
7 |
Correct |
61 ms |
21984 KB |
Output is correct |
8 |
Correct |
59 ms |
21876 KB |
Output is correct |
9 |
Correct |
64 ms |
21896 KB |
Output is correct |
10 |
Correct |
58 ms |
21936 KB |
Output is correct |
11 |
Correct |
52 ms |
21844 KB |
Output is correct |
12 |
Correct |
58 ms |
21844 KB |
Output is correct |
13 |
Correct |
54 ms |
21844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21828 KB |
Output is correct |
2 |
Correct |
14 ms |
21820 KB |
Output is correct |
3 |
Correct |
61 ms |
21832 KB |
Output is correct |
4 |
Correct |
58 ms |
21844 KB |
Output is correct |
5 |
Correct |
60 ms |
21876 KB |
Output is correct |
6 |
Correct |
58 ms |
21844 KB |
Output is correct |
7 |
Correct |
61 ms |
21984 KB |
Output is correct |
8 |
Correct |
59 ms |
21876 KB |
Output is correct |
9 |
Correct |
64 ms |
21896 KB |
Output is correct |
10 |
Correct |
58 ms |
21936 KB |
Output is correct |
11 |
Correct |
52 ms |
21844 KB |
Output is correct |
12 |
Correct |
58 ms |
21844 KB |
Output is correct |
13 |
Correct |
54 ms |
21844 KB |
Output is correct |
14 |
Correct |
15 ms |
21716 KB |
Output is correct |
15 |
Correct |
17 ms |
21716 KB |
Output is correct |
16 |
Correct |
61 ms |
21832 KB |
Output is correct |
17 |
Correct |
61 ms |
21944 KB |
Output is correct |
18 |
Correct |
61 ms |
21836 KB |
Output is correct |
19 |
Correct |
60 ms |
21716 KB |
Output is correct |
20 |
Correct |
59 ms |
21836 KB |
Output is correct |
21 |
Correct |
67 ms |
21880 KB |
Output is correct |
22 |
Correct |
63 ms |
21844 KB |
Output is correct |
23 |
Correct |
57 ms |
21716 KB |
Output is correct |
24 |
Correct |
57 ms |
21716 KB |
Output is correct |
25 |
Correct |
58 ms |
21836 KB |
Output is correct |
26 |
Correct |
58 ms |
21836 KB |
Output is correct |
27 |
Correct |
57 ms |
21844 KB |
Output is correct |
28 |
Correct |
56 ms |
21716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21828 KB |
Output is correct |
2 |
Correct |
14 ms |
21820 KB |
Output is correct |
3 |
Correct |
61 ms |
21832 KB |
Output is correct |
4 |
Correct |
58 ms |
21844 KB |
Output is correct |
5 |
Correct |
60 ms |
21876 KB |
Output is correct |
6 |
Correct |
58 ms |
21844 KB |
Output is correct |
7 |
Correct |
61 ms |
21984 KB |
Output is correct |
8 |
Correct |
59 ms |
21876 KB |
Output is correct |
9 |
Correct |
64 ms |
21896 KB |
Output is correct |
10 |
Correct |
58 ms |
21936 KB |
Output is correct |
11 |
Correct |
52 ms |
21844 KB |
Output is correct |
12 |
Correct |
58 ms |
21844 KB |
Output is correct |
13 |
Correct |
54 ms |
21844 KB |
Output is correct |
14 |
Correct |
15 ms |
21716 KB |
Output is correct |
15 |
Correct |
17 ms |
21716 KB |
Output is correct |
16 |
Correct |
61 ms |
21832 KB |
Output is correct |
17 |
Correct |
61 ms |
21944 KB |
Output is correct |
18 |
Correct |
61 ms |
21836 KB |
Output is correct |
19 |
Correct |
60 ms |
21716 KB |
Output is correct |
20 |
Correct |
59 ms |
21836 KB |
Output is correct |
21 |
Correct |
67 ms |
21880 KB |
Output is correct |
22 |
Correct |
63 ms |
21844 KB |
Output is correct |
23 |
Correct |
57 ms |
21716 KB |
Output is correct |
24 |
Correct |
57 ms |
21716 KB |
Output is correct |
25 |
Correct |
58 ms |
21836 KB |
Output is correct |
26 |
Correct |
58 ms |
21836 KB |
Output is correct |
27 |
Correct |
57 ms |
21844 KB |
Output is correct |
28 |
Correct |
56 ms |
21716 KB |
Output is correct |
29 |
Execution timed out |
5047 ms |
21836 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21828 KB |
Output is correct |
2 |
Correct |
14 ms |
21820 KB |
Output is correct |
3 |
Correct |
61 ms |
21832 KB |
Output is correct |
4 |
Correct |
58 ms |
21844 KB |
Output is correct |
5 |
Correct |
60 ms |
21876 KB |
Output is correct |
6 |
Correct |
58 ms |
21844 KB |
Output is correct |
7 |
Correct |
61 ms |
21984 KB |
Output is correct |
8 |
Correct |
59 ms |
21876 KB |
Output is correct |
9 |
Correct |
64 ms |
21896 KB |
Output is correct |
10 |
Correct |
58 ms |
21936 KB |
Output is correct |
11 |
Correct |
52 ms |
21844 KB |
Output is correct |
12 |
Correct |
58 ms |
21844 KB |
Output is correct |
13 |
Correct |
54 ms |
21844 KB |
Output is correct |
14 |
Correct |
15 ms |
21716 KB |
Output is correct |
15 |
Correct |
17 ms |
21716 KB |
Output is correct |
16 |
Correct |
61 ms |
21832 KB |
Output is correct |
17 |
Correct |
61 ms |
21944 KB |
Output is correct |
18 |
Correct |
61 ms |
21836 KB |
Output is correct |
19 |
Correct |
60 ms |
21716 KB |
Output is correct |
20 |
Correct |
59 ms |
21836 KB |
Output is correct |
21 |
Correct |
67 ms |
21880 KB |
Output is correct |
22 |
Correct |
63 ms |
21844 KB |
Output is correct |
23 |
Correct |
57 ms |
21716 KB |
Output is correct |
24 |
Correct |
57 ms |
21716 KB |
Output is correct |
25 |
Correct |
58 ms |
21836 KB |
Output is correct |
26 |
Correct |
58 ms |
21836 KB |
Output is correct |
27 |
Correct |
57 ms |
21844 KB |
Output is correct |
28 |
Correct |
56 ms |
21716 KB |
Output is correct |
29 |
Execution timed out |
5047 ms |
21836 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21716 KB |
Output is correct |
2 |
Correct |
17 ms |
21716 KB |
Output is correct |
3 |
Correct |
17 ms |
21716 KB |
Output is correct |
4 |
Correct |
13 ms |
21724 KB |
Output is correct |
5 |
Correct |
2262 ms |
22120 KB |
Output is correct |
6 |
Correct |
58 ms |
21816 KB |
Output is correct |
7 |
Correct |
58 ms |
21844 KB |
Output is correct |
8 |
Correct |
57 ms |
21828 KB |
Output is correct |
9 |
Correct |
56 ms |
21780 KB |
Output is correct |
10 |
Correct |
57 ms |
21816 KB |
Output is correct |
11 |
Correct |
60 ms |
21716 KB |
Output is correct |
12 |
Correct |
65 ms |
22032 KB |
Output is correct |
13 |
Incorrect |
2446 ms |
22732 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21828 KB |
Output is correct |
2 |
Correct |
17 ms |
21824 KB |
Output is correct |
3 |
Correct |
14 ms |
21848 KB |
Output is correct |
4 |
Correct |
1105 ms |
22060 KB |
Output is correct |
5 |
Correct |
2292 ms |
22448 KB |
Output is correct |
6 |
Correct |
57 ms |
21844 KB |
Output is correct |
7 |
Correct |
60 ms |
21840 KB |
Output is correct |
8 |
Execution timed out |
5042 ms |
21824 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |