#include "stations.h"
#include <vector>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,int> ipii;
const int MAXN = 2e3+10;
const int MAX = 1e3;
int n, k;
vector <int> adj[MAXN], dw[MAXN];
vector <int> lab;
int chi[MAXN], val[MAXN], cnt;
void dfs(int nw, int par){ // build val, tergantung dfs
val[nw] = cnt++;
for(auto nx : adj[nw]){
if(nx == par) continue;
dw[nw].pb(nx);
dfs(nx, nw);
}
}
void bd(int nw, int par){
for(int i=0; i<dw[nw].size(); i++){
if(i+1==dw[nw].size()) chi[ dw[nw][i] ] = 0;
else { // ada nx nya
chi[ dw[nw][i] ] = val[ dw[nw][i+1] ]; // nx nya yg itu
}
}
for(auto nx : dw[nw]){
if(nx == par) continue;
bd(nx, nw);
}
}
vector<int> label(int N, int K, vector<int> u, vector<int> v) {
n = N; k = K;
lab.clear(); lab.resize(n); cnt = 0;
for(int i=0; i<=n; i++){
adj[i].clear(); dw[i].clear();
}
for(int i=0; i<n-1; i++){
adj[u[i]].pb(v[i]); adj[v[i]].pb(u[i]);
}
dfs(0, -1); bd(0, -1);
for(int i=0; i<n; i++) lab[i] = val[i] + chi[i]*MAX;
//for(int i=0; i<n; i++) cout << i << ' ' << val[i] << ' ' << chi[i] << " p\n";
return lab;
}
int find_next_station(int s, int t, vector<int> c) { // c sorted
int num = c.size();
vector <pii> val(num);
for(int i=0; i<num; i++){
val[i].fi = c[i]%MAX; val[i].se = c[i] / MAX;
}
sort(val.begin(), val.end()); // based on idx
int valt = t%MAX, chit = t/MAX;
int ans = -1, idx = -1;
if(s==0){ // s itu root
for(int i=0; i<val.size(); i++){
if(val[i].fi <= valt){
if(ans < val[i].fi){ // ambil yg terakhir -> in <=
ans = val[i].fi; idx = i;
}
}
}
return (val[idx].fi + val[idx].se*MAX);
}
int nx = s/MAX; // kalo nx==0, brati gk ada nx
for(int i=0; i<val.size(); i++){
if(val[i].fi <= valt){
if(ans < val[i].fi){ // ambil yg terakhir -> in <=
ans = val[i].fi; idx = i;
}
}
}
if(valt < val[1].fi || valt >= nx) idx = 0; // parent
return (val[idx].fi + val[idx].se*MAX);
}
Compilation message
stations.cpp: In function 'void bd(int, int)':
stations.cpp:27:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0; i<dw[nw].size(); i++){
| ~^~~~~~~~~~~~~~
stations.cpp:29:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(i+1==dw[nw].size()) chi[ dw[nw][i] ] = 0;
| ~~~^~~~~~~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:70:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=0; i<val.size(); i++){
| ~^~~~~~~~~~~
stations.cpp:81:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i=0; i<val.size(); i++){
| ~^~~~~~~~~~~
stations.cpp:67:20: warning: unused variable 'chit' [-Wunused-variable]
67 | int valt = t%MAX, chit = t/MAX;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
692 KB |
Invalid labels (values out of range). scenario=0, k=1000, vertex=9, label=2001 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
632 KB |
Invalid labels (values out of range). scenario=0, k=1000, vertex=1, label=512001 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
294 ms |
1040 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
482 ms |
684 KB |
Output is correct |
2 |
Incorrect |
350 ms |
864 KB |
Wrong query response. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
325 ms |
1028 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |