#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1.2e5;
const int MAX_Q = 1.2e5;
struct edge {
int u, v, t;
int other( int x ) {
return u ^ v ^ x;
}
};
struct query {
char type;
int u, v, t, ans;
};
struct FRECV {
int f[MAX_N + 1];
vector<int> ch;
void init() {
for ( int i = 0; i <= MAX_N; i++ )
f[i] = MAX_N + MAX_Q + 1;
}
void clear() {
for ( int i: ch )
f[i] = MAX_N + MAX_Q + 1;
ch.clear();
}
void update( int i, int x ) {
f[i] = x;
ch.push_back( i );
}
int qry( int x ){
return f[x];
}
};
struct AIB {
int aib[MAX_N + MAX_Q + 1];
vector<int> ch;
void clear() {
for ( int i: ch )
aib[i] = 0;
ch.clear();
}
void update( int i, int x ) {
while ( i <= MAX_N + MAX_Q ) {
aib[i] += x;
ch.push_back( i );
i += (i & -i);
}
}
int query( int i ) {
int s = 0;
while ( i > 0 ) {
s += aib[i];
i -= (i & -i);
}
return s;
}
};
edge edges[MAX_N];
query queries[MAX_Q];
vector<int> adj[MAX_N + 1], queriesQ[MAX_N + 1], queriesC[MAX_N + 1];
bool isCentroid[MAX_N + 1];
int sizee[MAX_N + 1];
void calcSizes( int u, int p ) {
sizee[u] = 1;
for ( int e: adj[u] ) {
int v = edges[e].other( u );
if ( v == p || isCentroid[v] )
continue;
calcSizes( v, u );
sizee[u] += sizee[v];
}
}
int sz, centroid;
void findCentroid( int u, int p ) {
int maxSizee = sz - sizee[u];
for ( int e: adj[u] ) {
int v = edges[e].other( u );
if ( v == p || isCentroid[v] )
continue;
findCentroid( v, u );
maxSizee = max( maxSizee, sizee[v] );
}
if ( maxSizee <= sz / 2 )
centroid = u;
}
vector<pair<int, int>> incc;
vector<int> decc;
void calcIncreasing( int u, int p, int t ) {
incc.push_back( { u, t } );
for ( int e: adj[u] ) {
int v = edges[e].other( u ), s = edges[e].t;
if ( v == p || isCentroid[v] || t > s )
continue;
calcIncreasing( v, u, s );
}
}
void calcDecreasing( int u, int p, int t ) {
decc.push_back( u );
for ( int e: adj[u] ) {
int v = edges[e].other( u ), s = edges[e].t;
if ( v == p || isCentroid[v] || t < s )
continue;
calcDecreasing( v, u, s );
}
}
FRECV timee;
AIB lowerTimee;
void decomp( int r ) {
calcSizes( r, 0 );
sz = sizee[r];
findCentroid( r, 0 );
int c = centroid;
isCentroid[c] = true;
sort( adj[c].begin(), adj[c].end(), []( int e, int f ) {
return edges[e].t > edges[f].t;
} );
timee.clear();
lowerTimee.clear();
timee.update( c, 1 );
lowerTimee.update( 1, 1 );
for ( int e: adj[c] ) {
int v = edges[e].other( c );
if ( isCentroid[v] )
continue;
incc.clear();
decc.clear();
calcIncreasing( v, c, edges[e].t );
calcDecreasing( v, c, edges[e].t );
for ( int u: decc ) {
for ( int q: queriesQ[u] ) {
if ( edges[e].t <= queries[q].t && timee.qry( queries[q].u ) <= queries[q].t )
queries[q].ans = true;
}
for ( int q: queriesC[u] ) {
if ( edges[e].t <= queries[q].t )
queries[q].ans += lowerTimee.query( queries[q].t );
}
}
for ( auto p: incc ) {
int u = p.first, t = p.second;
timee.update( u, t );
lowerTimee.update( t, 1 );
}
}
for ( int q: queriesQ[c] ) {
if ( timee.qry( queries[q].u ) <= queries[q].t )
queries[q].ans = true;
}
for ( int q: queriesC[c] )
queries[q].ans += lowerTimee.query( queries[q].t );
for ( int e: adj[c] ) {
int v = edges[e].other( c );
if ( !isCentroid[v] )
decomp( v );
}
}
int main() {
int n, q;
cin >> n >> q;
for ( int t = 1, i = 0, j = 0; t <= n + q - 1; t++ ) {
char ch;
cin >> ch;
if ( ch == 'S' ) {
cin >> edges[i].u >> edges[i].v;
adj[edges[i].u].push_back( i );
adj[edges[i].v].push_back( i );
edges[i].t = t;
i++;
} else {
queries[j].type = ch;
if ( ch == 'Q' ) {
cin >> queries[j].u >> queries[j].v;
queriesQ[queries[j].v].push_back( j );
} else {
cin >> queries[j].v;
queriesC[queries[j].v].push_back( j );
}
queries[j].t = t;
j++;
}
}
timee.init();
decomp( 1 );
for ( int i = 0; i < q; i++ ) {
if ( queries[i].type == 'Q' )
cout << (queries[i].ans ? "yes\n" : "no\n");
else
cout << queries[i].ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14164 KB |
Output is correct |
2 |
Correct |
57 ms |
14420 KB |
Output is correct |
3 |
Correct |
56 ms |
14560 KB |
Output is correct |
4 |
Correct |
58 ms |
14672 KB |
Output is correct |
5 |
Correct |
59 ms |
14808 KB |
Output is correct |
6 |
Correct |
66 ms |
15020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14164 KB |
Output is correct |
2 |
Correct |
57 ms |
14420 KB |
Output is correct |
3 |
Correct |
56 ms |
14560 KB |
Output is correct |
4 |
Correct |
58 ms |
14672 KB |
Output is correct |
5 |
Correct |
59 ms |
14808 KB |
Output is correct |
6 |
Correct |
66 ms |
15020 KB |
Output is correct |
7 |
Correct |
40 ms |
14216 KB |
Output is correct |
8 |
Correct |
60 ms |
15764 KB |
Output is correct |
9 |
Correct |
50 ms |
15640 KB |
Output is correct |
10 |
Correct |
55 ms |
15712 KB |
Output is correct |
11 |
Correct |
57 ms |
15920 KB |
Output is correct |
12 |
Correct |
54 ms |
16076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
14428 KB |
Output is correct |
2 |
Correct |
194 ms |
31352 KB |
Output is correct |
3 |
Correct |
186 ms |
30644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
14428 KB |
Output is correct |
2 |
Correct |
194 ms |
31352 KB |
Output is correct |
3 |
Correct |
186 ms |
30644 KB |
Output is correct |
4 |
Correct |
42 ms |
14420 KB |
Output is correct |
5 |
Correct |
210 ms |
33472 KB |
Output is correct |
6 |
Correct |
118 ms |
30772 KB |
Output is correct |
7 |
Correct |
128 ms |
30652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14220 KB |
Output is correct |
2 |
Correct |
327 ms |
30292 KB |
Output is correct |
3 |
Correct |
348 ms |
30196 KB |
Output is correct |
4 |
Correct |
268 ms |
34436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14220 KB |
Output is correct |
2 |
Correct |
327 ms |
30292 KB |
Output is correct |
3 |
Correct |
348 ms |
30196 KB |
Output is correct |
4 |
Correct |
268 ms |
34436 KB |
Output is correct |
5 |
Correct |
40 ms |
14164 KB |
Output is correct |
6 |
Correct |
363 ms |
33760 KB |
Output is correct |
7 |
Correct |
279 ms |
36520 KB |
Output is correct |
8 |
Correct |
330 ms |
33108 KB |
Output is correct |
9 |
Correct |
332 ms |
33152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
14160 KB |
Output is correct |
2 |
Correct |
252 ms |
23712 KB |
Output is correct |
3 |
Correct |
269 ms |
20584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
14160 KB |
Output is correct |
2 |
Correct |
252 ms |
23712 KB |
Output is correct |
3 |
Correct |
269 ms |
20584 KB |
Output is correct |
4 |
Correct |
41 ms |
14160 KB |
Output is correct |
5 |
Correct |
274 ms |
27424 KB |
Output is correct |
6 |
Correct |
301 ms |
24216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
14160 KB |
Output is correct |
2 |
Correct |
357 ms |
30368 KB |
Output is correct |
3 |
Correct |
344 ms |
30188 KB |
Output is correct |
4 |
Correct |
275 ms |
34948 KB |
Output is correct |
5 |
Correct |
41 ms |
14256 KB |
Output is correct |
6 |
Correct |
272 ms |
23896 KB |
Output is correct |
7 |
Correct |
297 ms |
20704 KB |
Output is correct |
8 |
Correct |
283 ms |
21584 KB |
Output is correct |
9 |
Correct |
270 ms |
21328 KB |
Output is correct |
10 |
Correct |
336 ms |
25516 KB |
Output is correct |
11 |
Correct |
367 ms |
25144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
14160 KB |
Output is correct |
2 |
Correct |
357 ms |
30368 KB |
Output is correct |
3 |
Correct |
344 ms |
30188 KB |
Output is correct |
4 |
Correct |
275 ms |
34948 KB |
Output is correct |
5 |
Correct |
41 ms |
14256 KB |
Output is correct |
6 |
Correct |
272 ms |
23896 KB |
Output is correct |
7 |
Correct |
297 ms |
20704 KB |
Output is correct |
8 |
Correct |
283 ms |
21584 KB |
Output is correct |
9 |
Correct |
270 ms |
21328 KB |
Output is correct |
10 |
Correct |
336 ms |
25516 KB |
Output is correct |
11 |
Correct |
367 ms |
25144 KB |
Output is correct |
12 |
Correct |
40 ms |
14160 KB |
Output is correct |
13 |
Correct |
352 ms |
33944 KB |
Output is correct |
14 |
Correct |
280 ms |
36352 KB |
Output is correct |
15 |
Correct |
318 ms |
33164 KB |
Output is correct |
16 |
Correct |
326 ms |
33224 KB |
Output is correct |
17 |
Correct |
40 ms |
15216 KB |
Output is correct |
18 |
Correct |
264 ms |
27232 KB |
Output is correct |
19 |
Correct |
262 ms |
24368 KB |
Output is correct |
20 |
Correct |
320 ms |
24916 KB |
Output is correct |
21 |
Correct |
271 ms |
25072 KB |
Output is correct |
22 |
Correct |
327 ms |
27748 KB |
Output is correct |
23 |
Correct |
431 ms |
30184 KB |
Output is correct |
24 |
Correct |
380 ms |
30084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14160 KB |
Output is correct |
2 |
Correct |
57 ms |
14416 KB |
Output is correct |
3 |
Correct |
56 ms |
14416 KB |
Output is correct |
4 |
Correct |
58 ms |
14676 KB |
Output is correct |
5 |
Correct |
74 ms |
14676 KB |
Output is correct |
6 |
Correct |
56 ms |
14836 KB |
Output is correct |
7 |
Correct |
41 ms |
14400 KB |
Output is correct |
8 |
Correct |
176 ms |
31204 KB |
Output is correct |
9 |
Correct |
182 ms |
30656 KB |
Output is correct |
10 |
Correct |
40 ms |
14164 KB |
Output is correct |
11 |
Correct |
321 ms |
30280 KB |
Output is correct |
12 |
Correct |
363 ms |
30292 KB |
Output is correct |
13 |
Correct |
290 ms |
36228 KB |
Output is correct |
14 |
Correct |
41 ms |
14164 KB |
Output is correct |
15 |
Correct |
279 ms |
23756 KB |
Output is correct |
16 |
Correct |
340 ms |
20632 KB |
Output is correct |
17 |
Correct |
274 ms |
21556 KB |
Output is correct |
18 |
Correct |
325 ms |
21200 KB |
Output is correct |
19 |
Correct |
367 ms |
25388 KB |
Output is correct |
20 |
Correct |
334 ms |
24888 KB |
Output is correct |
21 |
Correct |
216 ms |
27672 KB |
Output is correct |
22 |
Correct |
255 ms |
27072 KB |
Output is correct |
23 |
Correct |
233 ms |
21748 KB |
Output is correct |
24 |
Correct |
227 ms |
21532 KB |
Output is correct |
25 |
Correct |
347 ms |
31076 KB |
Output is correct |
26 |
Correct |
282 ms |
20176 KB |
Output is correct |
27 |
Correct |
277 ms |
19924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14160 KB |
Output is correct |
2 |
Correct |
57 ms |
14416 KB |
Output is correct |
3 |
Correct |
56 ms |
14416 KB |
Output is correct |
4 |
Correct |
58 ms |
14676 KB |
Output is correct |
5 |
Correct |
74 ms |
14676 KB |
Output is correct |
6 |
Correct |
56 ms |
14836 KB |
Output is correct |
7 |
Correct |
41 ms |
14400 KB |
Output is correct |
8 |
Correct |
176 ms |
31204 KB |
Output is correct |
9 |
Correct |
182 ms |
30656 KB |
Output is correct |
10 |
Correct |
40 ms |
14164 KB |
Output is correct |
11 |
Correct |
321 ms |
30280 KB |
Output is correct |
12 |
Correct |
363 ms |
30292 KB |
Output is correct |
13 |
Correct |
290 ms |
36228 KB |
Output is correct |
14 |
Correct |
41 ms |
14164 KB |
Output is correct |
15 |
Correct |
279 ms |
23756 KB |
Output is correct |
16 |
Correct |
340 ms |
20632 KB |
Output is correct |
17 |
Correct |
274 ms |
21556 KB |
Output is correct |
18 |
Correct |
325 ms |
21200 KB |
Output is correct |
19 |
Correct |
367 ms |
25388 KB |
Output is correct |
20 |
Correct |
334 ms |
24888 KB |
Output is correct |
21 |
Correct |
216 ms |
27672 KB |
Output is correct |
22 |
Correct |
255 ms |
27072 KB |
Output is correct |
23 |
Correct |
233 ms |
21748 KB |
Output is correct |
24 |
Correct |
227 ms |
21532 KB |
Output is correct |
25 |
Correct |
347 ms |
31076 KB |
Output is correct |
26 |
Correct |
282 ms |
20176 KB |
Output is correct |
27 |
Correct |
277 ms |
19924 KB |
Output is correct |
28 |
Correct |
44 ms |
14160 KB |
Output is correct |
29 |
Correct |
55 ms |
15696 KB |
Output is correct |
30 |
Correct |
60 ms |
15732 KB |
Output is correct |
31 |
Correct |
57 ms |
15724 KB |
Output is correct |
32 |
Correct |
57 ms |
15956 KB |
Output is correct |
33 |
Correct |
54 ms |
15856 KB |
Output is correct |
34 |
Correct |
40 ms |
15188 KB |
Output is correct |
35 |
Correct |
185 ms |
34436 KB |
Output is correct |
36 |
Correct |
123 ms |
31328 KB |
Output is correct |
37 |
Correct |
131 ms |
30612 KB |
Output is correct |
38 |
Correct |
40 ms |
15220 KB |
Output is correct |
39 |
Correct |
329 ms |
33876 KB |
Output is correct |
40 |
Correct |
284 ms |
36480 KB |
Output is correct |
41 |
Correct |
312 ms |
33108 KB |
Output is correct |
42 |
Correct |
325 ms |
33152 KB |
Output is correct |
43 |
Correct |
48 ms |
15096 KB |
Output is correct |
44 |
Correct |
276 ms |
27436 KB |
Output is correct |
45 |
Correct |
263 ms |
24436 KB |
Output is correct |
46 |
Correct |
298 ms |
24912 KB |
Output is correct |
47 |
Correct |
279 ms |
24896 KB |
Output is correct |
48 |
Correct |
325 ms |
27872 KB |
Output is correct |
49 |
Correct |
430 ms |
30052 KB |
Output is correct |
50 |
Correct |
398 ms |
30088 KB |
Output is correct |
51 |
Correct |
166 ms |
33908 KB |
Output is correct |
52 |
Correct |
146 ms |
32592 KB |
Output is correct |
53 |
Correct |
139 ms |
32004 KB |
Output is correct |
54 |
Correct |
154 ms |
33140 KB |
Output is correct |
55 |
Correct |
147 ms |
28728 KB |
Output is correct |
56 |
Correct |
214 ms |
24048 KB |
Output is correct |
57 |
Correct |
279 ms |
31552 KB |
Output is correct |
58 |
Correct |
351 ms |
24492 KB |
Output is correct |
59 |
Correct |
277 ms |
23476 KB |
Output is correct |