#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 7;
vector < int > tin, tout, d;
void dfs( vector<vector<int>> adj, int v, int p, int h, int &cnt ) {
tin[v] = cnt++;
d[v] = h;
for ( auto u: adj[v] ) {
if ( u != p ) {
dfs( adj, u, v, h + 1, cnt );
}
}
tout[v] = cnt++;
}
vector<int> label(int n, int k, vector<int> U, vector<int> V) {
vector < vector < int > > adj(n + 1);
tin.resize( n + 1 );
tout.resize( n + 1 );
d.resize( n + 1 );
for ( int i = 0; i < n - 1; i++ ) {
int u = U[i], v = V[i];
adj[u].push_back(v);
adj[v].push_back(u);
}
vector < int > lb(n);
int cnt = 0;
dfs( adj, 0, -1, 0, cnt );
vector < int > vc;
for ( int i = 0; i < n; i++ ) {
if ( i & 1 ) {
vc.push_back( tout[i] );
}
else {
vc.push_back( tin[i] );
}
}
sort( vc.begin(), vc.end() );
int x = 0;
map < int, int > mp;
for ( auto i: vc ) {
mp[i] = x++;
}
for ( int i = 0; i < n; i++ ) {
if ( i & 1 ) {
lb[i] = mp[ tout[i] ];
}
else {
lb[i] = mp[ tin[i] ];
}
}
return lb;
}
int find_next_station(int s, int t, vector<int> c) {
int sz = c.size(), pr = -1;
int tins = -1, touts = -1;
vector < int > tin(sz), tout(sz);
if ( c[0] > s ) { // s is in even layer, s in tin
tins = s;
if ( s == 0 ) {
touts = c.back() + 1;
}
else {
touts = c[sz - 2] + 1;
pr = c.back();
c.pop_back();
sz--;
}
tin[0] = tins + 1;
tout[0] = c[0];
for ( int i = 1; i < sz; i++ ) {
tin[i] = tout[i - 1] + 1;
tout[i] = c[i];
}
}
else { // s in odd layer
touts = s;
if ( sz == 1 ) {
tins = s - 1;
return c[0];
}
else {
pr = c[0];
c.erase(c.begin());
sz--;
tins = c[0] - 1;
}
tout[sz - 1] = s - 1;
tin[sz - 1] = c[sz - 1];
for ( int i = sz - 2; i >= 0; i-- ) {
tout[i] = tin[i + 1] - 1;
tin[i] = c[i];
}
}
// cout << tins << ' ' << touts << ' ' << pr << endl;
for ( int i = 0; i < sz; i++ ) {
int u = c[i];
// cout << tin[i] << " " << tout[i] << endl;
if ( tin[i] <= t && tout[i] >= t ) {
return u;
}
}
assert( pr != -1 );
return pr;
}
Compilation message
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:62:17: warning: variable 'touts' set but not used [-Wunused-but-set-variable]
62 | int tins = -1, touts = -1;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
315 ms |
55756 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
531 ms |
1452 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
448 ms |
55780 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
843 ms |
400 KB |
Output is correct |
2 |
Runtime error |
2 ms |
744 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
311 ms |
55792 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |