#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
const int N = 120000;
int n, q;
struct AIB
{
int aib[2 * N + 5];
vector<int> descos;
inline int lsb(int x) {return x & -x;}
void update(int pos, int val)
{
while(pos < n + q)
aib[pos] += val,
descos.push_back(pos),
pos += lsb(pos);
}
int query(int pos)
{
int ans = 0;
while(pos)
ans += aib[pos],
pos -= lsb(pos);
return ans;
}
void sterge()
{
for(auto i : descos)
aib[i] = 0;
descos.clear();
}
} ds;
struct Query
{
char type;
int to, time;
};
vector<Query> qry[N + 5];
vector<pii> vec[N + 5];
int ans[2 * N + 5], sz[N + 5], muchtopapa[N + 5];
int crdcr[N + 5], papa[N + 5], papasubc[N + 5];
bool viz[N + 5], amdat[N + 5];
void dfssz(int nod, int papa)
{
sz[nod] = 1;
for(auto [i, j] : vec[nod])
if(!viz[i] && i != papa)
dfssz(i, nod), sz[nod] += sz[i];
}
int dfsCentroid(int nod, int szinit)
{
for(auto [i, j] : vec[nod])
if(!viz[i] && sz[i] < sz[nod] && 2 * sz[i] >= szinit)
return dfsCentroid(i, szinit);
return nod;
}
void dfs(int nod, vector<int> &x)
{
x.push_back(nod);
amdat[nod] = true;
for(auto [i, j] : vec[nod])
{
if(viz[i] || i == papa[nod])
continue;
papa[i] = nod;
papasubc[i] = papasubc[nod];
muchtopapa[i] = j;
crdcr[i] = 0;
if(j < muchtopapa[nod] && (crdcr[nod] & 1))
crdcr[i] ++;
if(j > muchtopapa[nod] && (crdcr[nod] & 2))
crdcr[i] += 2;
dfs(i, x);
}
}
void doCentroid(int nod)
{
dfssz(nod, 0);
int centr = dfsCentroid(nod, sz[nod]);
vector<pair<int, vector<int>>> subarb;
for(auto [i, j] : vec[centr])
{
if(viz[i])
continue;
subarb.push_back({i, {}});
papa[i] = centr;
papasubc[i] = i;
muchtopapa[i] = j;
crdcr[i] = 3;
dfs(i, subarb.back().sc);
}
muchtopapa[centr] = 0;
sort(all(subarb), [&](pair<int, vector<int>> a, pair<int, vector<int>> b)
{
return muchtopapa[a.fr] > muchtopapa[b.fr];
});
amdat[centr] = true;
crdcr[centr] = 3;
ds.update(1, 1);
for(auto [i, v] : subarb)
{
for(auto j : v)
for(auto k : qry[j])
{
if(k.type == 'c')
{
if(muchtopapa[papasubc[j]] <= k.time && (crdcr[j] & 1))
ans[k.time] += ds.query(k.time);
}
else
{
if(k.to != centr && amdat[k.to] && muchtopapa[papasubc[k.to]] > muchtopapa[papasubc[j]] &&
muchtopapa[k.to] <= k.time && (crdcr[k.to] & 2) && (crdcr[j] & 1))
ans[k.time] = -1;
else if(k.to == centr && (crdcr[j] & 1) && muchtopapa[papasubc[j]] <= k.time)
ans[k.time] = -1;
}
}
for(auto j : v)
if(crdcr[j] & 2)
ds.update(muchtopapa[j], 1);
}
ds.update(1, -1);
for(auto i : qry[centr])
{
if(i.type == 'c')
ans[i.time] += ds.query(i.time);
else
{
if(amdat[i.to] && (crdcr[i.to] & 2) && muchtopapa[i.to] <= i.time)
ans[i.time] = -1;
}
}
ds.sterge();
amdat[centr] = false;
for(auto i : subarb)
for(auto j : i.sc)
amdat[j] = false;
viz[centr] = true;
for(auto [i, j] : vec[centr])
if(!viz[i])
doCentroid(i);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for(int i = 1; i < n + q; i ++)
{
char t;
cin >> t;
if(t == 'S')
{
int u, v;
cin >> u >> v;
vec[u].push_back({v, i});
vec[v].push_back({u, i});
ans[i] = -69;
}
if(t == 'Q')
{
int u, v;
cin >> u >> v;
qry[v].push_back({'q', u, i});
ans[i] = -2;
}
if(t == 'C')
{
int u;
cin >> u;
qry[u].push_back({'c', 0, i});
}
}
doCentroid(1);
for(int i = 1; i < n + q; i ++)
{
if(ans[i] == -1)
cout << "yes\n";
else if(ans[i] == -2)
cout << "no\n";
else if(ans[i] >= 0)
cout << ans[i] + 1 << '\n';
}
}
Compilation message
servers.cpp: In function 'void dfssz(int, int)':
servers.cpp:55:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for(auto [i, j] : vec[nod])
| ^
servers.cpp: In function 'int dfsCentroid(int, int)':
servers.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | for(auto [i, j] : vec[nod])
| ^
servers.cpp: In function 'void dfs(int, std::vector<int>&)':
servers.cpp:72:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for(auto [i, j] : vec[nod])
| ^
servers.cpp: In function 'void doCentroid(int)':
servers.cpp:93:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
93 | for(auto [i, j] : vec[centr])
| ^
servers.cpp:112:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
112 | for(auto [i, v] : subarb)
| ^
servers.cpp:152:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
152 | for(auto [i, j] : vec[centr])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12368 KB |
Output is correct |
2 |
Correct |
36 ms |
12872 KB |
Output is correct |
3 |
Correct |
26 ms |
12624 KB |
Output is correct |
4 |
Correct |
32 ms |
12880 KB |
Output is correct |
5 |
Correct |
31 ms |
12624 KB |
Output is correct |
6 |
Correct |
27 ms |
13004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12368 KB |
Output is correct |
2 |
Correct |
36 ms |
12872 KB |
Output is correct |
3 |
Correct |
26 ms |
12624 KB |
Output is correct |
4 |
Correct |
32 ms |
12880 KB |
Output is correct |
5 |
Correct |
31 ms |
12624 KB |
Output is correct |
6 |
Correct |
27 ms |
13004 KB |
Output is correct |
7 |
Correct |
18 ms |
12112 KB |
Output is correct |
8 |
Correct |
34 ms |
12752 KB |
Output is correct |
9 |
Correct |
26 ms |
12440 KB |
Output is correct |
10 |
Correct |
37 ms |
12872 KB |
Output is correct |
11 |
Correct |
40 ms |
12616 KB |
Output is correct |
12 |
Correct |
29 ms |
12376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12368 KB |
Output is correct |
2 |
Correct |
188 ms |
35936 KB |
Output is correct |
3 |
Correct |
201 ms |
35768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12368 KB |
Output is correct |
2 |
Correct |
188 ms |
35936 KB |
Output is correct |
3 |
Correct |
201 ms |
35768 KB |
Output is correct |
4 |
Correct |
18 ms |
12368 KB |
Output is correct |
5 |
Correct |
197 ms |
33656 KB |
Output is correct |
6 |
Correct |
150 ms |
34676 KB |
Output is correct |
7 |
Correct |
155 ms |
34656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
12368 KB |
Output is correct |
2 |
Correct |
282 ms |
27336 KB |
Output is correct |
3 |
Correct |
285 ms |
27284 KB |
Output is correct |
4 |
Correct |
185 ms |
32816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
12368 KB |
Output is correct |
2 |
Correct |
282 ms |
27336 KB |
Output is correct |
3 |
Correct |
285 ms |
27284 KB |
Output is correct |
4 |
Correct |
185 ms |
32816 KB |
Output is correct |
5 |
Correct |
19 ms |
12112 KB |
Output is correct |
6 |
Correct |
285 ms |
27500 KB |
Output is correct |
7 |
Correct |
225 ms |
29544 KB |
Output is correct |
8 |
Correct |
285 ms |
27244 KB |
Output is correct |
9 |
Correct |
265 ms |
27384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
12368 KB |
Output is correct |
2 |
Correct |
179 ms |
21220 KB |
Output is correct |
3 |
Correct |
215 ms |
18976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
12368 KB |
Output is correct |
2 |
Correct |
179 ms |
21220 KB |
Output is correct |
3 |
Correct |
215 ms |
18976 KB |
Output is correct |
4 |
Correct |
19 ms |
12112 KB |
Output is correct |
5 |
Correct |
201 ms |
21220 KB |
Output is correct |
6 |
Correct |
237 ms |
19052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12360 KB |
Output is correct |
2 |
Correct |
260 ms |
27444 KB |
Output is correct |
3 |
Correct |
252 ms |
27424 KB |
Output is correct |
4 |
Correct |
185 ms |
32848 KB |
Output is correct |
5 |
Correct |
18 ms |
12368 KB |
Output is correct |
6 |
Correct |
172 ms |
21092 KB |
Output is correct |
7 |
Correct |
201 ms |
19008 KB |
Output is correct |
8 |
Correct |
222 ms |
19324 KB |
Output is correct |
9 |
Correct |
237 ms |
19652 KB |
Output is correct |
10 |
Correct |
244 ms |
23748 KB |
Output is correct |
11 |
Correct |
233 ms |
22524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
12360 KB |
Output is correct |
2 |
Correct |
260 ms |
27444 KB |
Output is correct |
3 |
Correct |
252 ms |
27424 KB |
Output is correct |
4 |
Correct |
185 ms |
32848 KB |
Output is correct |
5 |
Correct |
18 ms |
12368 KB |
Output is correct |
6 |
Correct |
172 ms |
21092 KB |
Output is correct |
7 |
Correct |
201 ms |
19008 KB |
Output is correct |
8 |
Correct |
222 ms |
19324 KB |
Output is correct |
9 |
Correct |
237 ms |
19652 KB |
Output is correct |
10 |
Correct |
244 ms |
23748 KB |
Output is correct |
11 |
Correct |
233 ms |
22524 KB |
Output is correct |
12 |
Correct |
17 ms |
12112 KB |
Output is correct |
13 |
Correct |
263 ms |
27284 KB |
Output is correct |
14 |
Correct |
193 ms |
29556 KB |
Output is correct |
15 |
Correct |
262 ms |
27244 KB |
Output is correct |
16 |
Correct |
278 ms |
27496 KB |
Output is correct |
17 |
Correct |
17 ms |
12112 KB |
Output is correct |
18 |
Correct |
175 ms |
21076 KB |
Output is correct |
19 |
Correct |
199 ms |
19052 KB |
Output is correct |
20 |
Correct |
190 ms |
19344 KB |
Output is correct |
21 |
Correct |
208 ms |
19836 KB |
Output is correct |
22 |
Correct |
217 ms |
22960 KB |
Output is correct |
23 |
Correct |
278 ms |
23864 KB |
Output is correct |
24 |
Correct |
275 ms |
25020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12368 KB |
Output is correct |
2 |
Correct |
30 ms |
12880 KB |
Output is correct |
3 |
Correct |
27 ms |
12616 KB |
Output is correct |
4 |
Correct |
32 ms |
12880 KB |
Output is correct |
5 |
Correct |
28 ms |
12652 KB |
Output is correct |
6 |
Correct |
26 ms |
13004 KB |
Output is correct |
7 |
Correct |
26 ms |
12368 KB |
Output is correct |
8 |
Correct |
209 ms |
35768 KB |
Output is correct |
9 |
Correct |
203 ms |
35772 KB |
Output is correct |
10 |
Correct |
19 ms |
12368 KB |
Output is correct |
11 |
Correct |
251 ms |
27284 KB |
Output is correct |
12 |
Correct |
243 ms |
27288 KB |
Output is correct |
13 |
Correct |
171 ms |
32780 KB |
Output is correct |
14 |
Correct |
17 ms |
12540 KB |
Output is correct |
15 |
Correct |
161 ms |
21092 KB |
Output is correct |
16 |
Correct |
218 ms |
18984 KB |
Output is correct |
17 |
Correct |
201 ms |
19324 KB |
Output is correct |
18 |
Correct |
200 ms |
19724 KB |
Output is correct |
19 |
Correct |
209 ms |
23744 KB |
Output is correct |
20 |
Correct |
217 ms |
22304 KB |
Output is correct |
21 |
Correct |
170 ms |
30008 KB |
Output is correct |
22 |
Correct |
168 ms |
29472 KB |
Output is correct |
23 |
Correct |
186 ms |
19652 KB |
Output is correct |
24 |
Correct |
161 ms |
19536 KB |
Output is correct |
25 |
Correct |
279 ms |
32312 KB |
Output is correct |
26 |
Correct |
193 ms |
18404 KB |
Output is correct |
27 |
Correct |
174 ms |
18440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12368 KB |
Output is correct |
2 |
Correct |
30 ms |
12880 KB |
Output is correct |
3 |
Correct |
27 ms |
12616 KB |
Output is correct |
4 |
Correct |
32 ms |
12880 KB |
Output is correct |
5 |
Correct |
28 ms |
12652 KB |
Output is correct |
6 |
Correct |
26 ms |
13004 KB |
Output is correct |
7 |
Correct |
26 ms |
12368 KB |
Output is correct |
8 |
Correct |
209 ms |
35768 KB |
Output is correct |
9 |
Correct |
203 ms |
35772 KB |
Output is correct |
10 |
Correct |
19 ms |
12368 KB |
Output is correct |
11 |
Correct |
251 ms |
27284 KB |
Output is correct |
12 |
Correct |
243 ms |
27288 KB |
Output is correct |
13 |
Correct |
171 ms |
32780 KB |
Output is correct |
14 |
Correct |
17 ms |
12540 KB |
Output is correct |
15 |
Correct |
161 ms |
21092 KB |
Output is correct |
16 |
Correct |
218 ms |
18984 KB |
Output is correct |
17 |
Correct |
201 ms |
19324 KB |
Output is correct |
18 |
Correct |
200 ms |
19724 KB |
Output is correct |
19 |
Correct |
209 ms |
23744 KB |
Output is correct |
20 |
Correct |
217 ms |
22304 KB |
Output is correct |
21 |
Correct |
170 ms |
30008 KB |
Output is correct |
22 |
Correct |
168 ms |
29472 KB |
Output is correct |
23 |
Correct |
186 ms |
19652 KB |
Output is correct |
24 |
Correct |
161 ms |
19536 KB |
Output is correct |
25 |
Correct |
279 ms |
32312 KB |
Output is correct |
26 |
Correct |
193 ms |
18404 KB |
Output is correct |
27 |
Correct |
174 ms |
18440 KB |
Output is correct |
28 |
Correct |
18 ms |
12112 KB |
Output is correct |
29 |
Correct |
33 ms |
13048 KB |
Output is correct |
30 |
Correct |
27 ms |
12408 KB |
Output is correct |
31 |
Correct |
34 ms |
12872 KB |
Output is correct |
32 |
Correct |
42 ms |
12736 KB |
Output is correct |
33 |
Correct |
26 ms |
12356 KB |
Output is correct |
34 |
Correct |
17 ms |
12112 KB |
Output is correct |
35 |
Correct |
166 ms |
33780 KB |
Output is correct |
36 |
Correct |
142 ms |
32668 KB |
Output is correct |
37 |
Correct |
149 ms |
32660 KB |
Output is correct |
38 |
Correct |
17 ms |
12112 KB |
Output is correct |
39 |
Correct |
291 ms |
27284 KB |
Output is correct |
40 |
Correct |
193 ms |
29552 KB |
Output is correct |
41 |
Correct |
271 ms |
27304 KB |
Output is correct |
42 |
Correct |
271 ms |
27504 KB |
Output is correct |
43 |
Correct |
18 ms |
12112 KB |
Output is correct |
44 |
Correct |
179 ms |
21220 KB |
Output is correct |
45 |
Correct |
220 ms |
18972 KB |
Output is correct |
46 |
Correct |
216 ms |
19472 KB |
Output is correct |
47 |
Correct |
237 ms |
19968 KB |
Output is correct |
48 |
Correct |
229 ms |
22948 KB |
Output is correct |
49 |
Correct |
290 ms |
23952 KB |
Output is correct |
50 |
Correct |
296 ms |
25216 KB |
Output is correct |
51 |
Correct |
141 ms |
31260 KB |
Output is correct |
52 |
Correct |
145 ms |
34648 KB |
Output is correct |
53 |
Correct |
135 ms |
34724 KB |
Output is correct |
54 |
Correct |
137 ms |
34796 KB |
Output is correct |
55 |
Correct |
138 ms |
28800 KB |
Output is correct |
56 |
Correct |
157 ms |
19252 KB |
Output is correct |
57 |
Correct |
213 ms |
31100 KB |
Output is correct |
58 |
Correct |
224 ms |
18772 KB |
Output is correct |
59 |
Correct |
186 ms |
18660 KB |
Output is correct |