#include "stations.h"
#include <vector>
using namespace std;
typedef vector<int> vi;
const int N = 1000;
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int ta[N], tb[N];
void dfs(int p, int i) {
static int time;
int o;
ta[i] = time++;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs(i, j);
}
tb[i] = time - 1;
}
vi label(int n, int k, vi uu, vi vv) {
int h, i;
vi labels(n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n - 1; h++)
append(uu[h], vv[h]), append(vv[h], uu[h]);
dfs(-1, 0);
for (i = 0; i < n; i++)
labels[i] = ta[i] * N + tb[i];
return labels;
}
int find_next_station(int s, int t, vi cc) {
int m = cc.size(), h;
for (h = 0; h < m; h++)
if (cc[h] / N <= t && t <= cc[h] % N)
return cc[h];
return -1;
}
Compilation message
stations.cpp: In function 'void append(int, int)':
stations.cpp:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
stations.cpp:15:19: error: 'realloc' was not declared in this scope
15 | ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
| ^~~~~~~
stations.cpp:2:1: note: 'realloc' is defined in header '<cstdlib>'; did you forget to '#include <cstdlib>'?
1 | #include "stations.h"
+++ |+#include <cstdlib>
2 | #include <vector>
stations.cpp: In function 'vi label(int, int, vi, vi)':
stations.cpp:40:19: error: 'malloc' was not declared in this scope
40 | ej[i] = (int *) malloc(2 * sizeof *ej[i]);
| ^~~~~~
stations.cpp:40:19: note: 'malloc' is defined in header '<cstdlib>'; did you forget to '#include <cstdlib>'?