#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define lli long long
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define MAX 240000
#define costo first
#define des second
struct x {
lli tipo;
lli des;
lli tiempo;
lli id;
};
lli n,qq,a,b,cent,cont,t;
lli visitados[MAX+2],tam[MAX+2],llego[MAX+2],bit[MAX+2];
char c;
queue<lli> cola;
vector<pair<lli,lli> > hijos[MAX+2];
vector<x> querys[MAX+2];
pair<lli,lli> res[MAX+2];
void actualiza(lli pos,lli val) {
while (pos <= t) {
bit[pos] += val;
pos += pos&(-pos);
}
}
lli sum(lli pos) {
lli r = 0;
while (pos > 0) {
r += bit[pos];
pos -= pos&(-pos);
}
return r;
}
lli buscaCen (lli pos, lli padre, lli mitad) {
for (auto h : hijos[pos]){
if (h.des == padre || visitados[h.des] == 1) continue;
if (tam[h.des] > mitad) return buscaCen(h.des, pos, mitad);
}
return pos;
}
void DFS(lli pos, lli padre) {
tam[pos] = 1;
for (auto h : hijos[pos]) {
if (h.des == padre || visitados[h.des] == 1) continue;
DFS(h.des, pos);
tam[pos] += tam[h.des];
}
}
void ami(lli pos, lli padre, lli ant, vector<lli> &nuevos) {
nuevos.push_back(pos);
for (auto h : hijos[pos]) {
if (h.des == padre || visitados[h.des] == 1) continue;
if (h.costo > ant) continue;
ami(h.des,pos,h.costo,nuevos);
}
}
void aellos(lli pos, lli padre, lli ant, vector<pair<lli,lli> > & acc) {
acc.push_back({pos,ant});
for (auto h : hijos[pos]) {
if (h.des == padre || visitados[h.des] == 1) continue;
if (h.costo < ant) continue;
aellos(h.des,pos,h.costo,acc);
}
}
void solve() {
lli act;
while (!cola.empty()) {
act = cola.front();
cola.pop();
DFS(act,0);
cent = buscaCen(act,0,tam[act]/2);
sort(hijos[cent].begin(), hijos[cent].end());
reverse(hijos[cent].begin(),hijos[cent].end());
vector < pair<lli,lli> > deshacer;
for (auto h : hijos[cent]) {
if (visitados[h.des] == 1) continue;
vector<lli> nuevos;
ami(h.des,cent,h.costo,nuevos);
for (auto nodo : nuevos) {
for (auto q : querys[nodo]) {
if (q.tipo == 1) {
if (q.des == cent && q.tiempo > h.costo) res[q.id].second = 1;
else if (llego[q.des] > 0 && llego[q.des] < q.tiempo) res[q.id].second = 1;
}
else {
res[q.id].second += sum(q.tiempo);
if (h.costo < q.tiempo) res[q.id].second++;
}
}
}
vector< pair<lli,lli> > accesibles;
aellos(h.des,cent,h.costo,accesibles);
for ( auto nodo : accesibles) {
deshacer.push_back(nodo);
llego[nodo.first] = nodo.second;
actualiza(nodo.second,1);
}
}
for (auto q : querys[cent]) {
if (q.tipo == 1) {
if (llego[q.des] > 0 && llego[q.des] < q.tiempo) res[q.id].second = 1;
}
else {
res[q.id].second += sum(q.tiempo);
}
}
for (auto borr : deshacer) {
llego[borr.first] = 0;
actualiza(borr.second,-1);
}
visitados[cent] = 1;
for (auto h : hijos[cent]) {
if (visitados[h.des] == 1) continue;
cola.push(h.des);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> qq;
t = n+qq-1;
cont = 1;
rep(i,1,t){
cin >> c;
if (c == 'S') {
cin >> a >> b;
hijos[a].push_back({i,b});
hijos[b].push_back({i,a});
}
else if (c == 'Q') {
cin >> a >> b;
querys[b].push_back({1,a,i,cont});
res[cont].first = 1;
if (a == b) res[cont].second = 1;
cont++;
}
else {
cin >> a;
querys[a].push_back({2,0,i,cont});
res[cont].first = 2;
cont++;
}
}
cola.push(1);
solve();
cont--;
rep(i,1,cont) {
if (res[i].first == 2) cout << res[i].second+1 << "\n";
else {
if (res[i].second == 1) cout << "yes\n";
else cout << "no\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19776 KB |
Output is correct |
2 |
Correct |
56 ms |
22784 KB |
Output is correct |
3 |
Correct |
48 ms |
21804 KB |
Output is correct |
4 |
Correct |
54 ms |
22852 KB |
Output is correct |
5 |
Correct |
52 ms |
22272 KB |
Output is correct |
6 |
Correct |
51 ms |
22212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19776 KB |
Output is correct |
2 |
Correct |
56 ms |
22784 KB |
Output is correct |
3 |
Correct |
48 ms |
21804 KB |
Output is correct |
4 |
Correct |
54 ms |
22852 KB |
Output is correct |
5 |
Correct |
52 ms |
22272 KB |
Output is correct |
6 |
Correct |
51 ms |
22212 KB |
Output is correct |
7 |
Correct |
38 ms |
20540 KB |
Output is correct |
8 |
Correct |
58 ms |
22412 KB |
Output is correct |
9 |
Correct |
50 ms |
21152 KB |
Output is correct |
10 |
Correct |
58 ms |
22468 KB |
Output is correct |
11 |
Correct |
55 ms |
21828 KB |
Output is correct |
12 |
Correct |
49 ms |
20708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19776 KB |
Output is correct |
2 |
Correct |
198 ms |
31016 KB |
Output is correct |
3 |
Correct |
200 ms |
33784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19776 KB |
Output is correct |
2 |
Correct |
198 ms |
31016 KB |
Output is correct |
3 |
Correct |
200 ms |
33784 KB |
Output is correct |
4 |
Correct |
41 ms |
20644 KB |
Output is correct |
5 |
Correct |
195 ms |
33460 KB |
Output is correct |
6 |
Correct |
132 ms |
33812 KB |
Output is correct |
7 |
Correct |
137 ms |
31396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19784 KB |
Output is correct |
2 |
Correct |
431 ms |
38248 KB |
Output is correct |
3 |
Correct |
453 ms |
41500 KB |
Output is correct |
4 |
Correct |
359 ms |
41612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19784 KB |
Output is correct |
2 |
Correct |
431 ms |
38248 KB |
Output is correct |
3 |
Correct |
453 ms |
41500 KB |
Output is correct |
4 |
Correct |
359 ms |
41612 KB |
Output is correct |
5 |
Correct |
38 ms |
20544 KB |
Output is correct |
6 |
Correct |
459 ms |
41016 KB |
Output is correct |
7 |
Correct |
428 ms |
41452 KB |
Output is correct |
8 |
Correct |
464 ms |
40660 KB |
Output is correct |
9 |
Correct |
460 ms |
40732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19776 KB |
Output is correct |
2 |
Correct |
342 ms |
30908 KB |
Output is correct |
3 |
Correct |
373 ms |
34628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19776 KB |
Output is correct |
2 |
Correct |
342 ms |
30908 KB |
Output is correct |
3 |
Correct |
373 ms |
34628 KB |
Output is correct |
4 |
Correct |
39 ms |
20604 KB |
Output is correct |
5 |
Correct |
350 ms |
33796 KB |
Output is correct |
6 |
Correct |
414 ms |
34156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
19776 KB |
Output is correct |
2 |
Correct |
422 ms |
38208 KB |
Output is correct |
3 |
Correct |
424 ms |
41412 KB |
Output is correct |
4 |
Correct |
354 ms |
41764 KB |
Output is correct |
5 |
Correct |
37 ms |
20704 KB |
Output is correct |
6 |
Correct |
314 ms |
34156 KB |
Output is correct |
7 |
Correct |
372 ms |
34592 KB |
Output is correct |
8 |
Correct |
365 ms |
33948 KB |
Output is correct |
9 |
Correct |
391 ms |
34892 KB |
Output is correct |
10 |
Correct |
542 ms |
39160 KB |
Output is correct |
11 |
Correct |
503 ms |
37976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
19776 KB |
Output is correct |
2 |
Correct |
422 ms |
38208 KB |
Output is correct |
3 |
Correct |
424 ms |
41412 KB |
Output is correct |
4 |
Correct |
354 ms |
41764 KB |
Output is correct |
5 |
Correct |
37 ms |
20704 KB |
Output is correct |
6 |
Correct |
314 ms |
34156 KB |
Output is correct |
7 |
Correct |
372 ms |
34592 KB |
Output is correct |
8 |
Correct |
365 ms |
33948 KB |
Output is correct |
9 |
Correct |
391 ms |
34892 KB |
Output is correct |
10 |
Correct |
542 ms |
39160 KB |
Output is correct |
11 |
Correct |
503 ms |
37976 KB |
Output is correct |
12 |
Correct |
37 ms |
20604 KB |
Output is correct |
13 |
Correct |
452 ms |
41148 KB |
Output is correct |
14 |
Correct |
422 ms |
41432 KB |
Output is correct |
15 |
Correct |
447 ms |
40644 KB |
Output is correct |
16 |
Correct |
471 ms |
40700 KB |
Output is correct |
17 |
Correct |
38 ms |
20612 KB |
Output is correct |
18 |
Correct |
358 ms |
33904 KB |
Output is correct |
19 |
Correct |
390 ms |
34116 KB |
Output is correct |
20 |
Correct |
386 ms |
33476 KB |
Output is correct |
21 |
Correct |
411 ms |
34500 KB |
Output is correct |
22 |
Correct |
682 ms |
38080 KB |
Output is correct |
23 |
Correct |
732 ms |
37452 KB |
Output is correct |
24 |
Correct |
638 ms |
38056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19972 KB |
Output is correct |
2 |
Correct |
51 ms |
22744 KB |
Output is correct |
3 |
Correct |
49 ms |
21804 KB |
Output is correct |
4 |
Correct |
54 ms |
22856 KB |
Output is correct |
5 |
Correct |
51 ms |
22212 KB |
Output is correct |
6 |
Correct |
50 ms |
22232 KB |
Output is correct |
7 |
Correct |
42 ms |
20720 KB |
Output is correct |
8 |
Correct |
194 ms |
30964 KB |
Output is correct |
9 |
Correct |
198 ms |
33792 KB |
Output is correct |
10 |
Correct |
37 ms |
20704 KB |
Output is correct |
11 |
Correct |
439 ms |
41488 KB |
Output is correct |
12 |
Correct |
434 ms |
41484 KB |
Output is correct |
13 |
Correct |
357 ms |
41608 KB |
Output is correct |
14 |
Correct |
37 ms |
20672 KB |
Output is correct |
15 |
Correct |
310 ms |
34152 KB |
Output is correct |
16 |
Correct |
362 ms |
34556 KB |
Output is correct |
17 |
Correct |
364 ms |
33988 KB |
Output is correct |
18 |
Correct |
380 ms |
34928 KB |
Output is correct |
19 |
Correct |
575 ms |
39084 KB |
Output is correct |
20 |
Correct |
502 ms |
38032 KB |
Output is correct |
21 |
Correct |
233 ms |
34992 KB |
Output is correct |
22 |
Correct |
228 ms |
35088 KB |
Output is correct |
23 |
Correct |
293 ms |
33852 KB |
Output is correct |
24 |
Correct |
288 ms |
33884 KB |
Output is correct |
25 |
Correct |
440 ms |
37268 KB |
Output is correct |
26 |
Correct |
410 ms |
33852 KB |
Output is correct |
27 |
Correct |
385 ms |
34792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
19972 KB |
Output is correct |
2 |
Correct |
51 ms |
22744 KB |
Output is correct |
3 |
Correct |
49 ms |
21804 KB |
Output is correct |
4 |
Correct |
54 ms |
22856 KB |
Output is correct |
5 |
Correct |
51 ms |
22212 KB |
Output is correct |
6 |
Correct |
50 ms |
22232 KB |
Output is correct |
7 |
Correct |
42 ms |
20720 KB |
Output is correct |
8 |
Correct |
194 ms |
30964 KB |
Output is correct |
9 |
Correct |
198 ms |
33792 KB |
Output is correct |
10 |
Correct |
37 ms |
20704 KB |
Output is correct |
11 |
Correct |
439 ms |
41488 KB |
Output is correct |
12 |
Correct |
434 ms |
41484 KB |
Output is correct |
13 |
Correct |
357 ms |
41608 KB |
Output is correct |
14 |
Correct |
37 ms |
20672 KB |
Output is correct |
15 |
Correct |
310 ms |
34152 KB |
Output is correct |
16 |
Correct |
362 ms |
34556 KB |
Output is correct |
17 |
Correct |
364 ms |
33988 KB |
Output is correct |
18 |
Correct |
380 ms |
34928 KB |
Output is correct |
19 |
Correct |
575 ms |
39084 KB |
Output is correct |
20 |
Correct |
502 ms |
38032 KB |
Output is correct |
21 |
Correct |
233 ms |
34992 KB |
Output is correct |
22 |
Correct |
228 ms |
35088 KB |
Output is correct |
23 |
Correct |
293 ms |
33852 KB |
Output is correct |
24 |
Correct |
288 ms |
33884 KB |
Output is correct |
25 |
Correct |
440 ms |
37268 KB |
Output is correct |
26 |
Correct |
410 ms |
33852 KB |
Output is correct |
27 |
Correct |
385 ms |
34792 KB |
Output is correct |
28 |
Correct |
37 ms |
20664 KB |
Output is correct |
29 |
Correct |
56 ms |
22420 KB |
Output is correct |
30 |
Correct |
49 ms |
21172 KB |
Output is correct |
31 |
Correct |
62 ms |
22480 KB |
Output is correct |
32 |
Correct |
63 ms |
21904 KB |
Output is correct |
33 |
Correct |
49 ms |
20776 KB |
Output is correct |
34 |
Correct |
37 ms |
20672 KB |
Output is correct |
35 |
Correct |
192 ms |
33364 KB |
Output is correct |
36 |
Correct |
128 ms |
33900 KB |
Output is correct |
37 |
Correct |
133 ms |
31408 KB |
Output is correct |
38 |
Correct |
38 ms |
20544 KB |
Output is correct |
39 |
Correct |
450 ms |
41240 KB |
Output is correct |
40 |
Correct |
416 ms |
41344 KB |
Output is correct |
41 |
Correct |
448 ms |
40728 KB |
Output is correct |
42 |
Correct |
467 ms |
40776 KB |
Output is correct |
43 |
Correct |
45 ms |
20544 KB |
Output is correct |
44 |
Correct |
356 ms |
33868 KB |
Output is correct |
45 |
Correct |
383 ms |
34196 KB |
Output is correct |
46 |
Correct |
401 ms |
33476 KB |
Output is correct |
47 |
Correct |
409 ms |
34464 KB |
Output is correct |
48 |
Correct |
715 ms |
37956 KB |
Output is correct |
49 |
Correct |
732 ms |
37488 KB |
Output is correct |
50 |
Correct |
671 ms |
38064 KB |
Output is correct |
51 |
Correct |
167 ms |
34552 KB |
Output is correct |
52 |
Correct |
159 ms |
32112 KB |
Output is correct |
53 |
Correct |
162 ms |
31812 KB |
Output is correct |
54 |
Correct |
145 ms |
35048 KB |
Output is correct |
55 |
Correct |
145 ms |
33408 KB |
Output is correct |
56 |
Correct |
269 ms |
32944 KB |
Output is correct |
57 |
Correct |
338 ms |
36732 KB |
Output is correct |
58 |
Correct |
560 ms |
33052 KB |
Output is correct |
59 |
Correct |
392 ms |
34508 KB |
Output is correct |