#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen("servers.inp", "r")){
freopen("servers.inp", "r", stdin);
freopen("servers.out", "w", stdout);
}
#else
auto start = chrono::steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
#define for_cen(v, w, u) for (auto [v, w] : adj[u]) if (!mark[v])
const int N = 1.2e5 + 5, Q = N << 1;
vector<pair<int, int>> query_q[N];
vector<int> query_c[N];
vector<pair<int, int>> adj[N];
int n, q, siz[N], mark[N];
int find_sz(int u, int p) {
siz[u] = 1;
for_cen(v, w, u) if (v != p)
siz[u] += find_sz(v, u);
return siz[u];
}
int find_cen(int u, int p, const int &tot) {
for_cen(v, w, u) if (v != p)
if (siz[v] > tot / 2) return find_cen(v, u, tot);
return u;
}
struct fenwick_tree {
int bit[Q], ver[Q], vercnt;
inline void clear() { vercnt ++; }
inline void add(int p, int v) {
for (; p < Q; p += p & -p) {
if (ver[p] ^ vercnt)
ver[p] = vercnt, bit[p] = 0;
bit[p] += v;
}
}
inline int query(int p) {
int r = 0;
for (; p > 0; p -= p & -p) {
if (ver[p] ^ vercnt)
ver[p] = vercnt, bit[p] = 0;
r += bit[p];
}
return r;
}
} bit;
vector<int> inQ, outQ, allinQ;
int res[Q], curin[N], curout[N];
void dfs_in(int u, int p, int cur) {
inQ.push_back(u);
for_cen(v, w, u) if (v != p) {
if (w < cur)
dfs_in(v, u, w);
}
}
void dfs_out(int u, int p, int cur) {
outQ.push_back(u);
bit.add(cur, 1);
curout[u] = cur;
for_cen(v, w, u) if (v != p) {
if (w > cur)
dfs_out(v, u, w);
}
}
void centroid_decomp(int r) {
int tot = find_sz(r, -1);
r = find_cen(r, -1, tot);
sort(adj[r].begin(), adj[r].end(), [&](const pair<int, int> &a, const pair<int, int> &b){
return a.second > b.second;
});
bit.clear();
curin[r] = 0;
for_cen(v, w, r) {
inQ.clear();
outQ.clear();
dfs_in(v, r, w);
for (auto u : inQ) {
for (auto i : query_c[u]) if (i >= w)
res[i] += 1 + bit.query(i);
curin[u] = w;
}
dfs_out(v, r, w);
}
for (auto i : query_c[r])
res[i] += 1 + bit.query(i);
reverse(adj[r].begin(), adj[r].end());
bit.clear();
allinQ.push_back(r);
for_cen(v, w, r) {
inQ.clear();
outQ.clear();
dfs_in(v, r, w);
for (auto u : inQ) {
allinQ.push_back(u);
}
dfs_out(v, r, w);
for (auto u : outQ) {
for (auto [i, v] : query_q[u])
if (curin[v] != -1 && curin[v] < w && i > curout[u]) {
res[i] = 1;
}
}
}
for (auto [i, v] : query_q[r])
if (curin[v] != -1 && i > curin[v]) {
res[i] = 1;
}
for (auto u : allinQ)
curin[u] = -1;
allinQ.clear();
mark[r] = 1;
for_cen(v, w, r)
centroid_decomp(v);
}
char t[Q];
void Hollwo_Pelw(){
cin >> n >> q, q += n - 1;
for (int i = 1, u, v; i <= q; i++) {
cin >> t[i];
if (t[i] == 'S') {
cin >> u >> v;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
if (t[i] == 'Q') {
cin >> u >> v;
query_q[u].emplace_back(i, v);
}
if (t[i] == 'C') {
cin >> u;
query_c[u].push_back(i);
}
}
memset(curin, -1, sizeof curin);
centroid_decomp(1);
for (int i = 1; i <= q; i++) {
if (t[i] == 'Q')
cout << (res[i] ? "yes\n" : "no\n");
if (t[i] == 'C')
cout << res[i] << '\n';
}
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:15:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen("servers.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("servers.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12336 KB |
Output is correct |
2 |
Correct |
39 ms |
12724 KB |
Output is correct |
3 |
Correct |
32 ms |
12872 KB |
Output is correct |
4 |
Correct |
37 ms |
12868 KB |
Output is correct |
5 |
Correct |
39 ms |
12228 KB |
Output is correct |
6 |
Correct |
32 ms |
12420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12336 KB |
Output is correct |
2 |
Correct |
39 ms |
12724 KB |
Output is correct |
3 |
Correct |
32 ms |
12872 KB |
Output is correct |
4 |
Correct |
37 ms |
12868 KB |
Output is correct |
5 |
Correct |
39 ms |
12228 KB |
Output is correct |
6 |
Correct |
32 ms |
12420 KB |
Output is correct |
7 |
Correct |
24 ms |
12512 KB |
Output is correct |
8 |
Correct |
41 ms |
12612 KB |
Output is correct |
9 |
Correct |
35 ms |
12564 KB |
Output is correct |
10 |
Correct |
44 ms |
12656 KB |
Output is correct |
11 |
Correct |
46 ms |
12816 KB |
Output is correct |
12 |
Correct |
36 ms |
12648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
12492 KB |
Output is correct |
2 |
Correct |
152 ms |
21952 KB |
Output is correct |
3 |
Correct |
148 ms |
21932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
12492 KB |
Output is correct |
2 |
Correct |
152 ms |
21952 KB |
Output is correct |
3 |
Correct |
148 ms |
21932 KB |
Output is correct |
4 |
Correct |
34 ms |
12520 KB |
Output is correct |
5 |
Correct |
198 ms |
21856 KB |
Output is correct |
6 |
Correct |
101 ms |
20504 KB |
Output is correct |
7 |
Correct |
133 ms |
20372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
12552 KB |
Output is correct |
2 |
Correct |
259 ms |
27716 KB |
Output is correct |
3 |
Correct |
231 ms |
27716 KB |
Output is correct |
4 |
Correct |
227 ms |
28348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
12552 KB |
Output is correct |
2 |
Correct |
259 ms |
27716 KB |
Output is correct |
3 |
Correct |
231 ms |
27716 KB |
Output is correct |
4 |
Correct |
227 ms |
28348 KB |
Output is correct |
5 |
Correct |
25 ms |
12492 KB |
Output is correct |
6 |
Correct |
235 ms |
28192 KB |
Output is correct |
7 |
Correct |
261 ms |
28992 KB |
Output is correct |
8 |
Correct |
230 ms |
27588 KB |
Output is correct |
9 |
Correct |
235 ms |
27600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
12364 KB |
Output is correct |
2 |
Correct |
234 ms |
21504 KB |
Output is correct |
3 |
Correct |
191 ms |
21048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
12364 KB |
Output is correct |
2 |
Correct |
234 ms |
21504 KB |
Output is correct |
3 |
Correct |
191 ms |
21048 KB |
Output is correct |
4 |
Correct |
26 ms |
12508 KB |
Output is correct |
5 |
Correct |
295 ms |
22508 KB |
Output is correct |
6 |
Correct |
189 ms |
21316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12484 KB |
Output is correct |
2 |
Correct |
320 ms |
27784 KB |
Output is correct |
3 |
Correct |
218 ms |
27716 KB |
Output is correct |
4 |
Correct |
246 ms |
28400 KB |
Output is correct |
5 |
Correct |
25 ms |
12352 KB |
Output is correct |
6 |
Correct |
195 ms |
21460 KB |
Output is correct |
7 |
Correct |
191 ms |
20992 KB |
Output is correct |
8 |
Correct |
255 ms |
20844 KB |
Output is correct |
9 |
Correct |
207 ms |
21872 KB |
Output is correct |
10 |
Correct |
343 ms |
25536 KB |
Output is correct |
11 |
Correct |
252 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12484 KB |
Output is correct |
2 |
Correct |
320 ms |
27784 KB |
Output is correct |
3 |
Correct |
218 ms |
27716 KB |
Output is correct |
4 |
Correct |
246 ms |
28400 KB |
Output is correct |
5 |
Correct |
25 ms |
12352 KB |
Output is correct |
6 |
Correct |
195 ms |
21460 KB |
Output is correct |
7 |
Correct |
191 ms |
20992 KB |
Output is correct |
8 |
Correct |
255 ms |
20844 KB |
Output is correct |
9 |
Correct |
207 ms |
21872 KB |
Output is correct |
10 |
Correct |
343 ms |
25536 KB |
Output is correct |
11 |
Correct |
252 ms |
23800 KB |
Output is correct |
12 |
Correct |
24 ms |
12528 KB |
Output is correct |
13 |
Correct |
252 ms |
28160 KB |
Output is correct |
14 |
Correct |
229 ms |
28940 KB |
Output is correct |
15 |
Correct |
254 ms |
27680 KB |
Output is correct |
16 |
Correct |
243 ms |
27684 KB |
Output is correct |
17 |
Correct |
24 ms |
12492 KB |
Output is correct |
18 |
Correct |
247 ms |
22708 KB |
Output is correct |
19 |
Correct |
213 ms |
21304 KB |
Output is correct |
20 |
Correct |
223 ms |
22448 KB |
Output is correct |
21 |
Correct |
220 ms |
22368 KB |
Output is correct |
22 |
Correct |
303 ms |
24704 KB |
Output is correct |
23 |
Correct |
544 ms |
25060 KB |
Output is correct |
24 |
Correct |
427 ms |
24380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
12360 KB |
Output is correct |
2 |
Correct |
36 ms |
12788 KB |
Output is correct |
3 |
Correct |
33 ms |
12824 KB |
Output is correct |
4 |
Correct |
35 ms |
12776 KB |
Output is correct |
5 |
Correct |
33 ms |
12228 KB |
Output is correct |
6 |
Correct |
34 ms |
12528 KB |
Output is correct |
7 |
Correct |
23 ms |
12524 KB |
Output is correct |
8 |
Correct |
145 ms |
21940 KB |
Output is correct |
9 |
Correct |
143 ms |
21944 KB |
Output is correct |
10 |
Correct |
23 ms |
12380 KB |
Output is correct |
11 |
Correct |
240 ms |
27692 KB |
Output is correct |
12 |
Correct |
229 ms |
27704 KB |
Output is correct |
13 |
Correct |
208 ms |
28436 KB |
Output is correct |
14 |
Correct |
29 ms |
12504 KB |
Output is correct |
15 |
Correct |
206 ms |
21460 KB |
Output is correct |
16 |
Correct |
204 ms |
21000 KB |
Output is correct |
17 |
Correct |
201 ms |
20840 KB |
Output is correct |
18 |
Correct |
209 ms |
21888 KB |
Output is correct |
19 |
Correct |
282 ms |
25492 KB |
Output is correct |
20 |
Correct |
292 ms |
23780 KB |
Output is correct |
21 |
Correct |
161 ms |
22096 KB |
Output is correct |
22 |
Correct |
178 ms |
21712 KB |
Output is correct |
23 |
Correct |
178 ms |
21508 KB |
Output is correct |
24 |
Correct |
169 ms |
21512 KB |
Output is correct |
25 |
Correct |
260 ms |
25120 KB |
Output is correct |
26 |
Correct |
252 ms |
20476 KB |
Output is correct |
27 |
Correct |
282 ms |
20496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
12360 KB |
Output is correct |
2 |
Correct |
36 ms |
12788 KB |
Output is correct |
3 |
Correct |
33 ms |
12824 KB |
Output is correct |
4 |
Correct |
35 ms |
12776 KB |
Output is correct |
5 |
Correct |
33 ms |
12228 KB |
Output is correct |
6 |
Correct |
34 ms |
12528 KB |
Output is correct |
7 |
Correct |
23 ms |
12524 KB |
Output is correct |
8 |
Correct |
145 ms |
21940 KB |
Output is correct |
9 |
Correct |
143 ms |
21944 KB |
Output is correct |
10 |
Correct |
23 ms |
12380 KB |
Output is correct |
11 |
Correct |
240 ms |
27692 KB |
Output is correct |
12 |
Correct |
229 ms |
27704 KB |
Output is correct |
13 |
Correct |
208 ms |
28436 KB |
Output is correct |
14 |
Correct |
29 ms |
12504 KB |
Output is correct |
15 |
Correct |
206 ms |
21460 KB |
Output is correct |
16 |
Correct |
204 ms |
21000 KB |
Output is correct |
17 |
Correct |
201 ms |
20840 KB |
Output is correct |
18 |
Correct |
209 ms |
21888 KB |
Output is correct |
19 |
Correct |
282 ms |
25492 KB |
Output is correct |
20 |
Correct |
292 ms |
23780 KB |
Output is correct |
21 |
Correct |
161 ms |
22096 KB |
Output is correct |
22 |
Correct |
178 ms |
21712 KB |
Output is correct |
23 |
Correct |
178 ms |
21508 KB |
Output is correct |
24 |
Correct |
169 ms |
21512 KB |
Output is correct |
25 |
Correct |
260 ms |
25120 KB |
Output is correct |
26 |
Correct |
252 ms |
20476 KB |
Output is correct |
27 |
Correct |
282 ms |
20496 KB |
Output is correct |
28 |
Correct |
25 ms |
12492 KB |
Output is correct |
29 |
Correct |
38 ms |
12604 KB |
Output is correct |
30 |
Correct |
35 ms |
12548 KB |
Output is correct |
31 |
Correct |
41 ms |
12632 KB |
Output is correct |
32 |
Correct |
45 ms |
12912 KB |
Output is correct |
33 |
Correct |
34 ms |
12636 KB |
Output is correct |
34 |
Correct |
23 ms |
12540 KB |
Output is correct |
35 |
Correct |
138 ms |
21820 KB |
Output is correct |
36 |
Correct |
139 ms |
20540 KB |
Output is correct |
37 |
Correct |
101 ms |
20408 KB |
Output is correct |
38 |
Correct |
25 ms |
12492 KB |
Output is correct |
39 |
Correct |
232 ms |
28168 KB |
Output is correct |
40 |
Correct |
258 ms |
28936 KB |
Output is correct |
41 |
Correct |
226 ms |
27644 KB |
Output is correct |
42 |
Correct |
261 ms |
27572 KB |
Output is correct |
43 |
Correct |
24 ms |
12492 KB |
Output is correct |
44 |
Correct |
235 ms |
22472 KB |
Output is correct |
45 |
Correct |
220 ms |
21272 KB |
Output is correct |
46 |
Correct |
211 ms |
22448 KB |
Output is correct |
47 |
Correct |
247 ms |
22384 KB |
Output is correct |
48 |
Correct |
293 ms |
24700 KB |
Output is correct |
49 |
Correct |
479 ms |
25004 KB |
Output is correct |
50 |
Correct |
471 ms |
24392 KB |
Output is correct |
51 |
Correct |
119 ms |
20492 KB |
Output is correct |
52 |
Correct |
108 ms |
20548 KB |
Output is correct |
53 |
Correct |
105 ms |
20316 KB |
Output is correct |
54 |
Correct |
104 ms |
20504 KB |
Output is correct |
55 |
Correct |
104 ms |
20908 KB |
Output is correct |
56 |
Correct |
201 ms |
20896 KB |
Output is correct |
57 |
Correct |
206 ms |
23380 KB |
Output is correct |
58 |
Correct |
324 ms |
21356 KB |
Output is correct |
59 |
Correct |
269 ms |
20680 KB |
Output is correct |