#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int INF = 1e9;
const int SIZE = 24e4 + 5;
const int H = __lg(SIZE) + 1;
int n, m;
bool ans;
vector<int> adj[SIZE];
int s[SIZE], t[SIZE], c[SIZE];
int h[SIZE], to[SIZE][H + 1];
vector<int> g[SIZE];
int deg[SIZE];
queue<int> q;
void dfs(int pos, int fa) {
for (int np : adj[pos]) if (np != fa) {
h[np] = h[pos] + 1;
to[np][0] = pos;
dfs(np, pos);
}
}
int jump(int pos, int k) {
if (k < 0) return 0;
int cnt = 0;
while (k) {
if (k & 1) pos = to[pos][cnt];
cnt++;
k >>= 1;
}
return pos;
}
int lca(int a, int b) {
if (h[a] < h[b]) swap(a, b);
a = jump(a, h[a] - h[b]);
if (a == b) return a;
for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
a = to[a][i];
b = to[b][i];
}
return to[a][0];
}
bool on_path(int pos, int a, int c, int b) {
return h[pos] >= h[c] && (jump(a, h[a] - h[pos]) == pos || jump(b, h[b] - h[pos]) == pos);
}
void add_edge(int a, int b) {
g[a].pb(b);
deg[b]++;
}
void add(int a, int b) {
vector<int> v;
for (int x : {s[a], t[a]}) for (int y : {s[b], t[b]}) {
int z = lca(x, y);
if (on_path(z, s[a], c[a], t[a]) && on_path(z, s[b], c[b], t[b])) v.pb(z);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
if (v.size() == 0) return;
if (v.size() == 1) {
int k = v[0];
if (s[a] == k) add_edge(a, b);
else if (s[b] == k) add_edge(b, a);
else if (t[a] == k) add_edge(b, a);
else if (t[b] == k) add_edge(a, b);
return;
}
if (v.size() == 3) {
int mn = h[v[0]];
for (int x : v) mn = min(mn, h[x]);
for (int i = 0; i < 3; i++) if (h[v[i]] == mn) {v.erase(v.begin() + i); break;}
}
assert(v.size() == 2);
int k1 = v[0], k2 = v[1];
bool isa, ita, isb, itb;
isa = s[a] == k1 || s[a] == k2;
ita = t[a] == k1 || t[a] == k2;
isb = s[b] == k1 || s[b] == k2;
itb = t[b] == k1 || t[b] == k2;
if ((isa && ita) || (isb && itb)) {
ans = 0;
return;
}
if ((isa && isb) || (ita && itb)) {
ans = 0;
return;
}
if (isa) add_edge(a, b);
else if (isb) add_edge(b, a);
else if (ita) add_edge(b, a);
else if (itb) add_edge(a, b);
}
tuple<int, int, int> p[SIZE];
void chain() {
cin >> m;
FOR (i, 1, m) {
int l, r;
cin >> l >> r;
p[i] = {min(l, r), max(l, r), l > r};
}
sort(p + 1, p + m + 1);
int last = 0, t;
FOR (i, 1, m) {
auto [l, r, ty] = p[i];
if (r <= last) {
cout << "No\n";
return;
}
if (l <= last && ty != t) {
cout << "No\n";
return;
}
last = r;
t = ty;
}
cout << "Yes\n";
}
void solve() {
cin >> n;
FOR (i, 1, n) adj[i].clear();
bool f = 1;
FOR (i, 2, n) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
f &= a + 1 == b;
}
if (f) {
chain();
return;
}
h[1] = 1, dfs(1, 1);
FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];
cin >> m;
FOR (i, 1, m) {
cin >> s[i] >> t[i];
c[i] = lca(s[i], t[i]);
g[i].clear();
deg[i] = 0;
}
ans = 1;
FOR (i, 1, m) FOR (j, i + 1, m) {
add(i, j);
if (!ans) {
cout << "No\n";
return;
}
}
FOR (i, 1, m) if (!deg[i]) q.push(i);
int cnt = 0;
while (q.size()) {
int pos = q.front();
q.pop();
cnt++;
for (int np : g[pos]) if (--deg[np] == 0) q.push(np);
}
cout << (cnt == m ? "Yes" : "No") << '\n';
}
int main() {
Waimai;
int tt;
cin >> tt;
while (tt--) solve();
}
Compilation message
jail.cpp: In function 'void chain()':
jail.cpp:140:23: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
140 | if (l <= last && ty != t) {
| ~~~~~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11604 KB |
Output is correct |
2 |
Correct |
6 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11604 KB |
Output is correct |
4 |
Correct |
13 ms |
11620 KB |
Output is correct |
5 |
Correct |
20 ms |
11616 KB |
Output is correct |
6 |
Correct |
7 ms |
11604 KB |
Output is correct |
7 |
Correct |
7 ms |
11604 KB |
Output is correct |
8 |
Correct |
8 ms |
11604 KB |
Output is correct |
9 |
Correct |
23 ms |
11804 KB |
Output is correct |
10 |
Correct |
31 ms |
15256 KB |
Output is correct |
11 |
Correct |
10 ms |
11604 KB |
Output is correct |
12 |
Correct |
24 ms |
11608 KB |
Output is correct |
13 |
Correct |
37 ms |
15848 KB |
Output is correct |
14 |
Correct |
40 ms |
15840 KB |
Output is correct |
15 |
Correct |
50 ms |
15892 KB |
Output is correct |
16 |
Correct |
58 ms |
16744 KB |
Output is correct |
17 |
Correct |
41 ms |
15936 KB |
Output is correct |
18 |
Correct |
48 ms |
16672 KB |
Output is correct |
19 |
Correct |
40 ms |
16064 KB |
Output is correct |
20 |
Correct |
40 ms |
16044 KB |
Output is correct |
21 |
Correct |
40 ms |
15964 KB |
Output is correct |
22 |
Correct |
41 ms |
16100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11604 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11520 KB |
Output is correct |
4 |
Correct |
7 ms |
11596 KB |
Output is correct |
5 |
Correct |
8 ms |
11672 KB |
Output is correct |
6 |
Correct |
8 ms |
11656 KB |
Output is correct |
7 |
Correct |
7 ms |
11604 KB |
Output is correct |
8 |
Correct |
7 ms |
11604 KB |
Output is correct |
9 |
Correct |
9 ms |
11600 KB |
Output is correct |
10 |
Correct |
7 ms |
11648 KB |
Output is correct |
11 |
Correct |
7 ms |
11604 KB |
Output is correct |
12 |
Correct |
8 ms |
11604 KB |
Output is correct |
13 |
Correct |
8 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11604 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11520 KB |
Output is correct |
4 |
Correct |
7 ms |
11596 KB |
Output is correct |
5 |
Correct |
8 ms |
11672 KB |
Output is correct |
6 |
Correct |
8 ms |
11656 KB |
Output is correct |
7 |
Correct |
7 ms |
11604 KB |
Output is correct |
8 |
Correct |
7 ms |
11604 KB |
Output is correct |
9 |
Correct |
9 ms |
11600 KB |
Output is correct |
10 |
Correct |
7 ms |
11648 KB |
Output is correct |
11 |
Correct |
7 ms |
11604 KB |
Output is correct |
12 |
Correct |
8 ms |
11604 KB |
Output is correct |
13 |
Correct |
8 ms |
11604 KB |
Output is correct |
14 |
Correct |
6 ms |
11604 KB |
Output is correct |
15 |
Correct |
7 ms |
11604 KB |
Output is correct |
16 |
Correct |
7 ms |
11604 KB |
Output is correct |
17 |
Correct |
7 ms |
11604 KB |
Output is correct |
18 |
Correct |
9 ms |
11600 KB |
Output is correct |
19 |
Correct |
7 ms |
11604 KB |
Output is correct |
20 |
Correct |
8 ms |
11604 KB |
Output is correct |
21 |
Correct |
7 ms |
11604 KB |
Output is correct |
22 |
Correct |
8 ms |
11668 KB |
Output is correct |
23 |
Correct |
8 ms |
11604 KB |
Output is correct |
24 |
Correct |
7 ms |
11604 KB |
Output is correct |
25 |
Correct |
9 ms |
11604 KB |
Output is correct |
26 |
Correct |
7 ms |
11604 KB |
Output is correct |
27 |
Correct |
8 ms |
11668 KB |
Output is correct |
28 |
Correct |
7 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11604 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11520 KB |
Output is correct |
4 |
Correct |
7 ms |
11596 KB |
Output is correct |
5 |
Correct |
8 ms |
11672 KB |
Output is correct |
6 |
Correct |
8 ms |
11656 KB |
Output is correct |
7 |
Correct |
7 ms |
11604 KB |
Output is correct |
8 |
Correct |
7 ms |
11604 KB |
Output is correct |
9 |
Correct |
9 ms |
11600 KB |
Output is correct |
10 |
Correct |
7 ms |
11648 KB |
Output is correct |
11 |
Correct |
7 ms |
11604 KB |
Output is correct |
12 |
Correct |
8 ms |
11604 KB |
Output is correct |
13 |
Correct |
8 ms |
11604 KB |
Output is correct |
14 |
Correct |
6 ms |
11604 KB |
Output is correct |
15 |
Correct |
7 ms |
11604 KB |
Output is correct |
16 |
Correct |
7 ms |
11604 KB |
Output is correct |
17 |
Correct |
7 ms |
11604 KB |
Output is correct |
18 |
Correct |
9 ms |
11600 KB |
Output is correct |
19 |
Correct |
7 ms |
11604 KB |
Output is correct |
20 |
Correct |
8 ms |
11604 KB |
Output is correct |
21 |
Correct |
7 ms |
11604 KB |
Output is correct |
22 |
Correct |
8 ms |
11668 KB |
Output is correct |
23 |
Correct |
8 ms |
11604 KB |
Output is correct |
24 |
Correct |
7 ms |
11604 KB |
Output is correct |
25 |
Correct |
9 ms |
11604 KB |
Output is correct |
26 |
Correct |
7 ms |
11604 KB |
Output is correct |
27 |
Correct |
8 ms |
11668 KB |
Output is correct |
28 |
Correct |
7 ms |
11604 KB |
Output is correct |
29 |
Correct |
8 ms |
11604 KB |
Output is correct |
30 |
Correct |
13 ms |
11604 KB |
Output is correct |
31 |
Correct |
9 ms |
11604 KB |
Output is correct |
32 |
Correct |
17 ms |
11668 KB |
Output is correct |
33 |
Correct |
8 ms |
11604 KB |
Output is correct |
34 |
Correct |
22 ms |
11604 KB |
Output is correct |
35 |
Correct |
23 ms |
11608 KB |
Output is correct |
36 |
Correct |
29 ms |
11628 KB |
Output is correct |
37 |
Correct |
28 ms |
11668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11604 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11520 KB |
Output is correct |
4 |
Correct |
7 ms |
11596 KB |
Output is correct |
5 |
Correct |
8 ms |
11672 KB |
Output is correct |
6 |
Correct |
8 ms |
11656 KB |
Output is correct |
7 |
Correct |
7 ms |
11604 KB |
Output is correct |
8 |
Correct |
7 ms |
11604 KB |
Output is correct |
9 |
Correct |
9 ms |
11600 KB |
Output is correct |
10 |
Correct |
7 ms |
11648 KB |
Output is correct |
11 |
Correct |
7 ms |
11604 KB |
Output is correct |
12 |
Correct |
8 ms |
11604 KB |
Output is correct |
13 |
Correct |
8 ms |
11604 KB |
Output is correct |
14 |
Correct |
6 ms |
11604 KB |
Output is correct |
15 |
Correct |
7 ms |
11604 KB |
Output is correct |
16 |
Correct |
7 ms |
11604 KB |
Output is correct |
17 |
Correct |
7 ms |
11604 KB |
Output is correct |
18 |
Correct |
9 ms |
11600 KB |
Output is correct |
19 |
Correct |
7 ms |
11604 KB |
Output is correct |
20 |
Correct |
8 ms |
11604 KB |
Output is correct |
21 |
Correct |
7 ms |
11604 KB |
Output is correct |
22 |
Correct |
8 ms |
11668 KB |
Output is correct |
23 |
Correct |
8 ms |
11604 KB |
Output is correct |
24 |
Correct |
7 ms |
11604 KB |
Output is correct |
25 |
Correct |
9 ms |
11604 KB |
Output is correct |
26 |
Correct |
7 ms |
11604 KB |
Output is correct |
27 |
Correct |
8 ms |
11668 KB |
Output is correct |
28 |
Correct |
7 ms |
11604 KB |
Output is correct |
29 |
Correct |
8 ms |
11604 KB |
Output is correct |
30 |
Correct |
13 ms |
11604 KB |
Output is correct |
31 |
Correct |
9 ms |
11604 KB |
Output is correct |
32 |
Correct |
17 ms |
11668 KB |
Output is correct |
33 |
Correct |
8 ms |
11604 KB |
Output is correct |
34 |
Correct |
22 ms |
11604 KB |
Output is correct |
35 |
Correct |
23 ms |
11608 KB |
Output is correct |
36 |
Correct |
29 ms |
11628 KB |
Output is correct |
37 |
Correct |
28 ms |
11668 KB |
Output is correct |
38 |
Correct |
22 ms |
11808 KB |
Output is correct |
39 |
Correct |
31 ms |
15320 KB |
Output is correct |
40 |
Correct |
422 ms |
12364 KB |
Output is correct |
41 |
Correct |
986 ms |
12480 KB |
Output is correct |
42 |
Correct |
427 ms |
12644 KB |
Output is correct |
43 |
Correct |
35 ms |
12372 KB |
Output is correct |
44 |
Correct |
435 ms |
11856 KB |
Output is correct |
45 |
Correct |
121 ms |
24824 KB |
Output is correct |
46 |
Correct |
174 ms |
24908 KB |
Output is correct |
47 |
Correct |
167 ms |
26864 KB |
Output is correct |
48 |
Correct |
187 ms |
26732 KB |
Output is correct |
49 |
Correct |
61 ms |
24780 KB |
Output is correct |
50 |
Correct |
161 ms |
24844 KB |
Output is correct |
51 |
Correct |
65 ms |
25360 KB |
Output is correct |
52 |
Correct |
54 ms |
25428 KB |
Output is correct |
53 |
Correct |
503 ms |
12576 KB |
Output is correct |
54 |
Correct |
132 ms |
24784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11604 KB |
Output is correct |
2 |
Correct |
6 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11604 KB |
Output is correct |
4 |
Correct |
7 ms |
11604 KB |
Output is correct |
5 |
Correct |
10 ms |
11604 KB |
Output is correct |
6 |
Correct |
7 ms |
11604 KB |
Output is correct |
7 |
Correct |
8 ms |
11616 KB |
Output is correct |
8 |
Correct |
7 ms |
11604 KB |
Output is correct |
9 |
Correct |
6 ms |
11604 KB |
Output is correct |
10 |
Correct |
7 ms |
11604 KB |
Output is correct |
11 |
Correct |
7 ms |
11604 KB |
Output is correct |
12 |
Correct |
30 ms |
11776 KB |
Output is correct |
13 |
Correct |
44 ms |
11604 KB |
Output is correct |
14 |
Correct |
169 ms |
11640 KB |
Output is correct |
15 |
Correct |
142 ms |
11652 KB |
Output is correct |
16 |
Execution timed out |
5044 ms |
25152 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11604 KB |
Output is correct |
2 |
Correct |
6 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11604 KB |
Output is correct |
4 |
Correct |
13 ms |
11620 KB |
Output is correct |
5 |
Correct |
20 ms |
11616 KB |
Output is correct |
6 |
Correct |
7 ms |
11604 KB |
Output is correct |
7 |
Correct |
7 ms |
11604 KB |
Output is correct |
8 |
Correct |
8 ms |
11604 KB |
Output is correct |
9 |
Correct |
23 ms |
11804 KB |
Output is correct |
10 |
Correct |
31 ms |
15256 KB |
Output is correct |
11 |
Correct |
10 ms |
11604 KB |
Output is correct |
12 |
Correct |
24 ms |
11608 KB |
Output is correct |
13 |
Correct |
37 ms |
15848 KB |
Output is correct |
14 |
Correct |
40 ms |
15840 KB |
Output is correct |
15 |
Correct |
50 ms |
15892 KB |
Output is correct |
16 |
Correct |
58 ms |
16744 KB |
Output is correct |
17 |
Correct |
41 ms |
15936 KB |
Output is correct |
18 |
Correct |
48 ms |
16672 KB |
Output is correct |
19 |
Correct |
40 ms |
16064 KB |
Output is correct |
20 |
Correct |
40 ms |
16044 KB |
Output is correct |
21 |
Correct |
40 ms |
15964 KB |
Output is correct |
22 |
Correct |
41 ms |
16100 KB |
Output is correct |
23 |
Correct |
8 ms |
11604 KB |
Output is correct |
24 |
Correct |
7 ms |
11604 KB |
Output is correct |
25 |
Correct |
7 ms |
11520 KB |
Output is correct |
26 |
Correct |
7 ms |
11596 KB |
Output is correct |
27 |
Correct |
8 ms |
11672 KB |
Output is correct |
28 |
Correct |
8 ms |
11656 KB |
Output is correct |
29 |
Correct |
7 ms |
11604 KB |
Output is correct |
30 |
Correct |
7 ms |
11604 KB |
Output is correct |
31 |
Correct |
9 ms |
11600 KB |
Output is correct |
32 |
Correct |
7 ms |
11648 KB |
Output is correct |
33 |
Correct |
7 ms |
11604 KB |
Output is correct |
34 |
Correct |
8 ms |
11604 KB |
Output is correct |
35 |
Correct |
8 ms |
11604 KB |
Output is correct |
36 |
Correct |
6 ms |
11604 KB |
Output is correct |
37 |
Correct |
7 ms |
11604 KB |
Output is correct |
38 |
Correct |
7 ms |
11604 KB |
Output is correct |
39 |
Correct |
7 ms |
11604 KB |
Output is correct |
40 |
Correct |
9 ms |
11600 KB |
Output is correct |
41 |
Correct |
7 ms |
11604 KB |
Output is correct |
42 |
Correct |
8 ms |
11604 KB |
Output is correct |
43 |
Correct |
7 ms |
11604 KB |
Output is correct |
44 |
Correct |
8 ms |
11668 KB |
Output is correct |
45 |
Correct |
8 ms |
11604 KB |
Output is correct |
46 |
Correct |
7 ms |
11604 KB |
Output is correct |
47 |
Correct |
9 ms |
11604 KB |
Output is correct |
48 |
Correct |
7 ms |
11604 KB |
Output is correct |
49 |
Correct |
8 ms |
11668 KB |
Output is correct |
50 |
Correct |
7 ms |
11604 KB |
Output is correct |
51 |
Correct |
8 ms |
11604 KB |
Output is correct |
52 |
Correct |
13 ms |
11604 KB |
Output is correct |
53 |
Correct |
9 ms |
11604 KB |
Output is correct |
54 |
Correct |
17 ms |
11668 KB |
Output is correct |
55 |
Correct |
8 ms |
11604 KB |
Output is correct |
56 |
Correct |
22 ms |
11604 KB |
Output is correct |
57 |
Correct |
23 ms |
11608 KB |
Output is correct |
58 |
Correct |
29 ms |
11628 KB |
Output is correct |
59 |
Correct |
28 ms |
11668 KB |
Output is correct |
60 |
Correct |
22 ms |
11808 KB |
Output is correct |
61 |
Correct |
31 ms |
15320 KB |
Output is correct |
62 |
Correct |
422 ms |
12364 KB |
Output is correct |
63 |
Correct |
986 ms |
12480 KB |
Output is correct |
64 |
Correct |
427 ms |
12644 KB |
Output is correct |
65 |
Correct |
35 ms |
12372 KB |
Output is correct |
66 |
Correct |
435 ms |
11856 KB |
Output is correct |
67 |
Correct |
121 ms |
24824 KB |
Output is correct |
68 |
Correct |
174 ms |
24908 KB |
Output is correct |
69 |
Correct |
167 ms |
26864 KB |
Output is correct |
70 |
Correct |
187 ms |
26732 KB |
Output is correct |
71 |
Correct |
61 ms |
24780 KB |
Output is correct |
72 |
Correct |
161 ms |
24844 KB |
Output is correct |
73 |
Correct |
65 ms |
25360 KB |
Output is correct |
74 |
Correct |
54 ms |
25428 KB |
Output is correct |
75 |
Correct |
503 ms |
12576 KB |
Output is correct |
76 |
Correct |
132 ms |
24784 KB |
Output is correct |
77 |
Correct |
7 ms |
11604 KB |
Output is correct |
78 |
Correct |
6 ms |
11604 KB |
Output is correct |
79 |
Correct |
7 ms |
11604 KB |
Output is correct |
80 |
Correct |
7 ms |
11604 KB |
Output is correct |
81 |
Correct |
10 ms |
11604 KB |
Output is correct |
82 |
Correct |
7 ms |
11604 KB |
Output is correct |
83 |
Correct |
8 ms |
11616 KB |
Output is correct |
84 |
Correct |
7 ms |
11604 KB |
Output is correct |
85 |
Correct |
6 ms |
11604 KB |
Output is correct |
86 |
Correct |
7 ms |
11604 KB |
Output is correct |
87 |
Correct |
7 ms |
11604 KB |
Output is correct |
88 |
Correct |
30 ms |
11776 KB |
Output is correct |
89 |
Correct |
44 ms |
11604 KB |
Output is correct |
90 |
Correct |
169 ms |
11640 KB |
Output is correct |
91 |
Correct |
142 ms |
11652 KB |
Output is correct |
92 |
Execution timed out |
5044 ms |
25152 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |