#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define pb push_back
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define F first
#define S second
const int N = 120005;
int n, pr[N], sz[N], q, dep[N], p[N][17], mn[N], vt[N];
int mx[N];
vector<array<int, 3>> qr;
vector<pair<int, int>> adj[N];
int findp(int u) {
return pr[u] = (pr[u] == u ? u : findp(pr[u]));
}
void merge(int u, int v) {
u = findp(u), v = findp(v);
if (u == v)return;
if (sz[u] < sz[v])swap(u, v);
sz[u] += sz[v];
pr[v] = u;
}
void init(int u, int par) {
for (auto &[v, t] : adj[u]) {
if (v == par)continue;
p[v][0] = u;
dep[v] = dep[u]+1;
vt[v] = t;
mn[v] = (vt[v] < vt[u] ? mn[u] : u);
mx[v] = (vt[v] > vt[u] ? mx[u] : u);
for (int i = 1; i < 17; i++) {
p[v][i] = p[p[v][i-1]][i-1];
}
init(v, u);
}
}
int lift(int u, int k) {
for (int i = 0; i < 17; i++) {
if ((k & (1 << i)))u = p[u][i];
}
return u;
}
int lca(int u, int v) {
if (dep[u] < dep[v])swap(u, v);
u = lift(u, dep[u]-dep[v]);
if (u == v)return u;
for (int i = 16; i >= 0; i--) {
if (p[u][i] != p[v][i]) {
u = p[u][i];
v = p[v][i];
}
}
return p[u][0];
}
bool sol(int u, int v) {
if (findp(u) != findp(v))return false;
int l = lca(u, v);
if (dep[mx[u]] > dep[l])return false;
if (dep[mn[v]] > dep[l])return false;
if (l == v || l == u)return true;
u = lift(u, dep[u]-dep[l]-1);
v = lift(v, dep[v]-dep[l]-1);
return (vt[v] < vt[u]);
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
int f[n];
for (int i = 0; i < n; i++) {
pr[i] = i, sz[i] = 1;
f[i] = -1;
}
int cc = 0;
q += n-1;
for (int i = 0; i < q; i++) {
char c;
cin >> c;
if (c == 'S') {
int u, v;
cin >> u >> v;
--u, --v;
qr.pb({0, u, v});
adj[u].pb({v, i});
adj[v].pb({u, i});
cc++;
} else if (c == 'Q') {
int u, v;
cin >> u >> v;
--u, --v;
qr.pb({1, u, v});
} else {
int u;
cin >> u;
--u;
qr.pb({2, u, cc});
}
}
vt[0] = q+5;
init(0, 0);
f[0] = 0;
cc = 0;
for (auto &[op, u, v] : qr) {
if (!op) {
merge(u, v);
if (u)f[u] = cc++;
else f[v] = cc++;
}
else if (op == 1) {
cout << (sol(u, v) ? "yes\n" : "no\n");
} else {
cout << (~f[u] ? cc-f[u]+1 : 1) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
6604 KB |
Output is correct |
2 |
Correct |
22 ms |
8672 KB |
Output is correct |
3 |
Correct |
28 ms |
8952 KB |
Output is correct |
4 |
Correct |
21 ms |
8920 KB |
Output is correct |
5 |
Correct |
33 ms |
9236 KB |
Output is correct |
6 |
Correct |
28 ms |
8688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
6604 KB |
Output is correct |
2 |
Correct |
22 ms |
8672 KB |
Output is correct |
3 |
Correct |
28 ms |
8952 KB |
Output is correct |
4 |
Correct |
21 ms |
8920 KB |
Output is correct |
5 |
Correct |
33 ms |
9236 KB |
Output is correct |
6 |
Correct |
28 ms |
8688 KB |
Output is correct |
7 |
Incorrect |
18 ms |
6608 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6608 KB |
Output is correct |
2 |
Correct |
101 ms |
22228 KB |
Output is correct |
3 |
Correct |
111 ms |
22420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6608 KB |
Output is correct |
2 |
Correct |
101 ms |
22228 KB |
Output is correct |
3 |
Correct |
111 ms |
22420 KB |
Output is correct |
4 |
Correct |
19 ms |
6604 KB |
Output is correct |
5 |
Correct |
75 ms |
22268 KB |
Output is correct |
6 |
Correct |
67 ms |
22784 KB |
Output is correct |
7 |
Correct |
73 ms |
22524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6608 KB |
Output is correct |
2 |
Correct |
83 ms |
30912 KB |
Output is correct |
3 |
Correct |
90 ms |
30940 KB |
Output is correct |
4 |
Correct |
81 ms |
30872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6608 KB |
Output is correct |
2 |
Correct |
83 ms |
30912 KB |
Output is correct |
3 |
Correct |
90 ms |
30940 KB |
Output is correct |
4 |
Correct |
81 ms |
30872 KB |
Output is correct |
5 |
Incorrect |
19 ms |
6604 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
6516 KB |
Output is correct |
2 |
Correct |
96 ms |
22260 KB |
Output is correct |
3 |
Correct |
104 ms |
23032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
6516 KB |
Output is correct |
2 |
Correct |
96 ms |
22260 KB |
Output is correct |
3 |
Correct |
104 ms |
23032 KB |
Output is correct |
4 |
Incorrect |
17 ms |
6604 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
6608 KB |
Output is correct |
2 |
Correct |
97 ms |
30908 KB |
Output is correct |
3 |
Correct |
107 ms |
30740 KB |
Output is correct |
4 |
Correct |
77 ms |
30712 KB |
Output is correct |
5 |
Correct |
17 ms |
6604 KB |
Output is correct |
6 |
Correct |
90 ms |
22272 KB |
Output is correct |
7 |
Correct |
101 ms |
22712 KB |
Output is correct |
8 |
Correct |
145 ms |
23236 KB |
Output is correct |
9 |
Correct |
93 ms |
23292 KB |
Output is correct |
10 |
Correct |
91 ms |
27648 KB |
Output is correct |
11 |
Correct |
101 ms |
27504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
6608 KB |
Output is correct |
2 |
Correct |
97 ms |
30908 KB |
Output is correct |
3 |
Correct |
107 ms |
30740 KB |
Output is correct |
4 |
Correct |
77 ms |
30712 KB |
Output is correct |
5 |
Correct |
17 ms |
6604 KB |
Output is correct |
6 |
Correct |
90 ms |
22272 KB |
Output is correct |
7 |
Correct |
101 ms |
22712 KB |
Output is correct |
8 |
Correct |
145 ms |
23236 KB |
Output is correct |
9 |
Correct |
93 ms |
23292 KB |
Output is correct |
10 |
Correct |
91 ms |
27648 KB |
Output is correct |
11 |
Correct |
101 ms |
27504 KB |
Output is correct |
12 |
Incorrect |
16 ms |
6608 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6608 KB |
Output is correct |
2 |
Correct |
21 ms |
8668 KB |
Output is correct |
3 |
Correct |
28 ms |
8960 KB |
Output is correct |
4 |
Correct |
21 ms |
8928 KB |
Output is correct |
5 |
Correct |
32 ms |
9232 KB |
Output is correct |
6 |
Correct |
32 ms |
8680 KB |
Output is correct |
7 |
Correct |
19 ms |
6604 KB |
Output is correct |
8 |
Correct |
79 ms |
22304 KB |
Output is correct |
9 |
Correct |
85 ms |
22260 KB |
Output is correct |
10 |
Correct |
18 ms |
6608 KB |
Output is correct |
11 |
Correct |
88 ms |
30760 KB |
Output is correct |
12 |
Correct |
94 ms |
30900 KB |
Output is correct |
13 |
Correct |
77 ms |
30720 KB |
Output is correct |
14 |
Correct |
18 ms |
6604 KB |
Output is correct |
15 |
Correct |
83 ms |
22488 KB |
Output is correct |
16 |
Correct |
80 ms |
22856 KB |
Output is correct |
17 |
Correct |
106 ms |
23292 KB |
Output is correct |
18 |
Correct |
124 ms |
23292 KB |
Output is correct |
19 |
Correct |
101 ms |
27780 KB |
Output is correct |
20 |
Correct |
118 ms |
27016 KB |
Output is correct |
21 |
Correct |
110 ms |
22344 KB |
Output is correct |
22 |
Correct |
81 ms |
22420 KB |
Output is correct |
23 |
Correct |
118 ms |
22900 KB |
Output is correct |
24 |
Correct |
112 ms |
22696 KB |
Output is correct |
25 |
Correct |
89 ms |
24436 KB |
Output is correct |
26 |
Correct |
80 ms |
22084 KB |
Output is correct |
27 |
Correct |
78 ms |
22104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
6608 KB |
Output is correct |
2 |
Correct |
21 ms |
8668 KB |
Output is correct |
3 |
Correct |
28 ms |
8960 KB |
Output is correct |
4 |
Correct |
21 ms |
8928 KB |
Output is correct |
5 |
Correct |
32 ms |
9232 KB |
Output is correct |
6 |
Correct |
32 ms |
8680 KB |
Output is correct |
7 |
Correct |
19 ms |
6604 KB |
Output is correct |
8 |
Correct |
79 ms |
22304 KB |
Output is correct |
9 |
Correct |
85 ms |
22260 KB |
Output is correct |
10 |
Correct |
18 ms |
6608 KB |
Output is correct |
11 |
Correct |
88 ms |
30760 KB |
Output is correct |
12 |
Correct |
94 ms |
30900 KB |
Output is correct |
13 |
Correct |
77 ms |
30720 KB |
Output is correct |
14 |
Correct |
18 ms |
6604 KB |
Output is correct |
15 |
Correct |
83 ms |
22488 KB |
Output is correct |
16 |
Correct |
80 ms |
22856 KB |
Output is correct |
17 |
Correct |
106 ms |
23292 KB |
Output is correct |
18 |
Correct |
124 ms |
23292 KB |
Output is correct |
19 |
Correct |
101 ms |
27780 KB |
Output is correct |
20 |
Correct |
118 ms |
27016 KB |
Output is correct |
21 |
Correct |
110 ms |
22344 KB |
Output is correct |
22 |
Correct |
81 ms |
22420 KB |
Output is correct |
23 |
Correct |
118 ms |
22900 KB |
Output is correct |
24 |
Correct |
112 ms |
22696 KB |
Output is correct |
25 |
Correct |
89 ms |
24436 KB |
Output is correct |
26 |
Correct |
80 ms |
22084 KB |
Output is correct |
27 |
Correct |
78 ms |
22104 KB |
Output is correct |
28 |
Incorrect |
18 ms |
6604 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |