#include <bits/stdc++.h>
using namespace std;
vector<int> label(int n ,int l , vector<int> u , vector<int> v){
vector<int> ans(n) ;
vector<int> adj[n] ;
for(int i=0;i<n-1;++i){
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
}
function<vector<int>(int,int,int)> dfs = [&](int a ,int p , int lvl){
vector<int> ans ;
if(lvl)
ans.push_back(a) ;
for(int x : adj[a]){
if(x!=p){
vector<int> res = dfs(x,a,lvl^1) ;
for(int x :res) ans.push_back(x) ;
}
}
if(lvl==0) ans.push_back(a) ;
return ans ;
};
vector<int> res = dfs(0,-1,1) ;
for(int i=0;i<n;i++) ans[res[i]]=i ;
return ans ;
}
int find_next_station(int u , int v , vector<int> ne){
if(u==0){
if(u<=v&&v<=ne[0]) return ne[0] ;
for(int i=1;i<ne.size();i++) if(ne[i]>=v&&v>ne[i-1]) return ne[i] ;
}
if(u<=ne[0]){
int dad = ne.back() ;
if(u<=v&&v<=ne[0]) return ne[0] ;
for(int i=1;i+1<ne.size();i++)
if(ne[i]>=v&&v>ne[i-1]) return ne[i] ;
return dad ;
}
else{
int dad = ne[0] ;
if(u>=v&&u>=ne.back()) return ne.back() ;
for(int i=(int)ne.size()-2;i>0;i--)
if(ne[i+1]>v&&v>=ne[i]) return ne[i] ;
return dad ;
}
return 5/0;
}
/*int main(){
vector<int> res = label(5,5654,{0,1,2,3},{1,2,3,4}) ;
for(int x : res) cout << x << " ";
cout << endl ;
} */
Compilation message
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:30:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=1;i<ne.size();i++) if(ne[i]>=v&&v>ne[i-1]) return ne[i] ;
| ~^~~~~~~~~~
stations.cpp:35:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=1;i+1<ne.size();i++)
| ~~~^~~~~~~~~~
stations.cpp:46:13: warning: division by zero [-Wdiv-by-zero]
46 | return 5/0;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
573 ms |
728 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
508 ms |
560 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
515 ms |
708 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1186 ms |
400 KB |
Output is correct |
2 |
Incorrect |
762 ms |
472 KB |
Wrong query response. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
750 ms |
656 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |