#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 10, MOD = 998244353;
vector<int> g[N],x[N];
int timer = 0,tin[N],tout[N],n,m,s[N],t[N],blocked[N],c[N],pp[N];
bool used[N];
void dfs(int v,int pr = 1){
tin[v] = ++timer;
pp[v] = pr;
for(int to:g[v]){
if(to == pr) continue;
dfs(to,v);
}
tout[v] = ++timer;
}
bool is(int v,int u){
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
vector<int> path(int v,int u){
vector<int> p(1,v);
while(!is(v,u)){
v = pp[v];
p.push_back(v);
}
while(!is(u,v)){
p.push_back(u);
u = pp[u];
}
return p;
}
bool can(int i){
for(int j:x[i]){
if(j != s[i] && used[j]) return false;
}
if(c[t[i]] > 1) return false;
return true;
}
void test(){
timer =0 ;
cin >> n;
for(int i = 1;i <= n;i++)
g[i].clear();
for(int i = 1;i <= n - 1;i++){
int x,y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
used[x] = used[y] = c[x] = c[y] = 0;
}
dfs(1);
cin >> m;
for(int i = 1;i <= m;i++){
blocked[i] = 0;
}
for(int i = 1;i <= m;++i){
cin >> s[i] >> t[i];
used[s[i]] = 1;
x[i] = path(s[i],t[i]);
for(auto j:x[i]){
c[j]++;
}
}
for(int i = 1;i <= m;i++){
bool ok = false;
for(int j = 1;j <= m;j++){
if(blocked[j] || !can(j)) continue;
blocked[j] = 1;
for(auto k:x[j]){
c[k]--;
}
ok = 1;
used[s[j]] = 0;
used[t[j]] = 1;
break;
}
if(!ok){
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
for (int i = 1; i <= T; i++)
{
test();
}
}
Compilation message
jail.cpp: In function 'void test()':
jail.cpp:50:34: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
50 | used[x] = used[y] = c[x] = c[y] = 0;
| ~~~~~^~~~~~~~~~
jail.cpp: At global scope:
jail.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
86 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
37720 KB |
Output is correct |
2 |
Correct |
7 ms |
37724 KB |
Output is correct |
3 |
Correct |
7 ms |
37980 KB |
Output is correct |
4 |
Correct |
13 ms |
38076 KB |
Output is correct |
5 |
Correct |
19 ms |
38236 KB |
Output is correct |
6 |
Correct |
8 ms |
37976 KB |
Output is correct |
7 |
Correct |
8 ms |
38012 KB |
Output is correct |
8 |
Correct |
9 ms |
37980 KB |
Output is correct |
9 |
Correct |
568 ms |
55648 KB |
Output is correct |
10 |
Execution timed out |
5096 ms |
283080 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37724 KB |
Output is correct |
2 |
Correct |
7 ms |
37820 KB |
Output is correct |
3 |
Correct |
8 ms |
37980 KB |
Output is correct |
4 |
Correct |
8 ms |
37976 KB |
Output is correct |
5 |
Correct |
7 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
38000 KB |
Output is correct |
7 |
Correct |
8 ms |
37980 KB |
Output is correct |
8 |
Correct |
8 ms |
37940 KB |
Output is correct |
9 |
Correct |
8 ms |
37940 KB |
Output is correct |
10 |
Correct |
8 ms |
37976 KB |
Output is correct |
11 |
Correct |
8 ms |
37972 KB |
Output is correct |
12 |
Correct |
8 ms |
37980 KB |
Output is correct |
13 |
Correct |
7 ms |
37944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37724 KB |
Output is correct |
2 |
Correct |
7 ms |
37820 KB |
Output is correct |
3 |
Correct |
8 ms |
37980 KB |
Output is correct |
4 |
Correct |
8 ms |
37976 KB |
Output is correct |
5 |
Correct |
7 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
38000 KB |
Output is correct |
7 |
Correct |
8 ms |
37980 KB |
Output is correct |
8 |
Correct |
8 ms |
37940 KB |
Output is correct |
9 |
Correct |
8 ms |
37940 KB |
Output is correct |
10 |
Correct |
8 ms |
37976 KB |
Output is correct |
11 |
Correct |
8 ms |
37972 KB |
Output is correct |
12 |
Correct |
8 ms |
37980 KB |
Output is correct |
13 |
Correct |
7 ms |
37944 KB |
Output is correct |
14 |
Correct |
7 ms |
37880 KB |
Output is correct |
15 |
Correct |
7 ms |
37724 KB |
Output is correct |
16 |
Correct |
8 ms |
37976 KB |
Output is correct |
17 |
Correct |
8 ms |
37976 KB |
Output is correct |
18 |
Correct |
9 ms |
37976 KB |
Output is correct |
19 |
Correct |
8 ms |
37724 KB |
Output is correct |
20 |
Correct |
8 ms |
37924 KB |
Output is correct |
21 |
Correct |
7 ms |
37976 KB |
Output is correct |
22 |
Correct |
8 ms |
37980 KB |
Output is correct |
23 |
Correct |
7 ms |
37724 KB |
Output is correct |
24 |
Correct |
7 ms |
37724 KB |
Output is correct |
25 |
Correct |
8 ms |
37980 KB |
Output is correct |
26 |
Correct |
8 ms |
37724 KB |
Output is correct |
27 |
Correct |
8 ms |
37980 KB |
Output is correct |
28 |
Correct |
7 ms |
37724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37724 KB |
Output is correct |
2 |
Correct |
7 ms |
37820 KB |
Output is correct |
3 |
Correct |
8 ms |
37980 KB |
Output is correct |
4 |
Correct |
8 ms |
37976 KB |
Output is correct |
5 |
Correct |
7 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
38000 KB |
Output is correct |
7 |
Correct |
8 ms |
37980 KB |
Output is correct |
8 |
Correct |
8 ms |
37940 KB |
Output is correct |
9 |
Correct |
8 ms |
37940 KB |
Output is correct |
10 |
Correct |
8 ms |
37976 KB |
Output is correct |
11 |
Correct |
8 ms |
37972 KB |
Output is correct |
12 |
Correct |
8 ms |
37980 KB |
Output is correct |
13 |
Correct |
7 ms |
37944 KB |
Output is correct |
14 |
Correct |
7 ms |
37880 KB |
Output is correct |
15 |
Correct |
7 ms |
37724 KB |
Output is correct |
16 |
Correct |
8 ms |
37976 KB |
Output is correct |
17 |
Correct |
8 ms |
37976 KB |
Output is correct |
18 |
Correct |
9 ms |
37976 KB |
Output is correct |
19 |
Correct |
8 ms |
37724 KB |
Output is correct |
20 |
Correct |
8 ms |
37924 KB |
Output is correct |
21 |
Correct |
7 ms |
37976 KB |
Output is correct |
22 |
Correct |
8 ms |
37980 KB |
Output is correct |
23 |
Correct |
7 ms |
37724 KB |
Output is correct |
24 |
Correct |
7 ms |
37724 KB |
Output is correct |
25 |
Correct |
8 ms |
37980 KB |
Output is correct |
26 |
Correct |
8 ms |
37724 KB |
Output is correct |
27 |
Correct |
8 ms |
37980 KB |
Output is correct |
28 |
Correct |
7 ms |
37724 KB |
Output is correct |
29 |
Correct |
9 ms |
37976 KB |
Output is correct |
30 |
Correct |
10 ms |
37980 KB |
Output is correct |
31 |
Correct |
9 ms |
37976 KB |
Output is correct |
32 |
Correct |
8 ms |
37980 KB |
Output is correct |
33 |
Correct |
8 ms |
37980 KB |
Output is correct |
34 |
Correct |
8 ms |
37980 KB |
Output is correct |
35 |
Correct |
9 ms |
37980 KB |
Output is correct |
36 |
Correct |
8 ms |
38024 KB |
Output is correct |
37 |
Correct |
8 ms |
37980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37724 KB |
Output is correct |
2 |
Correct |
7 ms |
37820 KB |
Output is correct |
3 |
Correct |
8 ms |
37980 KB |
Output is correct |
4 |
Correct |
8 ms |
37976 KB |
Output is correct |
5 |
Correct |
7 ms |
37980 KB |
Output is correct |
6 |
Correct |
8 ms |
38000 KB |
Output is correct |
7 |
Correct |
8 ms |
37980 KB |
Output is correct |
8 |
Correct |
8 ms |
37940 KB |
Output is correct |
9 |
Correct |
8 ms |
37940 KB |
Output is correct |
10 |
Correct |
8 ms |
37976 KB |
Output is correct |
11 |
Correct |
8 ms |
37972 KB |
Output is correct |
12 |
Correct |
8 ms |
37980 KB |
Output is correct |
13 |
Correct |
7 ms |
37944 KB |
Output is correct |
14 |
Correct |
7 ms |
37880 KB |
Output is correct |
15 |
Correct |
7 ms |
37724 KB |
Output is correct |
16 |
Correct |
8 ms |
37976 KB |
Output is correct |
17 |
Correct |
8 ms |
37976 KB |
Output is correct |
18 |
Correct |
9 ms |
37976 KB |
Output is correct |
19 |
Correct |
8 ms |
37724 KB |
Output is correct |
20 |
Correct |
8 ms |
37924 KB |
Output is correct |
21 |
Correct |
7 ms |
37976 KB |
Output is correct |
22 |
Correct |
8 ms |
37980 KB |
Output is correct |
23 |
Correct |
7 ms |
37724 KB |
Output is correct |
24 |
Correct |
7 ms |
37724 KB |
Output is correct |
25 |
Correct |
8 ms |
37980 KB |
Output is correct |
26 |
Correct |
8 ms |
37724 KB |
Output is correct |
27 |
Correct |
8 ms |
37980 KB |
Output is correct |
28 |
Correct |
7 ms |
37724 KB |
Output is correct |
29 |
Correct |
9 ms |
37976 KB |
Output is correct |
30 |
Correct |
10 ms |
37980 KB |
Output is correct |
31 |
Correct |
9 ms |
37976 KB |
Output is correct |
32 |
Correct |
8 ms |
37980 KB |
Output is correct |
33 |
Correct |
8 ms |
37980 KB |
Output is correct |
34 |
Correct |
8 ms |
37980 KB |
Output is correct |
35 |
Correct |
9 ms |
37980 KB |
Output is correct |
36 |
Correct |
8 ms |
38024 KB |
Output is correct |
37 |
Correct |
8 ms |
37980 KB |
Output is correct |
38 |
Correct |
534 ms |
55848 KB |
Output is correct |
39 |
Execution timed out |
5034 ms |
283616 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
37720 KB |
Output is correct |
2 |
Correct |
8 ms |
37724 KB |
Output is correct |
3 |
Correct |
8 ms |
37824 KB |
Output is correct |
4 |
Correct |
7 ms |
37924 KB |
Output is correct |
5 |
Correct |
11 ms |
37924 KB |
Output is correct |
6 |
Correct |
7 ms |
37724 KB |
Output is correct |
7 |
Correct |
7 ms |
37980 KB |
Output is correct |
8 |
Correct |
7 ms |
37720 KB |
Output is correct |
9 |
Correct |
7 ms |
37724 KB |
Output is correct |
10 |
Correct |
7 ms |
37724 KB |
Output is correct |
11 |
Correct |
7 ms |
37724 KB |
Output is correct |
12 |
Correct |
9 ms |
37972 KB |
Output is correct |
13 |
Correct |
20 ms |
38488 KB |
Output is correct |
14 |
Correct |
29 ms |
38844 KB |
Output is correct |
15 |
Correct |
24 ms |
38460 KB |
Output is correct |
16 |
Correct |
402 ms |
44688 KB |
Output is correct |
17 |
Correct |
68 ms |
50512 KB |
Output is correct |
18 |
Correct |
90 ms |
58964 KB |
Output is correct |
19 |
Correct |
648 ms |
46076 KB |
Output is correct |
20 |
Correct |
1800 ms |
46128 KB |
Output is correct |
21 |
Correct |
1141 ms |
46160 KB |
Output is correct |
22 |
Correct |
417 ms |
51172 KB |
Output is correct |
23 |
Correct |
63 ms |
50916 KB |
Output is correct |
24 |
Correct |
1192 ms |
51968 KB |
Output is correct |
25 |
Correct |
2152 ms |
52204 KB |
Output is correct |
26 |
Correct |
2296 ms |
52120 KB |
Output is correct |
27 |
Execution timed out |
5050 ms |
48796 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
37720 KB |
Output is correct |
2 |
Correct |
7 ms |
37724 KB |
Output is correct |
3 |
Correct |
7 ms |
37980 KB |
Output is correct |
4 |
Correct |
13 ms |
38076 KB |
Output is correct |
5 |
Correct |
19 ms |
38236 KB |
Output is correct |
6 |
Correct |
8 ms |
37976 KB |
Output is correct |
7 |
Correct |
8 ms |
38012 KB |
Output is correct |
8 |
Correct |
9 ms |
37980 KB |
Output is correct |
9 |
Correct |
568 ms |
55648 KB |
Output is correct |
10 |
Execution timed out |
5096 ms |
283080 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |