// TODO -> PONER ALGO AQUI
// amigos y no mas
// I'm in the coolest driver high
// += O(logn) ; + = O(n)
#include <bits/stdc++.h>
#include "stations.h"
#include <variant>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define ones(x) __builtin_popcount(x)
#define loga2(x) __builtin_ctzll(x)
#define pos2(x) __builtin_clzll(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef pair <ii, ii> iiii;
typedef vector <int> vi;
typedef vector <ii> vii;
const int mod = 998244353;
const ll inf = (1LL<<62)-1;
struct HASH{
size_t operator()(const pair<int,int>&x)const{
return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
}
};
vector< vector<int> > G;
vector<int> P , H;
vector<bool> vis;
int N;
int st[1005][11];
void dfs(int x){
for(int i=0; i<G[x].size(); i++){
int &y = G[x][i];
if(!vis[y]){
vis[y] = true;
H[y] = H[x]+1;
P[y] = x;
dfs(y);
}
}
}
void build(){
for(int i=0 ; i<N ; i++) st[i][0] = P[i];
for(int i=1; i<11; i++){
for(int j=0; j<N;j++){
st[j][i] = st[ st[j][i-1] ][i-1];
}
}
}
int query(int x, int y){
int a = x , b= y;
if(H[a] > H[b]){
int dis = H[a] - H[b];
for(int i=10; i>=0; i--){
if(dis & (1<<i) ){
a = st[a][i];
dis -= (1<<i);
}
}
}
else if(H[a] < H[b]){
int dis = H[b] - H[a];
for(int i=10; i>=0; i--){
if(dis & (1<<i) ){
b = st[b][i];
dis -= (1<<i);
}
}
}
if(a==b){
if(a==x){
b = y;
int dis = (H[b] - H[a]) - 1;
for(int i=10; i>=0; i--){
if(dis & (1<<i) ){
b = st[b][i];
dis -= (1<<i);
}
}
return b;
}
else{
return P[x];
}
}
else{
return P[x];
}
// s , t , P(s)
// s <- t (BL s-1)
// s -> t P(s)
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
N = n;
vector<int> labels(n);
for (int i = 0; i < n; i++) {
labels[i] = i;
}
G.clear(); H.clear();
P.clear(); vis.clear();
G.resize(n);
H.resize(n);
P.resize(n);
vis.resize(n,0);
for(int i=0; i<n-1;i++){
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
}
H[0] = 0;
P[0] = 0;
vis[0] = true;
dfs(0);
build();
return labels;
}
int find_next_station(int s, int t, std::vector<int> c) {
cerr<<query(s,t)<<"\n";
return query(s,t);
}
Compilation message
stations.cpp: In function 'void dfs(int)':
stations.cpp:40:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i=0; i<G[x].size(); i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
940 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
940 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
944 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
944 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
940 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |