#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
using Q = const vector<tuple<char, int, int>>&;
void yno(bool b) {
if (b) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
}
namespace s1 {
bool solve(int n, Q queries) {
if (n > 4000) {
return false;
}
vector<int> arr[n];
for (int i = 0; i < n; i++) {
arr[i] = {i};
}
int cnt[n] {};
for (auto& [c, u, v] : queries) {
if (c == 'S') {
if (sz(arr[v]) > sz(arr[u])) {
swap(arr[u], arr[v]);
}
vector<int> out(sz(arr[u]) + sz(arr[v]));
merge(begin(arr[u]), end(arr[u]), begin(arr[v]), end(arr[v]), out.begin());
arr[u] = out;
swap(out, arr[v]);
for (auto& a : arr[u]) {
cnt[a]++;
}
} else if (c == 'Q') {
if (binary_search(begin(arr[u]), end(arr[u]), v)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
} else {
cout << cnt[u] + 1 << endl;
}
}
return true;
}
} // namespace s1
namespace s2 {
bool solve(int n, Q queries) {
for (auto& [c, u, v] : queries) {
if (c == 'S' && u && v) {
return false;
}
}
vector<int> vals;
int ord[n], oind = 0;
memset(ord, -1, sizeof(ord));
for (auto& [c, u, v] : queries) {
if (c == 'S') {
int x = u;
if (!x) {
x = v;
}
vals.push_back(x);
ord[x] = oind++;
} else if (c == 'Q') {
if (u == v) {
yno(true);
} else if (u == 0) {
yno(ord[v] != -1);
} else if (v == 0) {
yno(ord[u] != -1);
} else {
yno(ord[u] != -1 && ord[v] != -1 && ord[u] > ord[v]);
}
} else {
if (u == 0) {
cout << oind + 1 << endl;
} else if (ord[u] == -1) {
cout << 1 << endl;
} else {
cout << oind - ord[u] + 1 << endl;
}
}
}
return true;
}
} // namespace s2
namespace s3 {
const int maxn = 2e5, logn = 20;
vector<pair<int, int>> graph[maxn];
int t, tin[maxn], tout[maxn], par[maxn], parv[maxn], lift1[logn][maxn], lift2[logn][maxn],
liftv1[logn][maxn], liftv2[logn][maxn];
void dfs(int u, int p = -1) {
tin[u] = t++;
par[u] = lift1[0][u] = lift2[0][u] = p;
for (auto& [v, w] : graph[u]) {
if (v != p) {
parv[v] = liftv1[0][v] = liftv2[0][v] = w;
dfs(v, u);
}
}
tout[u] = t++;
}
bool anc(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
template <typename T>
int blift(int u, int v, int lift[logn][maxn], int liftv[logn][maxn], const T& t) {
if (anc(u, v)) {
return u;
}
int prev = -1;
for (int i = logn - 1; i >= 0; i--) {
int p = lift[i][u];
if (p != -1 && !anc(p, v)) {
if (prev != -1 && !t(prev, parv[u])) {
return -1;
}
prev = liftv[i][u];
u = p;
}
}
if (lift[0][u] == -1) {
return -1;
}
if (prev != -1 && !t(prev, parv[u])) {
return -1;
}
return u;
}
bool solve(int n, Q queries) {
for (auto& [c, _u, _v] : queries) {
if (c == 'C') {
return false;
}
}
int ct = 0;
for (auto& [c, u, v] : queries) {
if (c == 'S') {
graph[u].emplace_back(v, ct);
graph[v].emplace_back(u, ct);
ct++;
}
}
dfs(0);
// 1 - increasing upwards
// 2 - decreasing upwards
for (int i = 1; i < logn; i++) {
for (int j = 0; j < n; j++) {
{
int p = lift1[i - 1][j];
if (p != -1 && liftv1[i - 1][j] < parv[p]) {
lift1[i][j] = lift1[i - 1][p];
liftv1[i][j] = liftv1[i - 1][p];
} else {
lift1[i][j] = -1;
}
}
{
int p = lift2[i - 1][j];
if (p != -1 && liftv2[i - 1][j] > parv[p]) {
lift2[i][j] = lift2[i - 1][p];
liftv2[i][j] = liftv2[i - 1][p];
} else {
lift2[i][j] = -1;
}
}
}
}
ct = -1;
for (auto [c, u, v] : queries) {
if (c == 'S') {
ct++;
} else {
swap(u, v);
if (u == v) {
yno(true);
} else {
int cu = blift(u, v, lift1, liftv1, [&](int a, int b) -> bool {
return a < b;
});
int cv = blift(v, u, lift2, liftv2, [&](int a, int b) -> bool {
return b < a;
});
if (cu != -1 && cv != -1) {
dbg(u, v, cu, cv, par[cu], par[cv]);
int lca = anc(u, v) ? u : par[cu];
dbg(lca);
if (lca != (anc(v, u) ? v : par[cv])) {
yno(false);
continue;
}
assert(lca == (anc(v, u) ? v : par[cv]));
if (anc(u, v)) {
yno(parv[v] <= ct);
} else if (anc(v, u)) {
yno(parv[cu] <= ct);
} else {
yno(parv[cu] < parv[cv] && parv[v] <= ct);
}
} else {
yno(false);
}
}
}
}
return true;
}
} // namespace s3
void solve() {
int n, q;
cin >> n >> q;
vector<tuple<char, int, int>> queries(n + q - 1);
for (auto& [c, u, v] : queries) {
cin >> c;
if (c != 'C') {
cin >> u >> v;
u--;
v--;
} else {
cin >> u;
u--;
}
}
s3::solve(n, queries) || s2::solve(n, queries) || s1::solve(n, queries);
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin.exceptions(ios::failbit);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7904 KB |
Output is correct |
2 |
Correct |
49 ms |
9400 KB |
Output is correct |
3 |
Correct |
45 ms |
9392 KB |
Output is correct |
4 |
Correct |
44 ms |
9536 KB |
Output is correct |
5 |
Correct |
44 ms |
9748 KB |
Output is correct |
6 |
Correct |
58 ms |
9360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7904 KB |
Output is correct |
2 |
Correct |
49 ms |
9400 KB |
Output is correct |
3 |
Correct |
45 ms |
9392 KB |
Output is correct |
4 |
Correct |
44 ms |
9536 KB |
Output is correct |
5 |
Correct |
44 ms |
9748 KB |
Output is correct |
6 |
Correct |
58 ms |
9360 KB |
Output is correct |
7 |
Correct |
26 ms |
7572 KB |
Output is correct |
8 |
Correct |
32 ms |
8112 KB |
Output is correct |
9 |
Correct |
41 ms |
13492 KB |
Output is correct |
10 |
Correct |
31 ms |
8096 KB |
Output is correct |
11 |
Correct |
31 ms |
8128 KB |
Output is correct |
12 |
Correct |
28 ms |
8004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
7940 KB |
Output is correct |
2 |
Correct |
140 ms |
37964 KB |
Output is correct |
3 |
Correct |
161 ms |
38012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
7940 KB |
Output is correct |
2 |
Correct |
140 ms |
37964 KB |
Output is correct |
3 |
Correct |
161 ms |
38012 KB |
Output is correct |
4 |
Correct |
25 ms |
7572 KB |
Output is correct |
5 |
Correct |
58 ms |
11972 KB |
Output is correct |
6 |
Correct |
56 ms |
11272 KB |
Output is correct |
7 |
Correct |
48 ms |
11400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
7956 KB |
Output is correct |
2 |
Correct |
137 ms |
47508 KB |
Output is correct |
3 |
Correct |
130 ms |
47428 KB |
Output is correct |
4 |
Correct |
140 ms |
52156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
7956 KB |
Output is correct |
2 |
Correct |
137 ms |
47508 KB |
Output is correct |
3 |
Correct |
130 ms |
47428 KB |
Output is correct |
4 |
Correct |
140 ms |
52156 KB |
Output is correct |
5 |
Correct |
26 ms |
7620 KB |
Output is correct |
6 |
Partially correct |
41 ms |
10772 KB |
Output is incorrect |
7 |
Partially correct |
43 ms |
10768 KB |
Output is incorrect |
8 |
Partially correct |
42 ms |
10392 KB |
Output is incorrect |
9 |
Partially correct |
41 ms |
10436 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
7948 KB |
Output is correct |
2 |
Correct |
138 ms |
40196 KB |
Output is correct |
3 |
Correct |
132 ms |
41292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
7948 KB |
Output is correct |
2 |
Correct |
138 ms |
40196 KB |
Output is correct |
3 |
Correct |
132 ms |
41292 KB |
Output is correct |
4 |
Correct |
29 ms |
7572 KB |
Output is correct |
5 |
Partially correct |
50 ms |
10628 KB |
Output is incorrect |
6 |
Partially correct |
43 ms |
10632 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
8004 KB |
Output is correct |
2 |
Correct |
121 ms |
47428 KB |
Output is correct |
3 |
Correct |
141 ms |
47440 KB |
Output is correct |
4 |
Correct |
139 ms |
52120 KB |
Output is correct |
5 |
Correct |
33 ms |
8080 KB |
Output is correct |
6 |
Correct |
147 ms |
40232 KB |
Output is correct |
7 |
Correct |
127 ms |
41356 KB |
Output is correct |
8 |
Correct |
177 ms |
41860 KB |
Output is correct |
9 |
Correct |
157 ms |
41668 KB |
Output is correct |
10 |
Correct |
158 ms |
56988 KB |
Output is correct |
11 |
Correct |
245 ms |
55488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
8004 KB |
Output is correct |
2 |
Correct |
121 ms |
47428 KB |
Output is correct |
3 |
Correct |
141 ms |
47440 KB |
Output is correct |
4 |
Correct |
139 ms |
52120 KB |
Output is correct |
5 |
Correct |
33 ms |
8080 KB |
Output is correct |
6 |
Correct |
147 ms |
40232 KB |
Output is correct |
7 |
Correct |
127 ms |
41356 KB |
Output is correct |
8 |
Correct |
177 ms |
41860 KB |
Output is correct |
9 |
Correct |
157 ms |
41668 KB |
Output is correct |
10 |
Correct |
158 ms |
56988 KB |
Output is correct |
11 |
Correct |
245 ms |
55488 KB |
Output is correct |
12 |
Correct |
25 ms |
7620 KB |
Output is correct |
13 |
Partially correct |
41 ms |
10696 KB |
Output is incorrect |
14 |
Partially correct |
42 ms |
10760 KB |
Output is incorrect |
15 |
Partially correct |
39 ms |
10436 KB |
Output is incorrect |
16 |
Partially correct |
37 ms |
10384 KB |
Output is incorrect |
17 |
Correct |
22 ms |
7572 KB |
Output is correct |
18 |
Partially correct |
39 ms |
10692 KB |
Output is incorrect |
19 |
Partially correct |
42 ms |
10632 KB |
Output is incorrect |
20 |
Partially correct |
42 ms |
10656 KB |
Output is incorrect |
21 |
Partially correct |
45 ms |
10768 KB |
Output is incorrect |
22 |
Partially correct |
36 ms |
10504 KB |
Output is incorrect |
23 |
Partially correct |
41 ms |
10764 KB |
Output is incorrect |
24 |
Partially correct |
49 ms |
11076 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
7956 KB |
Output is correct |
2 |
Correct |
45 ms |
9408 KB |
Output is correct |
3 |
Correct |
61 ms |
9396 KB |
Output is correct |
4 |
Correct |
48 ms |
9488 KB |
Output is correct |
5 |
Correct |
39 ms |
9632 KB |
Output is correct |
6 |
Correct |
41 ms |
9416 KB |
Output is correct |
7 |
Correct |
42 ms |
7972 KB |
Output is correct |
8 |
Correct |
140 ms |
38024 KB |
Output is correct |
9 |
Correct |
134 ms |
38016 KB |
Output is correct |
10 |
Correct |
30 ms |
7876 KB |
Output is correct |
11 |
Correct |
129 ms |
47536 KB |
Output is correct |
12 |
Correct |
130 ms |
47420 KB |
Output is correct |
13 |
Correct |
176 ms |
52120 KB |
Output is correct |
14 |
Correct |
31 ms |
7952 KB |
Output is correct |
15 |
Correct |
143 ms |
40260 KB |
Output is correct |
16 |
Correct |
134 ms |
41256 KB |
Output is correct |
17 |
Correct |
175 ms |
41748 KB |
Output is correct |
18 |
Correct |
148 ms |
41888 KB |
Output is correct |
19 |
Correct |
189 ms |
56968 KB |
Output is correct |
20 |
Correct |
205 ms |
55500 KB |
Output is correct |
21 |
Correct |
198 ms |
39928 KB |
Output is correct |
22 |
Correct |
152 ms |
39664 KB |
Output is correct |
23 |
Correct |
166 ms |
41188 KB |
Output is correct |
24 |
Correct |
162 ms |
41476 KB |
Output is correct |
25 |
Correct |
156 ms |
42640 KB |
Output is correct |
26 |
Correct |
127 ms |
41092 KB |
Output is correct |
27 |
Correct |
107 ms |
40964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
7956 KB |
Output is correct |
2 |
Correct |
45 ms |
9408 KB |
Output is correct |
3 |
Correct |
61 ms |
9396 KB |
Output is correct |
4 |
Correct |
48 ms |
9488 KB |
Output is correct |
5 |
Correct |
39 ms |
9632 KB |
Output is correct |
6 |
Correct |
41 ms |
9416 KB |
Output is correct |
7 |
Correct |
42 ms |
7972 KB |
Output is correct |
8 |
Correct |
140 ms |
38024 KB |
Output is correct |
9 |
Correct |
134 ms |
38016 KB |
Output is correct |
10 |
Correct |
30 ms |
7876 KB |
Output is correct |
11 |
Correct |
129 ms |
47536 KB |
Output is correct |
12 |
Correct |
130 ms |
47420 KB |
Output is correct |
13 |
Correct |
176 ms |
52120 KB |
Output is correct |
14 |
Correct |
31 ms |
7952 KB |
Output is correct |
15 |
Correct |
143 ms |
40260 KB |
Output is correct |
16 |
Correct |
134 ms |
41256 KB |
Output is correct |
17 |
Correct |
175 ms |
41748 KB |
Output is correct |
18 |
Correct |
148 ms |
41888 KB |
Output is correct |
19 |
Correct |
189 ms |
56968 KB |
Output is correct |
20 |
Correct |
205 ms |
55500 KB |
Output is correct |
21 |
Correct |
198 ms |
39928 KB |
Output is correct |
22 |
Correct |
152 ms |
39664 KB |
Output is correct |
23 |
Correct |
166 ms |
41188 KB |
Output is correct |
24 |
Correct |
162 ms |
41476 KB |
Output is correct |
25 |
Correct |
156 ms |
42640 KB |
Output is correct |
26 |
Correct |
127 ms |
41092 KB |
Output is correct |
27 |
Correct |
107 ms |
40964 KB |
Output is correct |
28 |
Correct |
24 ms |
7584 KB |
Output is correct |
29 |
Correct |
30 ms |
8160 KB |
Output is correct |
30 |
Correct |
43 ms |
13512 KB |
Output is correct |
31 |
Correct |
31 ms |
8096 KB |
Output is correct |
32 |
Correct |
28 ms |
8004 KB |
Output is correct |
33 |
Correct |
35 ms |
7992 KB |
Output is correct |
34 |
Correct |
24 ms |
7620 KB |
Output is correct |
35 |
Correct |
60 ms |
12036 KB |
Output is correct |
36 |
Correct |
40 ms |
11204 KB |
Output is correct |
37 |
Correct |
45 ms |
11356 KB |
Output is correct |
38 |
Correct |
24 ms |
7620 KB |
Output is correct |
39 |
Partially correct |
42 ms |
10764 KB |
Output is incorrect |
40 |
Partially correct |
43 ms |
10820 KB |
Output is incorrect |
41 |
Partially correct |
36 ms |
10392 KB |
Output is incorrect |
42 |
Partially correct |
37 ms |
10404 KB |
Output is incorrect |
43 |
Correct |
24 ms |
7568 KB |
Output is correct |
44 |
Partially correct |
41 ms |
10632 KB |
Output is incorrect |
45 |
Partially correct |
41 ms |
10640 KB |
Output is incorrect |
46 |
Partially correct |
38 ms |
10692 KB |
Output is incorrect |
47 |
Partially correct |
42 ms |
10764 KB |
Output is incorrect |
48 |
Partially correct |
38 ms |
10424 KB |
Output is incorrect |
49 |
Partially correct |
41 ms |
10784 KB |
Output is incorrect |
50 |
Partially correct |
43 ms |
11076 KB |
Output is incorrect |
51 |
Partially correct |
40 ms |
10632 KB |
Output is incorrect |
52 |
Partially correct |
45 ms |
10436 KB |
Output is incorrect |
53 |
Partially correct |
46 ms |
10124 KB |
Output is incorrect |
54 |
Partially correct |
36 ms |
10644 KB |
Output is incorrect |
55 |
Partially correct |
35 ms |
10380 KB |
Output is incorrect |
56 |
Partially correct |
44 ms |
10772 KB |
Output is incorrect |
57 |
Partially correct |
38 ms |
10508 KB |
Output is incorrect |
58 |
Partially correct |
40 ms |
10568 KB |
Output is incorrect |
59 |
Partially correct |
44 ms |
11140 KB |
Output is incorrect |