// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define sz(x) int(x.size())
#define f first
#define s second
#define mp make_pair
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using pi = pair<int, int>;
template<class T> using V = vector<T>;
using vi = V<int>;
using vpi = V<pi>;
const int INF = 1e9;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, K; cin >> N >> K;
V<vpi> adj(N); V<vpi> Q(N);
for(int i = 0; i < (N + K - 1); i++) {
char c; cin >> c;
if (c == 'S') {
int x, y; cin >> x >> y; --x, --y;
adj[x].pb(mp(y, i));
adj[y].pb(mp(x, i));
}
if (c == 'C') {
int x; cin >> x; --x;
Q[x].pb(mp(i, -1)); // (t, operation type)
}
if (c == 'Q') {
int u, x; cin >> u >> x; --u, --x;
Q[x].pb(mp(i, u)); // (t, operation type)
}
}
vi T(N, -1);
function <void(int, int)> gen = [&](int u, int p) {
for(auto& e : adj[u]) if (e.f != p) {
T[e.f] = e.s; gen(e.f, u);
}
};
gen(0, -1);
V<iset<int>> S(1); int cur = 0;
V<array<int, 3>> his;
auto rollback = [&]() {
auto [u, v, swp] = his.back(); his.pop_back();
// cout << u << " <R> " << v << endl;
for(auto& p : S[v]) S[u].erase(p);
if (swp) S[u].swap(S[v]);
};
auto merge = [&](int u, int v) {
bool swp = 0;
// cout << u << " <U> " << v << endl;
if (sz(S[u]) < sz(S[v])) S[u].swap(S[v]), swp = 1;
for(auto& p : S[v]) S[u].insert(p);
his.pb({u, v, swp});
};
V<string> ans(N + K - 1, "NK");
function<void(int, int, int)> dfs = [&](int u, int t, int id) {
vpi chd; sort(begin(adj[u]), end(adj[u]), [&](pi x, pi y) {
return x.s > y.s;
});
S.emplace_back(); int nid = ++cur; bool done = 0;
for(auto& e : adj[u]) if (e.s != t) {
int v, tv; tie(v, tv) = e;
swap(T[u], T[v]);
if (t > tv) {
if (!done) merge(id, nid), done = 1;
S[id].insert(tv);
int bef = sz(his);
dfs(v, tv, id);
int aft = sz(his);
chd.pb(mp(tv, aft - bef));
} else {
S[nid].insert(tv);
dfs(v, tv, nid);
// cout << v << " " << nid << " => ";
// for(auto& x : S[nid]) cout << x << " ";
// cout << endl;
}
swap(T[u], T[v]);
}
if (!done) merge(id, nid), done = 1;
// cout << u << " " << id << " => ";
// for(auto& x : S[id]) cout << x << " ";
// cout << endl;
for(auto& p : Q[u]) {
if (p.s == -1) {
// cout << p.f << endl;
// C -> count entries with t <= T
ans[p.f] = to_string(S[id].order_of_key(p.f + 1) + 1);
} else {
// cout << p.f << " " << p.s << " ==> " << T[p.s] << endl;
// Q -> check if p.s is connect at tie p.f
if (p.s == u) ans[p.f] = (T[p.s] <= p.f ? "yes" : "no");
else ans[p.f] = (T[p.s] <= p.f && S[id].find(T[p.s]) != S[id].end() ? "yes" : "no");
}
}
// cout << "U: " << id << " " << nid << endl;
reverse(begin(chd), end(chd));
for(auto& p : chd) {
while(p.s--) rollback();
S[id].erase(p.f);
}
};
dfs(0, INF, 0);
for(int i = 0; i < (N + K - 1); i++) {
if (ans[i] == "NK") continue;
cout << ans[i] << nl;
}
exit(0-0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
5980 KB |
Output is correct |
2 |
Correct |
31 ms |
6852 KB |
Output is correct |
3 |
Correct |
32 ms |
8252 KB |
Output is correct |
4 |
Correct |
31 ms |
8496 KB |
Output is correct |
5 |
Correct |
33 ms |
9220 KB |
Output is correct |
6 |
Correct |
35 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
5980 KB |
Output is correct |
2 |
Correct |
31 ms |
6852 KB |
Output is correct |
3 |
Correct |
32 ms |
8252 KB |
Output is correct |
4 |
Correct |
31 ms |
8496 KB |
Output is correct |
5 |
Correct |
33 ms |
9220 KB |
Output is correct |
6 |
Correct |
35 ms |
8532 KB |
Output is correct |
7 |
Correct |
28 ms |
7004 KB |
Output is correct |
8 |
Correct |
30 ms |
8020 KB |
Output is correct |
9 |
Correct |
29 ms |
8020 KB |
Output is correct |
10 |
Correct |
30 ms |
8108 KB |
Output is correct |
11 |
Correct |
29 ms |
9044 KB |
Output is correct |
12 |
Correct |
32 ms |
8020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6224 KB |
Output is correct |
2 |
Correct |
238 ms |
39612 KB |
Output is correct |
3 |
Correct |
235 ms |
39940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6224 KB |
Output is correct |
2 |
Correct |
238 ms |
39612 KB |
Output is correct |
3 |
Correct |
235 ms |
39940 KB |
Output is correct |
4 |
Correct |
21 ms |
5980 KB |
Output is correct |
5 |
Correct |
223 ms |
39620 KB |
Output is correct |
6 |
Correct |
155 ms |
38708 KB |
Output is correct |
7 |
Correct |
163 ms |
39048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5980 KB |
Output is correct |
2 |
Correct |
205 ms |
68532 KB |
Output is correct |
3 |
Correct |
187 ms |
68536 KB |
Output is correct |
4 |
Correct |
216 ms |
73904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5980 KB |
Output is correct |
2 |
Correct |
205 ms |
68532 KB |
Output is correct |
3 |
Correct |
187 ms |
68536 KB |
Output is correct |
4 |
Correct |
216 ms |
73904 KB |
Output is correct |
5 |
Correct |
22 ms |
6740 KB |
Output is correct |
6 |
Correct |
237 ms |
68284 KB |
Output is correct |
7 |
Correct |
194 ms |
70076 KB |
Output is correct |
8 |
Correct |
192 ms |
68048 KB |
Output is correct |
9 |
Correct |
188 ms |
68688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5980 KB |
Output is correct |
2 |
Correct |
278 ms |
75112 KB |
Output is correct |
3 |
Correct |
152 ms |
37960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5980 KB |
Output is correct |
2 |
Correct |
278 ms |
75112 KB |
Output is correct |
3 |
Correct |
152 ms |
37960 KB |
Output is correct |
4 |
Correct |
22 ms |
6744 KB |
Output is correct |
5 |
Correct |
275 ms |
73428 KB |
Output is correct |
6 |
Correct |
148 ms |
39364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5980 KB |
Output is correct |
2 |
Correct |
222 ms |
68432 KB |
Output is correct |
3 |
Correct |
188 ms |
68536 KB |
Output is correct |
4 |
Correct |
217 ms |
73660 KB |
Output is correct |
5 |
Correct |
23 ms |
6996 KB |
Output is correct |
6 |
Correct |
261 ms |
74464 KB |
Output is correct |
7 |
Correct |
144 ms |
39620 KB |
Output is correct |
8 |
Correct |
174 ms |
40724 KB |
Output is correct |
9 |
Correct |
182 ms |
39360 KB |
Output is correct |
10 |
Correct |
264 ms |
49676 KB |
Output is correct |
11 |
Correct |
201 ms |
45608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5980 KB |
Output is correct |
2 |
Correct |
222 ms |
68432 KB |
Output is correct |
3 |
Correct |
188 ms |
68536 KB |
Output is correct |
4 |
Correct |
217 ms |
73660 KB |
Output is correct |
5 |
Correct |
23 ms |
6996 KB |
Output is correct |
6 |
Correct |
261 ms |
74464 KB |
Output is correct |
7 |
Correct |
144 ms |
39620 KB |
Output is correct |
8 |
Correct |
174 ms |
40724 KB |
Output is correct |
9 |
Correct |
182 ms |
39360 KB |
Output is correct |
10 |
Correct |
264 ms |
49676 KB |
Output is correct |
11 |
Correct |
201 ms |
45608 KB |
Output is correct |
12 |
Correct |
21 ms |
6740 KB |
Output is correct |
13 |
Correct |
186 ms |
68284 KB |
Output is correct |
14 |
Correct |
199 ms |
69800 KB |
Output is correct |
15 |
Correct |
231 ms |
68000 KB |
Output is correct |
16 |
Correct |
171 ms |
68064 KB |
Output is correct |
17 |
Correct |
21 ms |
6948 KB |
Output is correct |
18 |
Correct |
269 ms |
73924 KB |
Output is correct |
19 |
Correct |
148 ms |
38340 KB |
Output is correct |
20 |
Correct |
196 ms |
40392 KB |
Output is correct |
21 |
Correct |
182 ms |
39096 KB |
Output is correct |
22 |
Correct |
192 ms |
47044 KB |
Output is correct |
23 |
Correct |
238 ms |
54464 KB |
Output is correct |
24 |
Correct |
254 ms |
55360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
5980 KB |
Output is correct |
2 |
Correct |
31 ms |
6928 KB |
Output is correct |
3 |
Correct |
32 ms |
8144 KB |
Output is correct |
4 |
Correct |
31 ms |
8712 KB |
Output is correct |
5 |
Correct |
31 ms |
9300 KB |
Output is correct |
6 |
Correct |
35 ms |
8400 KB |
Output is correct |
7 |
Correct |
22 ms |
6996 KB |
Output is correct |
8 |
Correct |
240 ms |
43192 KB |
Output is correct |
9 |
Correct |
236 ms |
41664 KB |
Output is correct |
10 |
Correct |
22 ms |
6968 KB |
Output is correct |
11 |
Correct |
196 ms |
68540 KB |
Output is correct |
12 |
Correct |
247 ms |
68464 KB |
Output is correct |
13 |
Correct |
213 ms |
73660 KB |
Output is correct |
14 |
Correct |
22 ms |
6996 KB |
Output is correct |
15 |
Correct |
274 ms |
75020 KB |
Output is correct |
16 |
Correct |
154 ms |
38852 KB |
Output is correct |
17 |
Correct |
172 ms |
39156 KB |
Output is correct |
18 |
Correct |
198 ms |
38476 KB |
Output is correct |
19 |
Correct |
202 ms |
50172 KB |
Output is correct |
20 |
Correct |
204 ms |
45508 KB |
Output is correct |
21 |
Correct |
222 ms |
42292 KB |
Output is correct |
22 |
Correct |
222 ms |
41488 KB |
Output is correct |
23 |
Correct |
204 ms |
39108 KB |
Output is correct |
24 |
Correct |
205 ms |
40528 KB |
Output is correct |
25 |
Correct |
236 ms |
49080 KB |
Output is correct |
26 |
Correct |
172 ms |
36292 KB |
Output is correct |
27 |
Correct |
138 ms |
34316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
5980 KB |
Output is correct |
2 |
Correct |
31 ms |
6928 KB |
Output is correct |
3 |
Correct |
32 ms |
8144 KB |
Output is correct |
4 |
Correct |
31 ms |
8712 KB |
Output is correct |
5 |
Correct |
31 ms |
9300 KB |
Output is correct |
6 |
Correct |
35 ms |
8400 KB |
Output is correct |
7 |
Correct |
22 ms |
6996 KB |
Output is correct |
8 |
Correct |
240 ms |
43192 KB |
Output is correct |
9 |
Correct |
236 ms |
41664 KB |
Output is correct |
10 |
Correct |
22 ms |
6968 KB |
Output is correct |
11 |
Correct |
196 ms |
68540 KB |
Output is correct |
12 |
Correct |
247 ms |
68464 KB |
Output is correct |
13 |
Correct |
213 ms |
73660 KB |
Output is correct |
14 |
Correct |
22 ms |
6996 KB |
Output is correct |
15 |
Correct |
274 ms |
75020 KB |
Output is correct |
16 |
Correct |
154 ms |
38852 KB |
Output is correct |
17 |
Correct |
172 ms |
39156 KB |
Output is correct |
18 |
Correct |
198 ms |
38476 KB |
Output is correct |
19 |
Correct |
202 ms |
50172 KB |
Output is correct |
20 |
Correct |
204 ms |
45508 KB |
Output is correct |
21 |
Correct |
222 ms |
42292 KB |
Output is correct |
22 |
Correct |
222 ms |
41488 KB |
Output is correct |
23 |
Correct |
204 ms |
39108 KB |
Output is correct |
24 |
Correct |
205 ms |
40528 KB |
Output is correct |
25 |
Correct |
236 ms |
49080 KB |
Output is correct |
26 |
Correct |
172 ms |
36292 KB |
Output is correct |
27 |
Correct |
138 ms |
34316 KB |
Output is correct |
28 |
Correct |
23 ms |
6808 KB |
Output is correct |
29 |
Correct |
32 ms |
8020 KB |
Output is correct |
30 |
Correct |
32 ms |
7888 KB |
Output is correct |
31 |
Correct |
30 ms |
8276 KB |
Output is correct |
32 |
Correct |
30 ms |
8868 KB |
Output is correct |
33 |
Correct |
31 ms |
8020 KB |
Output is correct |
34 |
Correct |
22 ms |
6996 KB |
Output is correct |
35 |
Correct |
224 ms |
41744 KB |
Output is correct |
36 |
Correct |
162 ms |
40760 KB |
Output is correct |
37 |
Correct |
160 ms |
40972 KB |
Output is correct |
38 |
Correct |
22 ms |
6740 KB |
Output is correct |
39 |
Correct |
189 ms |
68164 KB |
Output is correct |
40 |
Correct |
204 ms |
69772 KB |
Output is correct |
41 |
Correct |
206 ms |
68032 KB |
Output is correct |
42 |
Correct |
169 ms |
68028 KB |
Output is correct |
43 |
Correct |
22 ms |
6740 KB |
Output is correct |
44 |
Correct |
300 ms |
73544 KB |
Output is correct |
45 |
Correct |
148 ms |
38336 KB |
Output is correct |
46 |
Correct |
166 ms |
38412 KB |
Output is correct |
47 |
Correct |
184 ms |
38852 KB |
Output is correct |
48 |
Correct |
219 ms |
46948 KB |
Output is correct |
49 |
Correct |
265 ms |
54804 KB |
Output is correct |
50 |
Correct |
268 ms |
54900 KB |
Output is correct |
51 |
Correct |
169 ms |
42660 KB |
Output is correct |
52 |
Correct |
165 ms |
40900 KB |
Output is correct |
53 |
Correct |
146 ms |
40120 KB |
Output is correct |
54 |
Correct |
153 ms |
40696 KB |
Output is correct |
55 |
Correct |
132 ms |
36308 KB |
Output is correct |
56 |
Correct |
180 ms |
38340 KB |
Output is correct |
57 |
Correct |
155 ms |
45484 KB |
Output is correct |
58 |
Correct |
182 ms |
36804 KB |
Output is correct |
59 |
Correct |
144 ms |
34496 KB |
Output is correct |