#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 12e4;
const int BITS = 17;
vector<int> edges[MAXN];
bool adj[500][500];
int n, m;
pii queries[MAXN];
int lca[MAXN][BITS];
int depth[MAXN];
vector<int> order;
bool vis[500];
class query {
public:
int l, r;
bool f;
query(int l, int r, bool f) : l(l), r(r), f(f) {}
};
bool operator<(query &a, query &b) {
return (a.l == b.l) ? (a.r > b.r) : (a.l < b.l);
}
void dfs(int cur = 0, int p = -1) {
for (int nxt: edges[cur]) {
if (nxt == p) continue;
depth[nxt] = depth[cur]+1;
lca[nxt][0] = cur;
dfs(nxt, cur);
}
}
int get_lca(int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
int diff = depth[x]-depth[y];
int p = 0;
while (diff) {
if (diff & 1) x = lca[x][p];
diff /= 2;
p++;
}
if (x == y) return x;
for (int i = 16; i >= 0; i--) {
if (lca[x][i] != lca[y][i]) {
x = lca[x][i];
y = lca[y][i];
}
}
return lca[x][0];
}
int dist(int x, int y) {
return depth[x]+depth[y]-2*depth[get_lca(x, y)];
}
bool on_seg(int a, int b, int c) {
return dist(a, b) == dist(a, c)+dist(b, c);
}
void dfs2(int cur) {
vis[cur] = 1;
for (int i = 0; i < m; i++) {
if (!vis[i] && adj[cur][i]) dfs2(i);
}
order.push_back(cur);
}
int main() {
int t; cin >> t;
while (t--) {
bool s1 = 1;
cin >> n;
for (int i = 0; i < n; i++) edges[i].clear();
for (int i = 0; i < n-1; i++) {
int x, y; cin >> x >> y;
x--; y--;
edges[x].push_back(y);
edges[y].push_back(x);
if (y-x != 1) s1 = 0;
}
cin >> m;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
queries[i] = pii(x-1, y-1);
}
if (s1) {
vector<query> sorted;
for (int i = 0; i < m; i++) {
if (queries[i].first > queries[i].second) sorted.push_back(query(queries[i].second, queries[i].first, 1));
else sorted.push_back(query(queries[i].first, queries[i].second, 0));
}
sort(sorted.begin(), sorted.end());
int r[2];
r[0] = r[1] = -1;
bool ans = 1;
for (query q: sorted) {
if (r[q.f] >= q.r || r[!q.f] >= q.l) {
ans = 0;
break;
}
r[q.f] = q.r;
}
if (ans) {
cout << "Yes\n";
}
else cout << "No\n";
}
else if (m <= 500) {
dfs();
for (int i = 1; i < BITS; i++) {
for (int j = 0; j < n; j++) {
lca[j][i] = lca[lca[j][i-1]][i-1];
}
}
for (int i = 0; i < m; i++) fill(adj[i], adj[i]+m, 0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (i == j) continue;
adj[i][j] = (on_seg(queries[i].first, queries[i].second, queries[j].first) || on_seg(queries[j].first, queries[j].second, queries[i].second));
}
}
if (m == 2) {
if (adj[0][1] && adj[1][0]) cout << "No\n";
else cout << "Yes\n";
continue;
}
fill(vis, vis+m, 0);
order.clear();
for (int i = 0; i < m; i++) {
if (!vis[i]) dfs2(i);
}
bool ans = 1;
for (int i = 0; i < m && ans; i++) {
for (int j = i+1; j < m && ans; j++) {
if (adj[order[i]][order[j]]) ans = 0;
}
}
if (ans) cout << "Yes\n";
else cout << "No\n";
}
else {
srand(0);
if (rand()%2) cout << "Yes\n";
else cout << "No\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
27 ms |
3028 KB |
Output is correct |
5 |
Correct |
45 ms |
3028 KB |
Output is correct |
6 |
Correct |
3 ms |
3028 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
4 ms |
3028 KB |
Output is correct |
9 |
Correct |
57 ms |
3244 KB |
Output is correct |
10 |
Correct |
69 ms |
6856 KB |
Output is correct |
11 |
Correct |
11 ms |
3028 KB |
Output is correct |
12 |
Correct |
56 ms |
3028 KB |
Output is correct |
13 |
Correct |
95 ms |
8108 KB |
Output is correct |
14 |
Correct |
96 ms |
8100 KB |
Output is correct |
15 |
Correct |
98 ms |
8088 KB |
Output is correct |
16 |
Correct |
139 ms |
9412 KB |
Output is correct |
17 |
Correct |
109 ms |
8236 KB |
Output is correct |
18 |
Correct |
137 ms |
9500 KB |
Output is correct |
19 |
Correct |
101 ms |
8256 KB |
Output is correct |
20 |
Correct |
100 ms |
8208 KB |
Output is correct |
21 |
Correct |
101 ms |
8148 KB |
Output is correct |
22 |
Correct |
102 ms |
8140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
1 ms |
3028 KB |
Output is correct |
3 |
Correct |
4 ms |
3028 KB |
Output is correct |
4 |
Correct |
4 ms |
3156 KB |
Output is correct |
5 |
Correct |
4 ms |
3204 KB |
Output is correct |
6 |
Correct |
4 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3156 KB |
Output is correct |
8 |
Correct |
4 ms |
3136 KB |
Output is correct |
9 |
Correct |
4 ms |
3156 KB |
Output is correct |
10 |
Correct |
3 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
3 ms |
3156 KB |
Output is correct |
13 |
Correct |
3 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
1 ms |
3028 KB |
Output is correct |
3 |
Correct |
4 ms |
3028 KB |
Output is correct |
4 |
Correct |
4 ms |
3156 KB |
Output is correct |
5 |
Correct |
4 ms |
3204 KB |
Output is correct |
6 |
Correct |
4 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3156 KB |
Output is correct |
8 |
Correct |
4 ms |
3136 KB |
Output is correct |
9 |
Correct |
4 ms |
3156 KB |
Output is correct |
10 |
Correct |
3 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
3 ms |
3156 KB |
Output is correct |
13 |
Correct |
3 ms |
3156 KB |
Output is correct |
14 |
Correct |
2 ms |
3028 KB |
Output is correct |
15 |
Correct |
2 ms |
3028 KB |
Output is correct |
16 |
Correct |
4 ms |
3156 KB |
Output is correct |
17 |
Correct |
4 ms |
3132 KB |
Output is correct |
18 |
Correct |
5 ms |
3136 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
4 ms |
3156 KB |
Output is correct |
21 |
Correct |
4 ms |
3156 KB |
Output is correct |
22 |
Correct |
4 ms |
3156 KB |
Output is correct |
23 |
Correct |
2 ms |
3028 KB |
Output is correct |
24 |
Correct |
2 ms |
3156 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
2 ms |
3156 KB |
Output is correct |
27 |
Correct |
4 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
1 ms |
3028 KB |
Output is correct |
3 |
Correct |
4 ms |
3028 KB |
Output is correct |
4 |
Correct |
4 ms |
3156 KB |
Output is correct |
5 |
Correct |
4 ms |
3204 KB |
Output is correct |
6 |
Correct |
4 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3156 KB |
Output is correct |
8 |
Correct |
4 ms |
3136 KB |
Output is correct |
9 |
Correct |
4 ms |
3156 KB |
Output is correct |
10 |
Correct |
3 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
3 ms |
3156 KB |
Output is correct |
13 |
Correct |
3 ms |
3156 KB |
Output is correct |
14 |
Correct |
2 ms |
3028 KB |
Output is correct |
15 |
Correct |
2 ms |
3028 KB |
Output is correct |
16 |
Correct |
4 ms |
3156 KB |
Output is correct |
17 |
Correct |
4 ms |
3132 KB |
Output is correct |
18 |
Correct |
5 ms |
3136 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
4 ms |
3156 KB |
Output is correct |
21 |
Correct |
4 ms |
3156 KB |
Output is correct |
22 |
Correct |
4 ms |
3156 KB |
Output is correct |
23 |
Correct |
2 ms |
3028 KB |
Output is correct |
24 |
Correct |
2 ms |
3156 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
2 ms |
3156 KB |
Output is correct |
27 |
Correct |
4 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
3028 KB |
Output is correct |
29 |
Correct |
4 ms |
3156 KB |
Output is correct |
30 |
Correct |
27 ms |
3264 KB |
Output is correct |
31 |
Correct |
25 ms |
3156 KB |
Output is correct |
32 |
Correct |
18 ms |
3156 KB |
Output is correct |
33 |
Correct |
7 ms |
3156 KB |
Output is correct |
34 |
Correct |
49 ms |
3156 KB |
Output is correct |
35 |
Correct |
60 ms |
3156 KB |
Output is correct |
36 |
Correct |
38 ms |
3156 KB |
Output is correct |
37 |
Correct |
31 ms |
3136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
1 ms |
3028 KB |
Output is correct |
3 |
Correct |
4 ms |
3028 KB |
Output is correct |
4 |
Correct |
4 ms |
3156 KB |
Output is correct |
5 |
Correct |
4 ms |
3204 KB |
Output is correct |
6 |
Correct |
4 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3156 KB |
Output is correct |
8 |
Correct |
4 ms |
3136 KB |
Output is correct |
9 |
Correct |
4 ms |
3156 KB |
Output is correct |
10 |
Correct |
3 ms |
3156 KB |
Output is correct |
11 |
Correct |
3 ms |
3156 KB |
Output is correct |
12 |
Correct |
3 ms |
3156 KB |
Output is correct |
13 |
Correct |
3 ms |
3156 KB |
Output is correct |
14 |
Correct |
2 ms |
3028 KB |
Output is correct |
15 |
Correct |
2 ms |
3028 KB |
Output is correct |
16 |
Correct |
4 ms |
3156 KB |
Output is correct |
17 |
Correct |
4 ms |
3132 KB |
Output is correct |
18 |
Correct |
5 ms |
3136 KB |
Output is correct |
19 |
Correct |
2 ms |
3028 KB |
Output is correct |
20 |
Correct |
4 ms |
3156 KB |
Output is correct |
21 |
Correct |
4 ms |
3156 KB |
Output is correct |
22 |
Correct |
4 ms |
3156 KB |
Output is correct |
23 |
Correct |
2 ms |
3028 KB |
Output is correct |
24 |
Correct |
2 ms |
3156 KB |
Output is correct |
25 |
Correct |
4 ms |
3156 KB |
Output is correct |
26 |
Correct |
2 ms |
3156 KB |
Output is correct |
27 |
Correct |
4 ms |
3156 KB |
Output is correct |
28 |
Correct |
2 ms |
3028 KB |
Output is correct |
29 |
Correct |
4 ms |
3156 KB |
Output is correct |
30 |
Correct |
27 ms |
3264 KB |
Output is correct |
31 |
Correct |
25 ms |
3156 KB |
Output is correct |
32 |
Correct |
18 ms |
3156 KB |
Output is correct |
33 |
Correct |
7 ms |
3156 KB |
Output is correct |
34 |
Correct |
49 ms |
3156 KB |
Output is correct |
35 |
Correct |
60 ms |
3156 KB |
Output is correct |
36 |
Correct |
38 ms |
3156 KB |
Output is correct |
37 |
Correct |
31 ms |
3136 KB |
Output is correct |
38 |
Correct |
58 ms |
4460 KB |
Output is correct |
39 |
Correct |
71 ms |
8292 KB |
Output is correct |
40 |
Correct |
1730 ms |
5380 KB |
Output is correct |
41 |
Correct |
1509 ms |
5328 KB |
Output is correct |
42 |
Correct |
1167 ms |
5576 KB |
Output is correct |
43 |
Correct |
77 ms |
5060 KB |
Output is correct |
44 |
Correct |
1010 ms |
3640 KB |
Output is correct |
45 |
Correct |
249 ms |
17032 KB |
Output is correct |
46 |
Correct |
250 ms |
17032 KB |
Output is correct |
47 |
Correct |
282 ms |
20736 KB |
Output is correct |
48 |
Correct |
269 ms |
20712 KB |
Output is correct |
49 |
Correct |
264 ms |
17132 KB |
Output is correct |
50 |
Correct |
271 ms |
17192 KB |
Output is correct |
51 |
Correct |
108 ms |
18092 KB |
Output is correct |
52 |
Correct |
112 ms |
18132 KB |
Output is correct |
53 |
Correct |
842 ms |
4636 KB |
Output is correct |
54 |
Correct |
196 ms |
16996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
1 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3120 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Correct |
12 ms |
3156 KB |
Output is correct |
6 |
Correct |
3 ms |
3156 KB |
Output is correct |
7 |
Correct |
3 ms |
3136 KB |
Output is correct |
8 |
Correct |
2 ms |
3028 KB |
Output is correct |
9 |
Correct |
2 ms |
3028 KB |
Output is correct |
10 |
Correct |
2 ms |
3120 KB |
Output is correct |
11 |
Correct |
2 ms |
3028 KB |
Output is correct |
12 |
Correct |
39 ms |
3156 KB |
Output is correct |
13 |
Correct |
487 ms |
3688 KB |
Output is correct |
14 |
Correct |
258 ms |
3916 KB |
Output is correct |
15 |
Correct |
372 ms |
3792 KB |
Output is correct |
16 |
Incorrect |
80 ms |
8548 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
27 ms |
3028 KB |
Output is correct |
5 |
Correct |
45 ms |
3028 KB |
Output is correct |
6 |
Correct |
3 ms |
3028 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
4 ms |
3028 KB |
Output is correct |
9 |
Correct |
57 ms |
3244 KB |
Output is correct |
10 |
Correct |
69 ms |
6856 KB |
Output is correct |
11 |
Correct |
11 ms |
3028 KB |
Output is correct |
12 |
Correct |
56 ms |
3028 KB |
Output is correct |
13 |
Correct |
95 ms |
8108 KB |
Output is correct |
14 |
Correct |
96 ms |
8100 KB |
Output is correct |
15 |
Correct |
98 ms |
8088 KB |
Output is correct |
16 |
Correct |
139 ms |
9412 KB |
Output is correct |
17 |
Correct |
109 ms |
8236 KB |
Output is correct |
18 |
Correct |
137 ms |
9500 KB |
Output is correct |
19 |
Correct |
101 ms |
8256 KB |
Output is correct |
20 |
Correct |
100 ms |
8208 KB |
Output is correct |
21 |
Correct |
101 ms |
8148 KB |
Output is correct |
22 |
Correct |
102 ms |
8140 KB |
Output is correct |
23 |
Correct |
2 ms |
3028 KB |
Output is correct |
24 |
Correct |
1 ms |
3028 KB |
Output is correct |
25 |
Correct |
4 ms |
3028 KB |
Output is correct |
26 |
Correct |
4 ms |
3156 KB |
Output is correct |
27 |
Correct |
4 ms |
3204 KB |
Output is correct |
28 |
Correct |
4 ms |
3156 KB |
Output is correct |
29 |
Correct |
4 ms |
3156 KB |
Output is correct |
30 |
Correct |
4 ms |
3136 KB |
Output is correct |
31 |
Correct |
4 ms |
3156 KB |
Output is correct |
32 |
Correct |
3 ms |
3156 KB |
Output is correct |
33 |
Correct |
3 ms |
3156 KB |
Output is correct |
34 |
Correct |
3 ms |
3156 KB |
Output is correct |
35 |
Correct |
3 ms |
3156 KB |
Output is correct |
36 |
Correct |
2 ms |
3028 KB |
Output is correct |
37 |
Correct |
2 ms |
3028 KB |
Output is correct |
38 |
Correct |
4 ms |
3156 KB |
Output is correct |
39 |
Correct |
4 ms |
3132 KB |
Output is correct |
40 |
Correct |
5 ms |
3136 KB |
Output is correct |
41 |
Correct |
2 ms |
3028 KB |
Output is correct |
42 |
Correct |
4 ms |
3156 KB |
Output is correct |
43 |
Correct |
4 ms |
3156 KB |
Output is correct |
44 |
Correct |
4 ms |
3156 KB |
Output is correct |
45 |
Correct |
2 ms |
3028 KB |
Output is correct |
46 |
Correct |
2 ms |
3156 KB |
Output is correct |
47 |
Correct |
4 ms |
3156 KB |
Output is correct |
48 |
Correct |
2 ms |
3156 KB |
Output is correct |
49 |
Correct |
4 ms |
3156 KB |
Output is correct |
50 |
Correct |
2 ms |
3028 KB |
Output is correct |
51 |
Correct |
4 ms |
3156 KB |
Output is correct |
52 |
Correct |
27 ms |
3264 KB |
Output is correct |
53 |
Correct |
25 ms |
3156 KB |
Output is correct |
54 |
Correct |
18 ms |
3156 KB |
Output is correct |
55 |
Correct |
7 ms |
3156 KB |
Output is correct |
56 |
Correct |
49 ms |
3156 KB |
Output is correct |
57 |
Correct |
60 ms |
3156 KB |
Output is correct |
58 |
Correct |
38 ms |
3156 KB |
Output is correct |
59 |
Correct |
31 ms |
3136 KB |
Output is correct |
60 |
Correct |
58 ms |
4460 KB |
Output is correct |
61 |
Correct |
71 ms |
8292 KB |
Output is correct |
62 |
Correct |
1730 ms |
5380 KB |
Output is correct |
63 |
Correct |
1509 ms |
5328 KB |
Output is correct |
64 |
Correct |
1167 ms |
5576 KB |
Output is correct |
65 |
Correct |
77 ms |
5060 KB |
Output is correct |
66 |
Correct |
1010 ms |
3640 KB |
Output is correct |
67 |
Correct |
249 ms |
17032 KB |
Output is correct |
68 |
Correct |
250 ms |
17032 KB |
Output is correct |
69 |
Correct |
282 ms |
20736 KB |
Output is correct |
70 |
Correct |
269 ms |
20712 KB |
Output is correct |
71 |
Correct |
264 ms |
17132 KB |
Output is correct |
72 |
Correct |
271 ms |
17192 KB |
Output is correct |
73 |
Correct |
108 ms |
18092 KB |
Output is correct |
74 |
Correct |
112 ms |
18132 KB |
Output is correct |
75 |
Correct |
842 ms |
4636 KB |
Output is correct |
76 |
Correct |
196 ms |
16996 KB |
Output is correct |
77 |
Correct |
2 ms |
3028 KB |
Output is correct |
78 |
Correct |
1 ms |
3028 KB |
Output is correct |
79 |
Correct |
2 ms |
3120 KB |
Output is correct |
80 |
Correct |
2 ms |
3028 KB |
Output is correct |
81 |
Correct |
12 ms |
3156 KB |
Output is correct |
82 |
Correct |
3 ms |
3156 KB |
Output is correct |
83 |
Correct |
3 ms |
3136 KB |
Output is correct |
84 |
Correct |
2 ms |
3028 KB |
Output is correct |
85 |
Correct |
2 ms |
3028 KB |
Output is correct |
86 |
Correct |
2 ms |
3120 KB |
Output is correct |
87 |
Correct |
2 ms |
3028 KB |
Output is correct |
88 |
Correct |
39 ms |
3156 KB |
Output is correct |
89 |
Correct |
487 ms |
3688 KB |
Output is correct |
90 |
Correct |
258 ms |
3916 KB |
Output is correct |
91 |
Correct |
372 ms |
3792 KB |
Output is correct |
92 |
Incorrect |
80 ms |
8548 KB |
Output isn't correct |
93 |
Halted |
0 ms |
0 KB |
- |