#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 120000
#define des first
#define val second
struct x{
lli prof;
lli creciente;
lli decreciente;
};
lli n,q,a,b,ta,tb,cambio,k,w,c;
lli padres[19][MAX+2],tam[MAX+2],pas[MAX+2];
vector< pair<lli,lli> > hijos[MAX+2];
x nodos[MAX+2];
vector<x> querys;
char tipo;
void une(lli pos, lli padre, lli p) {
lli nuevo,cont = 1;
padres[0][pos] = padre;
nuevo = padre;
nodos[pos].prof = p;
while (padres[cont-1][nuevo] != 0) {
padres[cont][pos] = padres[cont-1][nuevo];
nuevo = padres[cont][pos];
cont++;
}
for (auto h : hijos[pos]){
if (h.des == padre) continue;
if (h.val > pas[pos]) {
nodos[h.des].creciente = nodos[pos].creciente + 1;
nodos[h.des].decreciente = 1;
}
else {
nodos[h.des].decreciente = nodos[pos].decreciente + 1;
nodos[h.des].creciente = 1;
}
pas[h.des] = h.val;
une(h.des,pos,p+1);
}
}
lli lift(lli pp, lli dis) {
lli cont = 0;
while (dis > 0) {
if (dis&1) pp = padres[cont][pp];
cont++;
dis /= 2;
}
return pp;
}
lli LCA(lli x, lli y) {
repa(i,17,0) {
if (padres[i][x] != padres[i][y]) {
y = padres[i][y];
x = padres[i][x];
}
}
ta = x;
tb = y;
return padres[0][y];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
q += n-1;
rep(T,1,q) {
cin >> tipo;
if (tipo == 'S') {
cin >> a >> b;
hijos[a].push_back({b,T});
hijos[b].push_back({a,T});
}
else if (tipo == 'Q') {
cin >> a >> b;
querys.push_back({T,a,b});
}
else if (tipo == 'C') {
cin >> a;
//vacio
}
}
pas[1] = 0;
une(1,0,1);
//rep(i,1,n) {
// cout << "Nodo " << i << endl;
// debugsl(nodos[i].creciente);
// debugsl(nodos[i].decreciente);
// debugsl(pas[i]);
// debug(nodos[i].prof);
//}
for (auto act : querys) {
a = act.creciente;
b = act.decreciente;
w = act.prof;
if (a == b) {cout << "yes\n"; continue;}
cambio = true;
if (nodos[a].prof > nodos[b].prof) {swap(a,b); cambio = false;}
c = lift(b ,nodos[b].prof-nodos[a].prof);
if (c != a) c = LCA(a,c);
if (cambio) {
k = nodos[b].prof - nodos[b].decreciente;
if (k > nodos[c].prof) {cout << "no\n"; continue;}
k = nodos[a].prof - nodos[a].creciente;
if (k > nodos[c].prof) {cout << "no\n"; continue;}
if (a == c){
c = lift(b ,nodos[b].prof-nodos[a].prof-1);
if (pas[c] > w) {cout << "no\n"; continue;}
}
else {
if (pas[a] > w) {cout << "no\n"; continue;}
if (pas[tb] > pas[ta]) {cout << "no\n"; continue;}
}
cout << "yes\n";
}
else {
k = nodos[a].prof - nodos[a].decreciente;
if (k > nodos[c].prof) {cout << "no\n"; continue;}
k = nodos[b].prof - nodos[b].creciente;
if (k > nodos[c].prof) {cout << "no\n"; continue;}
if (pas[b] > w) {cout << "no\n"; continue;}
if (c != a && pas[ta] > pas[tb]) {cout << "no\n"; continue;}
cout << "yes\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
6448 KB |
Output is correct |
2 |
Correct |
61 ms |
8148 KB |
Output is correct |
3 |
Correct |
60 ms |
8284 KB |
Output is correct |
4 |
Correct |
64 ms |
8504 KB |
Output is correct |
5 |
Correct |
46 ms |
8864 KB |
Output is correct |
6 |
Correct |
60 ms |
8132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
6448 KB |
Output is correct |
2 |
Correct |
61 ms |
8148 KB |
Output is correct |
3 |
Correct |
60 ms |
8284 KB |
Output is correct |
4 |
Correct |
64 ms |
8504 KB |
Output is correct |
5 |
Correct |
46 ms |
8864 KB |
Output is correct |
6 |
Correct |
60 ms |
8132 KB |
Output is correct |
7 |
Incorrect |
41 ms |
7136 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6400 KB |
Output is correct |
2 |
Correct |
160 ms |
19520 KB |
Output is correct |
3 |
Correct |
157 ms |
19520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
6400 KB |
Output is correct |
2 |
Correct |
160 ms |
19520 KB |
Output is correct |
3 |
Correct |
157 ms |
19520 KB |
Output is correct |
4 |
Incorrect |
38 ms |
7124 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
6480 KB |
Output is correct |
2 |
Correct |
139 ms |
40708 KB |
Output is correct |
3 |
Correct |
137 ms |
40712 KB |
Output is correct |
4 |
Correct |
155 ms |
40096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
6480 KB |
Output is correct |
2 |
Correct |
139 ms |
40708 KB |
Output is correct |
3 |
Correct |
137 ms |
40712 KB |
Output is correct |
4 |
Correct |
155 ms |
40096 KB |
Output is correct |
5 |
Incorrect |
32 ms |
6376 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
6460 KB |
Output is correct |
2 |
Correct |
162 ms |
24496 KB |
Output is correct |
3 |
Correct |
145 ms |
24688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
6460 KB |
Output is correct |
2 |
Correct |
162 ms |
24496 KB |
Output is correct |
3 |
Correct |
145 ms |
24688 KB |
Output is correct |
4 |
Incorrect |
37 ms |
7116 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
6404 KB |
Output is correct |
2 |
Correct |
141 ms |
40716 KB |
Output is correct |
3 |
Correct |
142 ms |
40684 KB |
Output is correct |
4 |
Correct |
161 ms |
40268 KB |
Output is correct |
5 |
Correct |
38 ms |
6480 KB |
Output is correct |
6 |
Correct |
152 ms |
24452 KB |
Output is correct |
7 |
Correct |
160 ms |
24744 KB |
Output is correct |
8 |
Correct |
300 ms |
30680 KB |
Output is correct |
9 |
Correct |
242 ms |
29728 KB |
Output is correct |
10 |
Correct |
250 ms |
40376 KB |
Output is correct |
11 |
Correct |
364 ms |
39712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
6404 KB |
Output is correct |
2 |
Correct |
141 ms |
40716 KB |
Output is correct |
3 |
Correct |
142 ms |
40684 KB |
Output is correct |
4 |
Correct |
161 ms |
40268 KB |
Output is correct |
5 |
Correct |
38 ms |
6480 KB |
Output is correct |
6 |
Correct |
152 ms |
24452 KB |
Output is correct |
7 |
Correct |
160 ms |
24744 KB |
Output is correct |
8 |
Correct |
300 ms |
30680 KB |
Output is correct |
9 |
Correct |
242 ms |
29728 KB |
Output is correct |
10 |
Correct |
250 ms |
40376 KB |
Output is correct |
11 |
Correct |
364 ms |
39712 KB |
Output is correct |
12 |
Incorrect |
33 ms |
7120 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6452 KB |
Output is correct |
2 |
Correct |
63 ms |
8208 KB |
Output is correct |
3 |
Correct |
59 ms |
8236 KB |
Output is correct |
4 |
Correct |
64 ms |
8572 KB |
Output is correct |
5 |
Correct |
46 ms |
8880 KB |
Output is correct |
6 |
Correct |
60 ms |
8160 KB |
Output is correct |
7 |
Correct |
39 ms |
7332 KB |
Output is correct |
8 |
Correct |
169 ms |
19544 KB |
Output is correct |
9 |
Correct |
159 ms |
19524 KB |
Output is correct |
10 |
Correct |
35 ms |
7324 KB |
Output is correct |
11 |
Correct |
145 ms |
44012 KB |
Output is correct |
12 |
Correct |
142 ms |
44032 KB |
Output is correct |
13 |
Correct |
167 ms |
43376 KB |
Output is correct |
14 |
Correct |
37 ms |
7380 KB |
Output is correct |
15 |
Correct |
157 ms |
24424 KB |
Output is correct |
16 |
Correct |
147 ms |
24688 KB |
Output is correct |
17 |
Correct |
291 ms |
30756 KB |
Output is correct |
18 |
Correct |
232 ms |
29824 KB |
Output is correct |
19 |
Correct |
245 ms |
40380 KB |
Output is correct |
20 |
Correct |
400 ms |
39656 KB |
Output is correct |
21 |
Correct |
164 ms |
21064 KB |
Output is correct |
22 |
Correct |
169 ms |
21916 KB |
Output is correct |
23 |
Correct |
219 ms |
25520 KB |
Output is correct |
24 |
Correct |
245 ms |
26592 KB |
Output is correct |
25 |
Correct |
299 ms |
36956 KB |
Output is correct |
26 |
Correct |
143 ms |
24848 KB |
Output is correct |
27 |
Correct |
134 ms |
24500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
6452 KB |
Output is correct |
2 |
Correct |
63 ms |
8208 KB |
Output is correct |
3 |
Correct |
59 ms |
8236 KB |
Output is correct |
4 |
Correct |
64 ms |
8572 KB |
Output is correct |
5 |
Correct |
46 ms |
8880 KB |
Output is correct |
6 |
Correct |
60 ms |
8160 KB |
Output is correct |
7 |
Correct |
39 ms |
7332 KB |
Output is correct |
8 |
Correct |
169 ms |
19544 KB |
Output is correct |
9 |
Correct |
159 ms |
19524 KB |
Output is correct |
10 |
Correct |
35 ms |
7324 KB |
Output is correct |
11 |
Correct |
145 ms |
44012 KB |
Output is correct |
12 |
Correct |
142 ms |
44032 KB |
Output is correct |
13 |
Correct |
167 ms |
43376 KB |
Output is correct |
14 |
Correct |
37 ms |
7380 KB |
Output is correct |
15 |
Correct |
157 ms |
24424 KB |
Output is correct |
16 |
Correct |
147 ms |
24688 KB |
Output is correct |
17 |
Correct |
291 ms |
30756 KB |
Output is correct |
18 |
Correct |
232 ms |
29824 KB |
Output is correct |
19 |
Correct |
245 ms |
40380 KB |
Output is correct |
20 |
Correct |
400 ms |
39656 KB |
Output is correct |
21 |
Correct |
164 ms |
21064 KB |
Output is correct |
22 |
Correct |
169 ms |
21916 KB |
Output is correct |
23 |
Correct |
219 ms |
25520 KB |
Output is correct |
24 |
Correct |
245 ms |
26592 KB |
Output is correct |
25 |
Correct |
299 ms |
36956 KB |
Output is correct |
26 |
Correct |
143 ms |
24848 KB |
Output is correct |
27 |
Correct |
134 ms |
24500 KB |
Output is correct |
28 |
Incorrect |
41 ms |
7212 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |