#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[i][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";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
4948 KB |
Output is correct |
2 |
Correct |
162 ms |
32188 KB |
Output is correct |
3 |
Correct |
173 ms |
32300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
4948 KB |
Output is correct |
2 |
Correct |
162 ms |
32188 KB |
Output is correct |
3 |
Correct |
173 ms |
32300 KB |
Output is correct |
4 |
Incorrect |
32 ms |
5816 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
4904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
4904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
4932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
4932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
4944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
4944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |