#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 120000 + 10;
bool dd[N];
int sz[N], ans[N << 1];
vector<pair<int, int>> adj[N], query1[N];
vector<int> query2[N];
int n, k, cnt[N], type[N << 1];
int calc(int u, int par) {
sz[u] = 1;
for(auto &[v, w] : adj[u]) if (!dd[v] && v != par) {
sz[u] += calc(v, u);
}
return sz[u];
}
int getcentr(int u, int par, int cnt) {
for(auto &[v, w] : adj[u]) if (!dd[v] && v != par && sz[v] * 2 > cnt)
return getcentr(v, u, cnt);
return u;
}
int bit[N << 1];
void up(int pos, int val) {
while (pos <= k) {
bit[pos] += val;
pos += pos & -pos;
}
}
int get(int pos) {
int ret = 0;
while (pos) {
ret += bit[pos];
pos -= pos & -pos;
}
return ret;
}
void dfs1(int u, int pre) {
up(pre, 1);
cnt[u] = pre;
for(auto &[v, w] : adj[u]) if (!dd[v] && w > pre) {
dfs1(v, w);
}
}
void dfs2(int u, int pre) {
for(int &idx : query2[u]) {
ans[idx] += get(idx);
}
for(auto &[a, idx] : query1[u]) {
ans[idx] |= (cnt[a] > 0 && cnt[a] < idx);
}
for(auto &[v, w] : adj[u]) if (!dd[v] && w < pre) {
dfs2(v, w);
}
}
void dfs3(int u, int pre) {
up(pre, -1);
cnt[u] = 0;
for(auto &[v, w] : adj[u]) if (!dd[v] && w > pre) {
dfs3(v, w);
}
}
void centroid(int u) {
int node = getcentr(u, u, calc(u, u));
dd[node] = 1;
vector<pair<int, int> > lst;
for(auto &[v, w] : adj[node]) if (!dd[v]) lst.emplace_back(-w, v);
sort(lst.begin(), lst.end());
for(auto &[w, v] : lst) {
w = -w;
cnt[node] = w;
up(w, 1);
dfs2(v, w);
up(w, -1);
cnt[node] = 0;
dfs1(v, w);
}
for(int &idx : query2[node]) {
ans[idx] += get(idx);
}
for(auto &[a, idx] : query1[node]) {
ans[idx] |= (cnt[a] > 0 && cnt[a] < idx);
}
for(auto &[w, v] : lst) {
dfs3(v, w);
}
for(auto &[v, w] : adj[node]) if (!dd[v]) centroid(v);
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> k;
k = n + k - 1;
for(int i = 1; i <= k; i++) {
char c; cin >> c;
if (c == 'S') {
int a, b; cin >> a >> b;
adj[a].emplace_back(b, i);
adj[b].emplace_back(a, i);
}
if (c == 'Q') {
int a, d; cin >> a >> d;
if (a == d) ans[i] = 1;
query1[d].emplace_back(a, i);
type[i] = 1;
}
if (c == 'C') {
int d; cin >> d;
query2[d].push_back(i);
type[i] = 2;
ans[i] = 1;
}
}
centroid(1);
for(int i = 1; i <= k; i++) {
if (type[i] == 1) cout << (ans[i] ? "yes\n" : "no\n");
else if (type[i] == 2) cout << ans[i] << endl;
}
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:106:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen ("task.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:107:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen ("task.out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:110:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:111:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
13004 KB |
Output is correct |
2 |
Correct |
40 ms |
13252 KB |
Output is correct |
3 |
Correct |
33 ms |
13136 KB |
Output is correct |
4 |
Correct |
47 ms |
13320 KB |
Output is correct |
5 |
Correct |
38 ms |
13212 KB |
Output is correct |
6 |
Correct |
37 ms |
13072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
13004 KB |
Output is correct |
2 |
Correct |
40 ms |
13252 KB |
Output is correct |
3 |
Correct |
33 ms |
13136 KB |
Output is correct |
4 |
Correct |
47 ms |
13320 KB |
Output is correct |
5 |
Correct |
38 ms |
13212 KB |
Output is correct |
6 |
Correct |
37 ms |
13072 KB |
Output is correct |
7 |
Correct |
25 ms |
12828 KB |
Output is correct |
8 |
Correct |
39 ms |
12880 KB |
Output is correct |
9 |
Correct |
45 ms |
12720 KB |
Output is correct |
10 |
Correct |
42 ms |
12996 KB |
Output is correct |
11 |
Correct |
39 ms |
12680 KB |
Output is correct |
12 |
Correct |
41 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
12984 KB |
Output is correct |
2 |
Correct |
180 ms |
21508 KB |
Output is correct |
3 |
Correct |
139 ms |
21684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
12984 KB |
Output is correct |
2 |
Correct |
180 ms |
21508 KB |
Output is correct |
3 |
Correct |
139 ms |
21684 KB |
Output is correct |
4 |
Correct |
29 ms |
12860 KB |
Output is correct |
5 |
Correct |
146 ms |
21368 KB |
Output is correct |
6 |
Correct |
94 ms |
19908 KB |
Output is correct |
7 |
Correct |
98 ms |
19844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
13024 KB |
Output is correct |
2 |
Correct |
239 ms |
27700 KB |
Output is correct |
3 |
Correct |
240 ms |
30044 KB |
Output is correct |
4 |
Correct |
201 ms |
29764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
13024 KB |
Output is correct |
2 |
Correct |
239 ms |
27700 KB |
Output is correct |
3 |
Correct |
240 ms |
30044 KB |
Output is correct |
4 |
Correct |
201 ms |
29764 KB |
Output is correct |
5 |
Correct |
28 ms |
12896 KB |
Output is correct |
6 |
Correct |
239 ms |
30028 KB |
Output is correct |
7 |
Correct |
222 ms |
30160 KB |
Output is correct |
8 |
Correct |
243 ms |
29412 KB |
Output is correct |
9 |
Correct |
227 ms |
29272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
13004 KB |
Output is correct |
2 |
Correct |
218 ms |
20768 KB |
Output is correct |
3 |
Correct |
230 ms |
20788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
13004 KB |
Output is correct |
2 |
Correct |
218 ms |
20768 KB |
Output is correct |
3 |
Correct |
230 ms |
20788 KB |
Output is correct |
4 |
Correct |
27 ms |
12864 KB |
Output is correct |
5 |
Correct |
225 ms |
21472 KB |
Output is correct |
6 |
Correct |
223 ms |
21056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
12928 KB |
Output is correct |
2 |
Correct |
231 ms |
27652 KB |
Output is correct |
3 |
Correct |
219 ms |
29992 KB |
Output is correct |
4 |
Correct |
208 ms |
29832 KB |
Output is correct |
5 |
Correct |
25 ms |
12952 KB |
Output is correct |
6 |
Correct |
184 ms |
23168 KB |
Output is correct |
7 |
Correct |
205 ms |
23104 KB |
Output is correct |
8 |
Correct |
231 ms |
23812 KB |
Output is correct |
9 |
Correct |
192 ms |
24096 KB |
Output is correct |
10 |
Correct |
310 ms |
27164 KB |
Output is correct |
11 |
Correct |
294 ms |
25680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
12928 KB |
Output is correct |
2 |
Correct |
231 ms |
27652 KB |
Output is correct |
3 |
Correct |
219 ms |
29992 KB |
Output is correct |
4 |
Correct |
208 ms |
29832 KB |
Output is correct |
5 |
Correct |
25 ms |
12952 KB |
Output is correct |
6 |
Correct |
184 ms |
23168 KB |
Output is correct |
7 |
Correct |
205 ms |
23104 KB |
Output is correct |
8 |
Correct |
231 ms |
23812 KB |
Output is correct |
9 |
Correct |
192 ms |
24096 KB |
Output is correct |
10 |
Correct |
310 ms |
27164 KB |
Output is correct |
11 |
Correct |
294 ms |
25680 KB |
Output is correct |
12 |
Correct |
28 ms |
12896 KB |
Output is correct |
13 |
Correct |
230 ms |
30124 KB |
Output is correct |
14 |
Correct |
216 ms |
30184 KB |
Output is correct |
15 |
Correct |
247 ms |
29252 KB |
Output is correct |
16 |
Correct |
250 ms |
29240 KB |
Output is correct |
17 |
Correct |
25 ms |
12892 KB |
Output is correct |
18 |
Correct |
220 ms |
23372 KB |
Output is correct |
19 |
Correct |
215 ms |
22912 KB |
Output is correct |
20 |
Correct |
211 ms |
23892 KB |
Output is correct |
21 |
Correct |
231 ms |
24308 KB |
Output is correct |
22 |
Correct |
301 ms |
25820 KB |
Output is correct |
23 |
Correct |
444 ms |
26200 KB |
Output is correct |
24 |
Correct |
365 ms |
25540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
12904 KB |
Output is correct |
2 |
Correct |
38 ms |
13268 KB |
Output is correct |
3 |
Correct |
35 ms |
13076 KB |
Output is correct |
4 |
Correct |
38 ms |
13384 KB |
Output is correct |
5 |
Correct |
49 ms |
13132 KB |
Output is correct |
6 |
Correct |
42 ms |
13096 KB |
Output is correct |
7 |
Correct |
26 ms |
13004 KB |
Output is correct |
8 |
Correct |
151 ms |
21596 KB |
Output is correct |
9 |
Correct |
140 ms |
21568 KB |
Output is correct |
10 |
Correct |
25 ms |
13004 KB |
Output is correct |
11 |
Correct |
215 ms |
27688 KB |
Output is correct |
12 |
Correct |
244 ms |
30156 KB |
Output is correct |
13 |
Correct |
210 ms |
29900 KB |
Output is correct |
14 |
Correct |
33 ms |
13004 KB |
Output is correct |
15 |
Correct |
187 ms |
23244 KB |
Output is correct |
16 |
Correct |
185 ms |
22992 KB |
Output is correct |
17 |
Correct |
213 ms |
23892 KB |
Output is correct |
18 |
Correct |
219 ms |
24212 KB |
Output is correct |
19 |
Correct |
269 ms |
27148 KB |
Output is correct |
20 |
Correct |
259 ms |
25752 KB |
Output is correct |
21 |
Correct |
160 ms |
24184 KB |
Output is correct |
22 |
Correct |
164 ms |
24264 KB |
Output is correct |
23 |
Correct |
193 ms |
23636 KB |
Output is correct |
24 |
Correct |
175 ms |
23740 KB |
Output is correct |
25 |
Correct |
252 ms |
26764 KB |
Output is correct |
26 |
Correct |
203 ms |
21768 KB |
Output is correct |
27 |
Correct |
196 ms |
21708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
12904 KB |
Output is correct |
2 |
Correct |
38 ms |
13268 KB |
Output is correct |
3 |
Correct |
35 ms |
13076 KB |
Output is correct |
4 |
Correct |
38 ms |
13384 KB |
Output is correct |
5 |
Correct |
49 ms |
13132 KB |
Output is correct |
6 |
Correct |
42 ms |
13096 KB |
Output is correct |
7 |
Correct |
26 ms |
13004 KB |
Output is correct |
8 |
Correct |
151 ms |
21596 KB |
Output is correct |
9 |
Correct |
140 ms |
21568 KB |
Output is correct |
10 |
Correct |
25 ms |
13004 KB |
Output is correct |
11 |
Correct |
215 ms |
27688 KB |
Output is correct |
12 |
Correct |
244 ms |
30156 KB |
Output is correct |
13 |
Correct |
210 ms |
29900 KB |
Output is correct |
14 |
Correct |
33 ms |
13004 KB |
Output is correct |
15 |
Correct |
187 ms |
23244 KB |
Output is correct |
16 |
Correct |
185 ms |
22992 KB |
Output is correct |
17 |
Correct |
213 ms |
23892 KB |
Output is correct |
18 |
Correct |
219 ms |
24212 KB |
Output is correct |
19 |
Correct |
269 ms |
27148 KB |
Output is correct |
20 |
Correct |
259 ms |
25752 KB |
Output is correct |
21 |
Correct |
160 ms |
24184 KB |
Output is correct |
22 |
Correct |
164 ms |
24264 KB |
Output is correct |
23 |
Correct |
193 ms |
23636 KB |
Output is correct |
24 |
Correct |
175 ms |
23740 KB |
Output is correct |
25 |
Correct |
252 ms |
26764 KB |
Output is correct |
26 |
Correct |
203 ms |
21768 KB |
Output is correct |
27 |
Correct |
196 ms |
21708 KB |
Output is correct |
28 |
Correct |
27 ms |
12900 KB |
Output is correct |
29 |
Correct |
38 ms |
13136 KB |
Output is correct |
30 |
Correct |
34 ms |
12916 KB |
Output is correct |
31 |
Correct |
40 ms |
13144 KB |
Output is correct |
32 |
Correct |
39 ms |
12912 KB |
Output is correct |
33 |
Correct |
35 ms |
12876 KB |
Output is correct |
34 |
Correct |
27 ms |
12916 KB |
Output is correct |
35 |
Correct |
132 ms |
23168 KB |
Output is correct |
36 |
Correct |
93 ms |
20420 KB |
Output is correct |
37 |
Correct |
99 ms |
20804 KB |
Output is correct |
38 |
Correct |
28 ms |
12884 KB |
Output is correct |
39 |
Correct |
241 ms |
30144 KB |
Output is correct |
40 |
Correct |
203 ms |
30172 KB |
Output is correct |
41 |
Correct |
220 ms |
29276 KB |
Output is correct |
42 |
Correct |
220 ms |
29396 KB |
Output is correct |
43 |
Correct |
27 ms |
12892 KB |
Output is correct |
44 |
Correct |
211 ms |
23380 KB |
Output is correct |
45 |
Correct |
205 ms |
22932 KB |
Output is correct |
46 |
Correct |
220 ms |
23832 KB |
Output is correct |
47 |
Correct |
223 ms |
24316 KB |
Output is correct |
48 |
Correct |
302 ms |
25872 KB |
Output is correct |
49 |
Correct |
369 ms |
26100 KB |
Output is correct |
50 |
Correct |
389 ms |
25544 KB |
Output is correct |
51 |
Correct |
114 ms |
22016 KB |
Output is correct |
52 |
Correct |
98 ms |
21540 KB |
Output is correct |
53 |
Correct |
92 ms |
21236 KB |
Output is correct |
54 |
Correct |
122 ms |
21648 KB |
Output is correct |
55 |
Correct |
108 ms |
21556 KB |
Output is correct |
56 |
Correct |
163 ms |
22776 KB |
Output is correct |
57 |
Correct |
239 ms |
24708 KB |
Output is correct |
58 |
Correct |
277 ms |
22220 KB |
Output is correct |
59 |
Correct |
226 ms |
21872 KB |
Output is correct |