#include "stations.h"
#include <vector>
#include <algorithm>
#include <utility>
#include <iostream>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
vvi al;
vi rans;
int ft = 0;
ii dfs(int v, int p, int r) {
int cmin = 1002;
int cmax = -1;
for(int i=0; i < (int)al[v].size(); ++i) {
int u = al[v][i];
if(u == p) continue;
ii x = dfs(u, v, !r);
//cerr << "VISITED " << u << " returned " << x.first << " " << x.second << endl;
cmin = min(cmin, x.first);
cmax = max(cmax, x.second);
}
if(cmin == 1002) {
rans[ft] = v;
ft++;
return ii(ft-1, ft-1);
}
else {
if(r) {
for(int i=ft; i > cmax+1; --i) {
swap(rans[i], rans[i-1]);
}
rans[cmax+1] = v;
ft++;
}
else {
for(int i=ft; i > cmin; --i) {
swap(rans[i], rans[i-1]);
}
rans[cmin] = v;
ft++;
}
return ii(cmin, max(cmax, ft-1));
}
}
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
al = vvi(n, vi());
for(int i=0; i < n-1; ++i) {
al[u[i]].push_back(v[i]);
al[v[i]].push_back(u[i]);
}
rans = vi(1001, -1);
ft = 0;
dfs(0, -1, 0);
vi ans(n);
/*for(int i=0; i < 1001; ++i) {
if(rans[i] != -1) {
cerr << "rans[" << i << "] = " << rans[i] << endl;
}
}*/
for(int i=0; i < n; ++i) {
ans[rans[i]] = i;
}
return ans;
}
int find_next_station(int s, int t, std::vector<int> c) {
int m = c.size();
if(m == 1) return c[0];
bool ismax = true;
for(int i=0; i < m; ++i) {
if(s < c[i]) ismax = false;
}
if(ismax) {
int minv = 1002;
for(int i=0; i < m; ++i) minv = min(c[i], minv);
if(t <= minv || t >= s) return minv;
vi otc;
for(int i=0; i < m; ++i) {
if(c[i] != minv) otc.push_back(c[i]);
}
sort(otc.begin(), otc.end());
for(int i=m-2; i > -1; --i) {
if(t >= otc[i]) return otc[i];
}
}
else {
int maxv = -1;
for(int i=0; i < m; ++i) maxv = max(c[i], maxv);
if(t >= maxv || t <= s) return maxv;
vi otc;
for(int i=0; i < m; ++i) {
if(c[i] != maxv) otc.push_back(c[i]);
}
sort(otc.begin(), otc.end());
for(int i=0; i < m-1; ++i) {
if(t <= otc[i]) return otc[i];
}
}
}
Compilation message
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:117:1: warning: control reaches end of non-void function [-Wreturn-type]
117 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
8 ms |
1360 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
1388 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
11 ms |
1280 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1119 ms |
876 KB |
Output is correct |
2 |
Correct |
710 ms |
768 KB |
Output is correct |
3 |
Runtime error |
4 ms |
1152 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
9 ms |
1340 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |