#include <bits/stdc++.h>
using namespace std;/*
<<<<It's never too late for a new beginning in your life>>>>
Today is hard
tomorrow will worse
but the day after tomorrow will be the sunshine..
HARD WORK BEATS TALENT WHEN TALENT DOESN'T WORK HARD............
Never give up */
//The most CHALISHKANCHIK
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("Ofast,unroll-loops")
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
//~ #define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vii;
const long long N = 2e5+50, inf = 1e18, mod = 1e9+7;
vector<int>v[N],pred(N);
vector<int>col(N),vis(N);
void dfs(int x,int pr){
vis[x] = 1;
for(auto to:v[x]){
if(to==pr||col[to])continue;
dfs(to,x);
}
}
void solve(){
int n,m;
cin>>n;
for(int i = 1;i<=n;i++){
v[i].clear();
vis[i] = 0;
col[i] = 0;
}
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
cin>>m;
vector<pii>g(m);
for(int i = 0;i<m;i++){
int a,b;
cin>>a>>b;
g[i] = {a, b};
}
if(m<=6){
vector<int>ind;
for(int i = 0;i<m;i++)ind.pb(i);
do
{
for(int i = 1;i<=n;i++)col[i] = 0;
for(auto to:g){
col[to.ff] += 1;
}
bool flag = true;
for(auto to:ind){
int a = g[to].ff;
int b = g[to].ss;
col[a]--;
for(int i = 1;i<=n;i++)vis[i] = 0;
if(col[a]>=1){
flag = false;
break;
}
dfs(a,-1);
col[b] ++;
if(vis[b]==0){
flag = false;
break;
}
}
if(flag){
cout<<"Yes\n";
return;
}
}while (next_permutation(all(ind)));
cout<<"No\n";
}
else{
sort(all(g));
int mn = 1e9;
for(int i = m-1;i>=0;i--){
if(mn < g[i].ss){
cout<<"No\n";
return;
}
mn = min(mn, g[i].ss);
}
cout << "Yes\n";
}
}
main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
cin >> t;
while(t--){
solve();
}
}
Compilation message
jail.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
101 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
9 ms |
7516 KB |
Output is correct |
5 |
Correct |
16 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
14 ms |
7532 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
16 ms |
7768 KB |
Output is correct |
10 |
Correct |
23 ms |
11092 KB |
Output is correct |
11 |
Correct |
9 ms |
7512 KB |
Output is correct |
12 |
Correct |
23 ms |
7528 KB |
Output is correct |
13 |
Correct |
30 ms |
11608 KB |
Output is correct |
14 |
Correct |
29 ms |
11612 KB |
Output is correct |
15 |
Correct |
32 ms |
11612 KB |
Output is correct |
16 |
Correct |
45 ms |
12204 KB |
Output is correct |
17 |
Correct |
31 ms |
11600 KB |
Output is correct |
18 |
Correct |
38 ms |
12204 KB |
Output is correct |
19 |
Correct |
34 ms |
11600 KB |
Output is correct |
20 |
Correct |
30 ms |
11600 KB |
Output is correct |
21 |
Correct |
34 ms |
11612 KB |
Output is correct |
22 |
Correct |
31 ms |
11756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7512 KB |
Output is correct |
13 |
Correct |
2 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7512 KB |
Output is correct |
13 |
Correct |
2 ms |
7512 KB |
Output is correct |
14 |
Correct |
2 ms |
7512 KB |
Output is correct |
15 |
Correct |
2 ms |
7260 KB |
Output is correct |
16 |
Correct |
14 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7512 KB |
Output is correct |
18 |
Correct |
18 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
12 ms |
7516 KB |
Output is correct |
21 |
Correct |
19 ms |
7512 KB |
Output is correct |
22 |
Correct |
12 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
3 ms |
7516 KB |
Output is correct |
25 |
Correct |
52 ms |
7516 KB |
Output is correct |
26 |
Correct |
9 ms |
7516 KB |
Output is correct |
27 |
Correct |
9 ms |
7516 KB |
Output is correct |
28 |
Correct |
7 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7512 KB |
Output is correct |
13 |
Correct |
2 ms |
7512 KB |
Output is correct |
14 |
Correct |
2 ms |
7512 KB |
Output is correct |
15 |
Correct |
2 ms |
7260 KB |
Output is correct |
16 |
Correct |
14 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7512 KB |
Output is correct |
18 |
Correct |
18 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
12 ms |
7516 KB |
Output is correct |
21 |
Correct |
19 ms |
7512 KB |
Output is correct |
22 |
Correct |
12 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
3 ms |
7516 KB |
Output is correct |
25 |
Correct |
52 ms |
7516 KB |
Output is correct |
26 |
Correct |
9 ms |
7516 KB |
Output is correct |
27 |
Correct |
9 ms |
7516 KB |
Output is correct |
28 |
Correct |
7 ms |
7516 KB |
Output is correct |
29 |
Correct |
3 ms |
7768 KB |
Output is correct |
30 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7512 KB |
Output is correct |
13 |
Correct |
2 ms |
7512 KB |
Output is correct |
14 |
Correct |
2 ms |
7512 KB |
Output is correct |
15 |
Correct |
2 ms |
7260 KB |
Output is correct |
16 |
Correct |
14 ms |
7516 KB |
Output is correct |
17 |
Correct |
3 ms |
7512 KB |
Output is correct |
18 |
Correct |
18 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
12 ms |
7516 KB |
Output is correct |
21 |
Correct |
19 ms |
7512 KB |
Output is correct |
22 |
Correct |
12 ms |
7516 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
3 ms |
7516 KB |
Output is correct |
25 |
Correct |
52 ms |
7516 KB |
Output is correct |
26 |
Correct |
9 ms |
7516 KB |
Output is correct |
27 |
Correct |
9 ms |
7516 KB |
Output is correct |
28 |
Correct |
7 ms |
7516 KB |
Output is correct |
29 |
Correct |
3 ms |
7768 KB |
Output is correct |
30 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
9 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
3 ms |
7516 KB |
Output is correct |
11 |
Correct |
7 ms |
7520 KB |
Output is correct |
12 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
9 ms |
7516 KB |
Output is correct |
5 |
Correct |
16 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
14 ms |
7532 KB |
Output is correct |
8 |
Correct |
3 ms |
7516 KB |
Output is correct |
9 |
Correct |
16 ms |
7768 KB |
Output is correct |
10 |
Correct |
23 ms |
11092 KB |
Output is correct |
11 |
Correct |
9 ms |
7512 KB |
Output is correct |
12 |
Correct |
23 ms |
7528 KB |
Output is correct |
13 |
Correct |
30 ms |
11608 KB |
Output is correct |
14 |
Correct |
29 ms |
11612 KB |
Output is correct |
15 |
Correct |
32 ms |
11612 KB |
Output is correct |
16 |
Correct |
45 ms |
12204 KB |
Output is correct |
17 |
Correct |
31 ms |
11600 KB |
Output is correct |
18 |
Correct |
38 ms |
12204 KB |
Output is correct |
19 |
Correct |
34 ms |
11600 KB |
Output is correct |
20 |
Correct |
30 ms |
11600 KB |
Output is correct |
21 |
Correct |
34 ms |
11612 KB |
Output is correct |
22 |
Correct |
31 ms |
11756 KB |
Output is correct |
23 |
Correct |
2 ms |
7512 KB |
Output is correct |
24 |
Correct |
2 ms |
7516 KB |
Output is correct |
25 |
Correct |
4 ms |
7516 KB |
Output is correct |
26 |
Correct |
2 ms |
7516 KB |
Output is correct |
27 |
Correct |
3 ms |
7516 KB |
Output is correct |
28 |
Correct |
3 ms |
7516 KB |
Output is correct |
29 |
Correct |
3 ms |
7516 KB |
Output is correct |
30 |
Correct |
3 ms |
7516 KB |
Output is correct |
31 |
Correct |
3 ms |
7516 KB |
Output is correct |
32 |
Correct |
3 ms |
7516 KB |
Output is correct |
33 |
Correct |
2 ms |
7516 KB |
Output is correct |
34 |
Correct |
3 ms |
7512 KB |
Output is correct |
35 |
Correct |
2 ms |
7512 KB |
Output is correct |
36 |
Correct |
2 ms |
7512 KB |
Output is correct |
37 |
Correct |
2 ms |
7260 KB |
Output is correct |
38 |
Correct |
14 ms |
7516 KB |
Output is correct |
39 |
Correct |
3 ms |
7512 KB |
Output is correct |
40 |
Correct |
18 ms |
7516 KB |
Output is correct |
41 |
Correct |
2 ms |
7260 KB |
Output is correct |
42 |
Correct |
12 ms |
7516 KB |
Output is correct |
43 |
Correct |
19 ms |
7512 KB |
Output is correct |
44 |
Correct |
12 ms |
7516 KB |
Output is correct |
45 |
Correct |
2 ms |
7260 KB |
Output is correct |
46 |
Correct |
3 ms |
7516 KB |
Output is correct |
47 |
Correct |
52 ms |
7516 KB |
Output is correct |
48 |
Correct |
9 ms |
7516 KB |
Output is correct |
49 |
Correct |
9 ms |
7516 KB |
Output is correct |
50 |
Correct |
7 ms |
7516 KB |
Output is correct |
51 |
Correct |
3 ms |
7768 KB |
Output is correct |
52 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |