#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int maxn = 120010;
int n, m, s[maxn], f[maxn], used[maxn], bef[maxn];
vector<int> v[maxn], g[maxn], paths[510];
int usedp[maxn];
void find_path(int ver){
used[ver] = 1;
for(int nb : v[ver]){
if(used[nb])
continue;
bef[nb] = ver;
find_path(nb);
}
}
void read(){
cin >> n;
for(int i = 1;i <= n;i++){
v[i].clear();
g[i].clear();
}
for(int i = 1;i <= m;i++)
paths[i].clear();
for(int i = 1;i <= n - 1;i++){
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cin >> m;
for(int i = 1;i <= m;i++){
cin >> s[i] >> f[i];
for(int k = 1;k <= n;k++)
used[k] = 0;
find_path(s[i]);
paths[i].push_back(f[i]);
int x = f[i];
while(x != s[i]){
x = bef[x];
paths[i].push_back(x);
}
sort(paths[i].begin(), paths[i].end());
}
}
bool cycle = 0;
void DFS(int ver){
used[ver] = 1;
for(int nb : g[ver]){
if(!used[nb])
DFS(nb);
else if(used[nb] == 1)
cycle = 1;
}
used[ver] = 2;
}
void dfs(int ver){
used[ver] = 1;
for(int nb : v[ver]){
if(used[nb])
continue;
dfs(nb);
}
}
void solve(){
read();
for(int i = 1;i <= m;i++){
for(int nb : paths[i])
usedp[nb] = 1;
for(int j = 1;j <= m;j++){
if(i == j)
continue;
if(usedp[s[j]]){
g[j].push_back(i);
continue;
}
int ans = -1, l = 0, r = paths[j].size() - 1;
while(l <= r){
int mid = (l + r) / 2;
if(paths[j][mid] <= f[i])
l = mid + 1, ans = mid;
else
r = mid - 1;
}
if(ans != -1 && paths[j][ans] == f[i]){
g[j].push_back(i);
}
}
for(int nb : paths[i])
usedp[nb] = 0;
}
cycle = 0;
memset(used, 0, sizeof(used));
for(int i = 1;i <= n;i++){
if(!used[i]){
DFS(i);
}
}
if(cycle)
cout << "No\n";
else
cout << "Yes\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
11 ms |
7108 KB |
Output is correct |
5 |
Correct |
23 ms |
7112 KB |
Output is correct |
6 |
Correct |
3 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
11 ms |
7260 KB |
Output is correct |
9 |
Correct |
641 ms |
24400 KB |
Output is correct |
10 |
Correct |
1426 ms |
250180 KB |
Output is correct |
11 |
Correct |
13 ms |
7004 KB |
Output is correct |
12 |
Correct |
97 ms |
7772 KB |
Output is correct |
13 |
Execution timed out |
5014 ms |
17408 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
2 ms |
7008 KB |
Output is correct |
6 |
Correct |
3 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7096 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7116 KB |
Output is correct |
12 |
Correct |
2 ms |
7108 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
2 ms |
7008 KB |
Output is correct |
6 |
Correct |
3 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7096 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7116 KB |
Output is correct |
12 |
Correct |
2 ms |
7108 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
14 |
Correct |
2 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
7004 KB |
Output is correct |
16 |
Correct |
3 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
2 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7000 KB |
Output is correct |
20 |
Correct |
3 ms |
7004 KB |
Output is correct |
21 |
Correct |
3 ms |
7004 KB |
Output is correct |
22 |
Correct |
2 ms |
7004 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
3 ms |
7100 KB |
Output is correct |
26 |
Correct |
2 ms |
7004 KB |
Output is correct |
27 |
Correct |
3 ms |
6884 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
2 ms |
7008 KB |
Output is correct |
6 |
Correct |
3 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7096 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7116 KB |
Output is correct |
12 |
Correct |
2 ms |
7108 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
14 |
Correct |
2 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
7004 KB |
Output is correct |
16 |
Correct |
3 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
2 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7000 KB |
Output is correct |
20 |
Correct |
3 ms |
7004 KB |
Output is correct |
21 |
Correct |
3 ms |
7004 KB |
Output is correct |
22 |
Correct |
2 ms |
7004 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
3 ms |
7100 KB |
Output is correct |
26 |
Correct |
2 ms |
7004 KB |
Output is correct |
27 |
Correct |
3 ms |
6884 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
29 |
Correct |
8 ms |
7260 KB |
Output is correct |
30 |
Correct |
6 ms |
7080 KB |
Output is correct |
31 |
Correct |
9 ms |
7004 KB |
Output is correct |
32 |
Correct |
6 ms |
7004 KB |
Output is correct |
33 |
Correct |
3 ms |
7108 KB |
Output is correct |
34 |
Correct |
7 ms |
7004 KB |
Output is correct |
35 |
Correct |
9 ms |
7004 KB |
Output is correct |
36 |
Correct |
7 ms |
7108 KB |
Output is correct |
37 |
Correct |
6 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
1 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
2 ms |
7008 KB |
Output is correct |
6 |
Correct |
3 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7096 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7116 KB |
Output is correct |
12 |
Correct |
2 ms |
7108 KB |
Output is correct |
13 |
Correct |
2 ms |
7000 KB |
Output is correct |
14 |
Correct |
2 ms |
7004 KB |
Output is correct |
15 |
Correct |
1 ms |
7004 KB |
Output is correct |
16 |
Correct |
3 ms |
7004 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
2 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
7000 KB |
Output is correct |
20 |
Correct |
3 ms |
7004 KB |
Output is correct |
21 |
Correct |
3 ms |
7004 KB |
Output is correct |
22 |
Correct |
2 ms |
7004 KB |
Output is correct |
23 |
Correct |
2 ms |
7004 KB |
Output is correct |
24 |
Correct |
2 ms |
7004 KB |
Output is correct |
25 |
Correct |
3 ms |
7100 KB |
Output is correct |
26 |
Correct |
2 ms |
7004 KB |
Output is correct |
27 |
Correct |
3 ms |
6884 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
29 |
Correct |
8 ms |
7260 KB |
Output is correct |
30 |
Correct |
6 ms |
7080 KB |
Output is correct |
31 |
Correct |
9 ms |
7004 KB |
Output is correct |
32 |
Correct |
6 ms |
7004 KB |
Output is correct |
33 |
Correct |
3 ms |
7108 KB |
Output is correct |
34 |
Correct |
7 ms |
7004 KB |
Output is correct |
35 |
Correct |
9 ms |
7004 KB |
Output is correct |
36 |
Correct |
7 ms |
7108 KB |
Output is correct |
37 |
Correct |
6 ms |
7004 KB |
Output is correct |
38 |
Correct |
637 ms |
24236 KB |
Output is correct |
39 |
Correct |
1403 ms |
250216 KB |
Output is correct |
40 |
Correct |
986 ms |
13996 KB |
Output is correct |
41 |
Correct |
852 ms |
8272 KB |
Output is correct |
42 |
Correct |
422 ms |
15956 KB |
Output is correct |
43 |
Correct |
83 ms |
8020 KB |
Output is correct |
44 |
Correct |
104 ms |
7508 KB |
Output is correct |
45 |
Correct |
1712 ms |
12888 KB |
Output is correct |
46 |
Correct |
1626 ms |
12716 KB |
Output is correct |
47 |
Correct |
851 ms |
54672 KB |
Output is correct |
48 |
Correct |
854 ms |
56712 KB |
Output is correct |
49 |
Correct |
1102 ms |
14960 KB |
Output is correct |
50 |
Correct |
1159 ms |
15260 KB |
Output is correct |
51 |
Correct |
387 ms |
12628 KB |
Output is correct |
52 |
Correct |
392 ms |
12628 KB |
Output is correct |
53 |
Correct |
95 ms |
7760 KB |
Output is correct |
54 |
Correct |
1409 ms |
12196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
2 ms |
7000 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
13 ms |
7004 KB |
Output is correct |
6 |
Correct |
2 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
2 ms |
7004 KB |
Output is correct |
9 |
Correct |
2 ms |
7004 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
2 ms |
7004 KB |
Output is correct |
12 |
Correct |
6 ms |
7004 KB |
Output is correct |
13 |
Correct |
65 ms |
7156 KB |
Output is correct |
14 |
Correct |
62 ms |
7004 KB |
Output is correct |
15 |
Correct |
60 ms |
7004 KB |
Output is correct |
16 |
Execution timed out |
5048 ms |
11672 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
1 ms |
7004 KB |
Output is correct |
4 |
Correct |
11 ms |
7108 KB |
Output is correct |
5 |
Correct |
23 ms |
7112 KB |
Output is correct |
6 |
Correct |
3 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
11 ms |
7260 KB |
Output is correct |
9 |
Correct |
641 ms |
24400 KB |
Output is correct |
10 |
Correct |
1426 ms |
250180 KB |
Output is correct |
11 |
Correct |
13 ms |
7004 KB |
Output is correct |
12 |
Correct |
97 ms |
7772 KB |
Output is correct |
13 |
Execution timed out |
5014 ms |
17408 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |