#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) int(x.size())
const int mx = 120'000;
struct edge
{
int v;
int t;
};
vector<edge> S[1+mx];
struct query
{
int a;
int t;
};
vector<query> Q[1+mx]; //stored by d
vector<int> C[1+mx]; //stored by d
int N, K;
vi res(2*mx + 5);
vi banned(1+mx, 0);
vi st_count(1+mx, 0);
vi lst;
void dfs(int u, int p)
{
lst.push_back(u);
st_count[u] = 1;
for(auto e: S[u])
{
int v = e.v;
if(v == p) continue;
if(banned[v]) continue;
dfs(v, u);
st_count[u] += st_count[v];
}
}
vi out_good(1+mx); //going out from centroid
vi in_good(1+mx); //coming in to centroid
vi root_time(1+mx);
vi par_time(1+mx);
vi st_index(1+mx);
vi exists(1+mx, 0);
void dfs2(int u, int p)
{
for(auto e: S[u])
{
int v = e.v;
int t = e.t;
if(v == p) continue;
if(banned[v]) continue;
root_time[v] = root_time[u];
par_time[v] = t;
out_good[v] = out_good[u] && (par_time[v] > par_time[u]);
in_good[v] = in_good[u] && (par_time[v] < par_time[u]);
st_index[v] = st_index[u];
dfs2(v, u);
}
}
vector<pii> obj_list;
vi BIT(1+2*mx, 0);
void solve(int src)
{
lst.clear();
dfs(src, src);
int cent = -1;
for(int u: lst)
{
exists[u] = 1;
if(2 * st_count[u] < st_count[src]) continue;
if(cent == -1) cent = u;
else if(st_count[u] < st_count[cent]) cent = u;
}
// cerr << "\n\n\n\n";
// for(int l: lst) cerr << l << ' ';
// cerr << ": centroid = " << cent << '\n';
// for(int l: lst) cerr << l << " : " << out_good[l] << ' ' << in_good[l] << ' ' << root_time[l] << ' ' << par_time[l] << '\n';
int stid = 0;
st_index[cent] = 0;
in_good[cent] = 1;
out_good[cent] = 1;
for(auto e: S[cent])
{
int v = e.v;
int t = e.t;
if(banned[v]) continue;
st_index[v] = ++stid;
in_good[v] = out_good[v] = 1;
root_time[v] = par_time[v] = t;
dfs2(v, cent);
}
for(int d: lst)
{
for(query qr: Q[d])
{
int a = qr.a;
int t = qr.t;
if(!exists[a]) continue;
if(d == cent)
{
if(a == cent)
res[t] = 1;
else
res[t] |= (out_good[a] && par_time[a] < t);
}
else
{
if(a == cent)
res[t] |= (in_good[d] && root_time[d] < t);
else
res[t] |= (in_good[d] && out_good[a] && root_time[d] < root_time[a] && par_time[a] < t);
}
}
}
vector<pii> queries, updates;
root_time[cent] = 0;
for(int d: lst)
{
if(in_good[d] == 0) continue;
for(int t: C[d])
{
if(root_time[d] > t) continue;
queries.push_back({root_time[d], t});
}
}
for(int a: lst)
{
if(out_good[a] == 0) continue;
if(a == cent) continue;
updates.push_back({root_time[a], par_time[a]});
}
sort(queries.begin(), queries.end(), [] (pii u, pii v)
{
return u.first > v.first;
});
sort(updates.begin(), updates.end(), [] (pii u, pii v)
{
return u.first > v.first;
});
int ui = -1;
// BIT = vi(N+K, 0);
for(pii z: queries)
{
while(ui+1 < sz(updates) && updates[ui+1].first > z.first)
{
ui++;
for(int q = updates[ui].second; q <= N+K-1; q += q&-q)
BIT[q]++;
}
res[z.second]++;
for(int q = z.second-1; q >= 1; q -= q&-q)
res[z.second] += BIT[q];
}
while(ui >= 0)
{
for(int q = updates[ui].second; q <= N+K-1; q += q&-q)
BIT[q]--;
ui--;
}
// vi BIT(N+K, 0);
// for(pii z: obj)
// {
// if(z.second > 0) //count query
// {
// int t = z.second;
// res[t]++; //centroid
// for(int q = t-1; q >= 1; q -= q&-q)
// res[t] += BIT[q];
// }
// else //node
// {
// int t = -z.second;
// for(int q = t; q <= N+K-1; q += q&-q)
// BIT[q]++;
// }
// }
for(int u: lst) exists[u] = 0;
banned[cent] = 1;
for(auto e: S[cent])
{
if(banned[e.v]) continue;
solve(e.v);
}
banned[cent] = 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
char c[N+K];
for(int q = 1; q <= N+K-1; q++)
{
cin >> c[q];
if(c[q] == 'S')
{
int a, b;
cin >> a >> b;
S[a].push_back({b, q});
S[b].push_back({a, q});
}
else if(c[q] == 'Q')
{
int a, d;
cin >> a >> d;
Q[d].push_back({a, q});
}
else
{
int d;
cin >> d;
C[d].push_back(q);
}
}
solve(1);
for(int q = 1; q <= N+K-1; q++)
{
if(c[q] == 'S') continue;
else if(c[q] == 'Q')
{
if(res[q] == 1) cout << "yes\n";
else cout << "no\n";
}
else cout << res[q] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
16400 KB |
Output is correct |
2 |
Correct |
38 ms |
16604 KB |
Output is correct |
3 |
Correct |
41 ms |
16532 KB |
Output is correct |
4 |
Correct |
42 ms |
16712 KB |
Output is correct |
5 |
Correct |
59 ms |
16924 KB |
Output is correct |
6 |
Correct |
33 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
16400 KB |
Output is correct |
2 |
Correct |
38 ms |
16604 KB |
Output is correct |
3 |
Correct |
41 ms |
16532 KB |
Output is correct |
4 |
Correct |
42 ms |
16712 KB |
Output is correct |
5 |
Correct |
59 ms |
16924 KB |
Output is correct |
6 |
Correct |
33 ms |
16732 KB |
Output is correct |
7 |
Correct |
31 ms |
16436 KB |
Output is correct |
8 |
Correct |
48 ms |
16328 KB |
Output is correct |
9 |
Correct |
44 ms |
16304 KB |
Output is correct |
10 |
Correct |
49 ms |
16400 KB |
Output is correct |
11 |
Correct |
52 ms |
16524 KB |
Output is correct |
12 |
Correct |
38 ms |
16476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
16500 KB |
Output is correct |
2 |
Correct |
150 ms |
24056 KB |
Output is correct |
3 |
Correct |
151 ms |
23980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
16500 KB |
Output is correct |
2 |
Correct |
150 ms |
24056 KB |
Output is correct |
3 |
Correct |
151 ms |
23980 KB |
Output is correct |
4 |
Correct |
28 ms |
16456 KB |
Output is correct |
5 |
Correct |
152 ms |
24072 KB |
Output is correct |
6 |
Correct |
94 ms |
23168 KB |
Output is correct |
7 |
Correct |
100 ms |
22976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
16452 KB |
Output is correct |
2 |
Correct |
364 ms |
29320 KB |
Output is correct |
3 |
Correct |
382 ms |
29324 KB |
Output is correct |
4 |
Correct |
219 ms |
29608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
16452 KB |
Output is correct |
2 |
Correct |
364 ms |
29320 KB |
Output is correct |
3 |
Correct |
382 ms |
29324 KB |
Output is correct |
4 |
Correct |
219 ms |
29608 KB |
Output is correct |
5 |
Correct |
28 ms |
16424 KB |
Output is correct |
6 |
Correct |
460 ms |
29692 KB |
Output is correct |
7 |
Correct |
304 ms |
30732 KB |
Output is correct |
8 |
Correct |
342 ms |
29412 KB |
Output is correct |
9 |
Correct |
364 ms |
29504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
16436 KB |
Output is correct |
2 |
Correct |
207 ms |
23500 KB |
Output is correct |
3 |
Correct |
337 ms |
22816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
16436 KB |
Output is correct |
2 |
Correct |
207 ms |
23500 KB |
Output is correct |
3 |
Correct |
337 ms |
22816 KB |
Output is correct |
4 |
Correct |
42 ms |
16452 KB |
Output is correct |
5 |
Correct |
281 ms |
24428 KB |
Output is correct |
6 |
Correct |
333 ms |
23384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
16400 KB |
Output is correct |
2 |
Correct |
388 ms |
29316 KB |
Output is correct |
3 |
Correct |
382 ms |
29324 KB |
Output is correct |
4 |
Correct |
214 ms |
29624 KB |
Output is correct |
5 |
Correct |
27 ms |
16496 KB |
Output is correct |
6 |
Correct |
260 ms |
23508 KB |
Output is correct |
7 |
Correct |
283 ms |
22808 KB |
Output is correct |
8 |
Correct |
338 ms |
23620 KB |
Output is correct |
9 |
Correct |
393 ms |
23464 KB |
Output is correct |
10 |
Correct |
449 ms |
26684 KB |
Output is correct |
11 |
Correct |
397 ms |
25500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
16400 KB |
Output is correct |
2 |
Correct |
388 ms |
29316 KB |
Output is correct |
3 |
Correct |
382 ms |
29324 KB |
Output is correct |
4 |
Correct |
214 ms |
29624 KB |
Output is correct |
5 |
Correct |
27 ms |
16496 KB |
Output is correct |
6 |
Correct |
260 ms |
23508 KB |
Output is correct |
7 |
Correct |
283 ms |
22808 KB |
Output is correct |
8 |
Correct |
338 ms |
23620 KB |
Output is correct |
9 |
Correct |
393 ms |
23464 KB |
Output is correct |
10 |
Correct |
449 ms |
26684 KB |
Output is correct |
11 |
Correct |
397 ms |
25500 KB |
Output is correct |
12 |
Correct |
28 ms |
16460 KB |
Output is correct |
13 |
Correct |
365 ms |
29700 KB |
Output is correct |
14 |
Correct |
313 ms |
30740 KB |
Output is correct |
15 |
Correct |
343 ms |
29412 KB |
Output is correct |
16 |
Correct |
316 ms |
29416 KB |
Output is correct |
17 |
Correct |
29 ms |
16708 KB |
Output is correct |
18 |
Correct |
279 ms |
24812 KB |
Output is correct |
19 |
Correct |
308 ms |
23204 KB |
Output is correct |
20 |
Correct |
378 ms |
24008 KB |
Output is correct |
21 |
Correct |
396 ms |
24764 KB |
Output is correct |
22 |
Correct |
450 ms |
26796 KB |
Output is correct |
23 |
Correct |
597 ms |
27900 KB |
Output is correct |
24 |
Correct |
527 ms |
29384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
16452 KB |
Output is correct |
2 |
Correct |
37 ms |
16708 KB |
Output is correct |
3 |
Correct |
49 ms |
16480 KB |
Output is correct |
4 |
Correct |
55 ms |
16772 KB |
Output is correct |
5 |
Correct |
41 ms |
16936 KB |
Output is correct |
6 |
Correct |
40 ms |
16724 KB |
Output is correct |
7 |
Correct |
27 ms |
16432 KB |
Output is correct |
8 |
Correct |
146 ms |
24032 KB |
Output is correct |
9 |
Correct |
131 ms |
23996 KB |
Output is correct |
10 |
Correct |
27 ms |
16472 KB |
Output is correct |
11 |
Correct |
341 ms |
29316 KB |
Output is correct |
12 |
Correct |
359 ms |
29324 KB |
Output is correct |
13 |
Correct |
239 ms |
29616 KB |
Output is correct |
14 |
Correct |
32 ms |
16480 KB |
Output is correct |
15 |
Correct |
229 ms |
23564 KB |
Output is correct |
16 |
Correct |
300 ms |
22816 KB |
Output is correct |
17 |
Correct |
307 ms |
23524 KB |
Output is correct |
18 |
Correct |
396 ms |
23512 KB |
Output is correct |
19 |
Correct |
459 ms |
26628 KB |
Output is correct |
20 |
Correct |
402 ms |
25484 KB |
Output is correct |
21 |
Correct |
161 ms |
23780 KB |
Output is correct |
22 |
Correct |
209 ms |
23444 KB |
Output is correct |
23 |
Correct |
296 ms |
23068 KB |
Output is correct |
24 |
Correct |
277 ms |
23120 KB |
Output is correct |
25 |
Correct |
421 ms |
26000 KB |
Output is correct |
26 |
Correct |
274 ms |
21832 KB |
Output is correct |
27 |
Correct |
303 ms |
21764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
16452 KB |
Output is correct |
2 |
Correct |
37 ms |
16708 KB |
Output is correct |
3 |
Correct |
49 ms |
16480 KB |
Output is correct |
4 |
Correct |
55 ms |
16772 KB |
Output is correct |
5 |
Correct |
41 ms |
16936 KB |
Output is correct |
6 |
Correct |
40 ms |
16724 KB |
Output is correct |
7 |
Correct |
27 ms |
16432 KB |
Output is correct |
8 |
Correct |
146 ms |
24032 KB |
Output is correct |
9 |
Correct |
131 ms |
23996 KB |
Output is correct |
10 |
Correct |
27 ms |
16472 KB |
Output is correct |
11 |
Correct |
341 ms |
29316 KB |
Output is correct |
12 |
Correct |
359 ms |
29324 KB |
Output is correct |
13 |
Correct |
239 ms |
29616 KB |
Output is correct |
14 |
Correct |
32 ms |
16480 KB |
Output is correct |
15 |
Correct |
229 ms |
23564 KB |
Output is correct |
16 |
Correct |
300 ms |
22816 KB |
Output is correct |
17 |
Correct |
307 ms |
23524 KB |
Output is correct |
18 |
Correct |
396 ms |
23512 KB |
Output is correct |
19 |
Correct |
459 ms |
26628 KB |
Output is correct |
20 |
Correct |
402 ms |
25484 KB |
Output is correct |
21 |
Correct |
161 ms |
23780 KB |
Output is correct |
22 |
Correct |
209 ms |
23444 KB |
Output is correct |
23 |
Correct |
296 ms |
23068 KB |
Output is correct |
24 |
Correct |
277 ms |
23120 KB |
Output is correct |
25 |
Correct |
421 ms |
26000 KB |
Output is correct |
26 |
Correct |
274 ms |
21832 KB |
Output is correct |
27 |
Correct |
303 ms |
21764 KB |
Output is correct |
28 |
Correct |
29 ms |
16500 KB |
Output is correct |
29 |
Correct |
45 ms |
16232 KB |
Output is correct |
30 |
Correct |
45 ms |
17112 KB |
Output is correct |
31 |
Correct |
52 ms |
17132 KB |
Output is correct |
32 |
Correct |
48 ms |
17260 KB |
Output is correct |
33 |
Correct |
38 ms |
17212 KB |
Output is correct |
34 |
Correct |
28 ms |
17244 KB |
Output is correct |
35 |
Correct |
142 ms |
24880 KB |
Output is correct |
36 |
Correct |
102 ms |
24812 KB |
Output is correct |
37 |
Correct |
131 ms |
24876 KB |
Output is correct |
38 |
Correct |
28 ms |
17272 KB |
Output is correct |
39 |
Correct |
354 ms |
32672 KB |
Output is correct |
40 |
Correct |
274 ms |
33492 KB |
Output is correct |
41 |
Correct |
357 ms |
31812 KB |
Output is correct |
42 |
Correct |
364 ms |
31828 KB |
Output is correct |
43 |
Correct |
28 ms |
17272 KB |
Output is correct |
44 |
Correct |
278 ms |
27296 KB |
Output is correct |
45 |
Correct |
323 ms |
26004 KB |
Output is correct |
46 |
Correct |
305 ms |
26856 KB |
Output is correct |
47 |
Correct |
391 ms |
26884 KB |
Output is correct |
48 |
Correct |
413 ms |
28784 KB |
Output is correct |
49 |
Correct |
581 ms |
30068 KB |
Output is correct |
50 |
Correct |
509 ms |
29360 KB |
Output is correct |
51 |
Correct |
125 ms |
26464 KB |
Output is correct |
52 |
Correct |
113 ms |
25748 KB |
Output is correct |
53 |
Correct |
107 ms |
25116 KB |
Output is correct |
54 |
Correct |
131 ms |
25956 KB |
Output is correct |
55 |
Correct |
101 ms |
25360 KB |
Output is correct |
56 |
Correct |
238 ms |
25716 KB |
Output is correct |
57 |
Correct |
286 ms |
28860 KB |
Output is correct |
58 |
Correct |
388 ms |
25964 KB |
Output is correct |
59 |
Correct |
277 ms |
25372 KB |
Output is correct |