#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 120001;
const int K = 18;
struct query{
char c;
int x, y;
};
int n, k, w[N], dep[N], p[N][K], s[N][K], dsu[N];
vector <query> a;
vector <pair <int, int> > v[N];
int lift(int node, int k){
for(int i = 0 ; i < K ; i++){
if((k >> i) & 1){
node = p[node][i];
}
}
return node;
}
int LCA(int a, int b){
if(dep[b] < dep[a]) swap(a, b);
b = lift(b, dep[b] - dep[a]);
if(a == b) return a;
for(int i = K - 1 ; i >= 0 ; i--){
if(p[a][i] != p[b][i]){
a = p[a][i];
b = p[b][i];
}
}
return p[a][0];
}
int calc_s(int node, int k){
int ret = 0;
for(int i = 0 ; i < K ; i++){
if((k >> i) & 1){
ret += s[node][i];
node = p[node][i];
}
}
return ret;
}
void dfs(int node, int pnode){
s[node][0] = w[node] < w[p[node][0]];
for(int i = 1 ; i < K ; i++){
p[node][i] = p[p[node][i - 1]][i - 1];
s[node][i] = s[node][i - 1] + s[p[node][i - 1]][i - 1];
}
for(auto &i : v[node]){
if(i.first == pnode) continue;
w[i.first] = i.second;
p[i.first][0] = node;
dep[i.first] = dep[node] + 1;
dfs(i.first, node);
}
}
int find(int x){
if(dsu[x] == x) return x;
return dsu[x] = find(dsu[x]);
}
void merge(int x, int y){
x = find(x);
y = find(y);
dsu[x] = y;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
a.resize(n + k - 1);
for(auto &i : a){
cin >> i.c >> i.x;
if(i.c == 'S' || i.c == 'Q'){
cin >> i.y;
}
}
int ed = 0;
for(auto &i : a){
if(i.c == 'S'){
v[i.x].push_back(make_pair(i.y, ed));
v[i.y].push_back(make_pair(i.x, ed));
ed++;
}
}
dfs(1, 0);
for(int i = 1 ; i <= n ; i++){
dsu[i] = i;
}
for(auto &i : a){
if(i.c == 'S'){
merge(i.x, i.y);
}
else if(i.c == 'Q'){
if(find(i.x) != find(i.y)){
cout << "no\n";
continue;
}
int lca = LCA(i.x, i.y);
bool ok = 1;
if(i.x != lca && calc_s(i.x, dep[i.x] - dep[lca] - 1) > 0) ok = 0;
if(i.y != lca && calc_s(i.y, dep[i.y] - dep[lca] - 1) != dep[i.y] - dep[lca] - 1) ok = 0;
if(i.x != lca && i.y != lca && w[lift(i.x, dep[i.x] - dep[lca] - 1)] < w[lift(i.y, dep[i.y] - dep[lca] - 1)]) ok = 0;
if(ok) cout << "yes\n";
else cout << "no\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
4944 KB |
Output is correct |
2 |
Correct |
37 ms |
7108 KB |
Output is correct |
3 |
Correct |
55 ms |
7156 KB |
Output is correct |
4 |
Correct |
33 ms |
7168 KB |
Output is correct |
5 |
Correct |
53 ms |
7364 KB |
Output is correct |
6 |
Correct |
54 ms |
7164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
4944 KB |
Output is correct |
2 |
Correct |
37 ms |
7108 KB |
Output is correct |
3 |
Correct |
55 ms |
7156 KB |
Output is correct |
4 |
Correct |
33 ms |
7168 KB |
Output is correct |
5 |
Correct |
53 ms |
7364 KB |
Output is correct |
6 |
Correct |
54 ms |
7164 KB |
Output is correct |
7 |
Incorrect |
29 ms |
5704 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
4948 KB |
Output is correct |
2 |
Correct |
170 ms |
29272 KB |
Output is correct |
3 |
Correct |
161 ms |
29312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
4948 KB |
Output is correct |
2 |
Correct |
170 ms |
29272 KB |
Output is correct |
3 |
Correct |
161 ms |
29312 KB |
Output is correct |
4 |
Incorrect |
33 ms |
4944 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4940 KB |
Output is correct |
2 |
Correct |
148 ms |
41168 KB |
Output is correct |
3 |
Correct |
149 ms |
41156 KB |
Output is correct |
4 |
Correct |
156 ms |
41072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
4940 KB |
Output is correct |
2 |
Correct |
148 ms |
41168 KB |
Output is correct |
3 |
Correct |
149 ms |
41156 KB |
Output is correct |
4 |
Correct |
156 ms |
41072 KB |
Output is correct |
5 |
Incorrect |
28 ms |
5712 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
4952 KB |
Output is correct |
2 |
Correct |
175 ms |
32632 KB |
Output is correct |
3 |
Correct |
149 ms |
33076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
4952 KB |
Output is correct |
2 |
Correct |
175 ms |
32632 KB |
Output is correct |
3 |
Correct |
149 ms |
33076 KB |
Output is correct |
4 |
Incorrect |
34 ms |
5700 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
4952 KB |
Output is correct |
2 |
Correct |
149 ms |
41248 KB |
Output is correct |
3 |
Correct |
150 ms |
41156 KB |
Output is correct |
4 |
Correct |
162 ms |
41024 KB |
Output is correct |
5 |
Correct |
31 ms |
5836 KB |
Output is correct |
6 |
Correct |
168 ms |
32624 KB |
Output is correct |
7 |
Correct |
143 ms |
33104 KB |
Output is correct |
8 |
Correct |
228 ms |
33464 KB |
Output is correct |
9 |
Correct |
183 ms |
33656 KB |
Output is correct |
10 |
Correct |
185 ms |
38088 KB |
Output is correct |
11 |
Correct |
266 ms |
37316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
4952 KB |
Output is correct |
2 |
Correct |
149 ms |
41248 KB |
Output is correct |
3 |
Correct |
150 ms |
41156 KB |
Output is correct |
4 |
Correct |
162 ms |
41024 KB |
Output is correct |
5 |
Correct |
31 ms |
5836 KB |
Output is correct |
6 |
Correct |
168 ms |
32624 KB |
Output is correct |
7 |
Correct |
143 ms |
33104 KB |
Output is correct |
8 |
Correct |
228 ms |
33464 KB |
Output is correct |
9 |
Correct |
183 ms |
33656 KB |
Output is correct |
10 |
Correct |
185 ms |
38088 KB |
Output is correct |
11 |
Correct |
266 ms |
37316 KB |
Output is correct |
12 |
Incorrect |
29 ms |
5700 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
4940 KB |
Output is correct |
2 |
Correct |
37 ms |
7108 KB |
Output is correct |
3 |
Correct |
57 ms |
7160 KB |
Output is correct |
4 |
Correct |
34 ms |
7156 KB |
Output is correct |
5 |
Correct |
54 ms |
7364 KB |
Output is correct |
6 |
Correct |
55 ms |
7236 KB |
Output is correct |
7 |
Correct |
33 ms |
5828 KB |
Output is correct |
8 |
Correct |
162 ms |
32188 KB |
Output is correct |
9 |
Correct |
163 ms |
32124 KB |
Output is correct |
10 |
Correct |
30 ms |
5772 KB |
Output is correct |
11 |
Correct |
149 ms |
41156 KB |
Output is correct |
12 |
Correct |
150 ms |
41172 KB |
Output is correct |
13 |
Correct |
159 ms |
41088 KB |
Output is correct |
14 |
Correct |
32 ms |
5836 KB |
Output is correct |
15 |
Correct |
167 ms |
32584 KB |
Output is correct |
16 |
Correct |
145 ms |
33044 KB |
Output is correct |
17 |
Correct |
234 ms |
33500 KB |
Output is correct |
18 |
Correct |
177 ms |
33476 KB |
Output is correct |
19 |
Correct |
208 ms |
38080 KB |
Output is correct |
20 |
Correct |
295 ms |
37372 KB |
Output is correct |
21 |
Correct |
182 ms |
32688 KB |
Output is correct |
22 |
Correct |
174 ms |
32684 KB |
Output is correct |
23 |
Correct |
198 ms |
32968 KB |
Output is correct |
24 |
Correct |
187 ms |
33144 KB |
Output is correct |
25 |
Correct |
196 ms |
34928 KB |
Output is correct |
26 |
Correct |
159 ms |
32596 KB |
Output is correct |
27 |
Correct |
147 ms |
32576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
4940 KB |
Output is correct |
2 |
Correct |
37 ms |
7108 KB |
Output is correct |
3 |
Correct |
57 ms |
7160 KB |
Output is correct |
4 |
Correct |
34 ms |
7156 KB |
Output is correct |
5 |
Correct |
54 ms |
7364 KB |
Output is correct |
6 |
Correct |
55 ms |
7236 KB |
Output is correct |
7 |
Correct |
33 ms |
5828 KB |
Output is correct |
8 |
Correct |
162 ms |
32188 KB |
Output is correct |
9 |
Correct |
163 ms |
32124 KB |
Output is correct |
10 |
Correct |
30 ms |
5772 KB |
Output is correct |
11 |
Correct |
149 ms |
41156 KB |
Output is correct |
12 |
Correct |
150 ms |
41172 KB |
Output is correct |
13 |
Correct |
159 ms |
41088 KB |
Output is correct |
14 |
Correct |
32 ms |
5836 KB |
Output is correct |
15 |
Correct |
167 ms |
32584 KB |
Output is correct |
16 |
Correct |
145 ms |
33044 KB |
Output is correct |
17 |
Correct |
234 ms |
33500 KB |
Output is correct |
18 |
Correct |
177 ms |
33476 KB |
Output is correct |
19 |
Correct |
208 ms |
38080 KB |
Output is correct |
20 |
Correct |
295 ms |
37372 KB |
Output is correct |
21 |
Correct |
182 ms |
32688 KB |
Output is correct |
22 |
Correct |
174 ms |
32684 KB |
Output is correct |
23 |
Correct |
198 ms |
32968 KB |
Output is correct |
24 |
Correct |
187 ms |
33144 KB |
Output is correct |
25 |
Correct |
196 ms |
34928 KB |
Output is correct |
26 |
Correct |
159 ms |
32596 KB |
Output is correct |
27 |
Correct |
147 ms |
32576 KB |
Output is correct |
28 |
Incorrect |
29 ms |
5716 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |