#include <iostream>
#include <vector>
#define pb push_back
using namespace std;
typedef pair<int, int> pii;
vector<int> con[1001];
vector<int> son[1001];
bool used[1001];
vector<int> ans;
void rt(int x)
{
int i;
for(i=0;i<con[x].size();i++){
int f=con[x][i];
if(!used[f])
son[x].pb(f), used[f]=true, rt(f);
}
}
pii wk(int x, int lev, int label)
{
pii ret={label, 1};
int i;
if(lev%2==0){
ans[x]=label;
for(i=0;i<son[x].size();i++){
pii f=wk(son[x][i], lev+1, label+1);
label+=f.second;
ret.first=max(ret.first, f.first);
ret.second+=f.second;
}
}
if(lev%2==1){
for(i=0;i<son[x].size();i++){
pii f=wk(son[x][i], lev+1, label);
label+=f.second;
ret.first=max(ret.first, f.first);
ret.second+=f.second;
}
ans[x]=label;
}
return ret;
}
vector<int> label(int n, int k, vector<int> u, vector<int> v)
{
int i;
for(i=0;i<=1000;i++){
con[i].clear();
son[i].clear();
used[i]=false;
}
for(i=0;i<n;i++){
con[u[i]].pb(v[i]);
con[v[i]].pb(u[i]);
ans.pb(0);
}
rt(0);
wk(0, 1, 1);
return ans;
}
int find_next_station(int s, int t, vector<int> nei)
{
int lev=1;
int i;
for(i=0;i<nei.size();i++)
if(nei[i]<s)
lev=0;
if(lev==1){
sort(nei.begin(), nei.end());
bool maxi=false;
if(t<s)
maxi=true;
if(maxi)
return nei[nei.size()-1];
for(i=int(nei.size())-1;i>=0;i--)
if(t>nei[i])
return nei[i];
}
else{
sort(nei.begin(), nei.end());
bool mini=false;
if(t>=nei[nei.size()-1])
mini=true;
if(mini)
return nei[0];
for(i=0;i<nei.size();i++)
if(t<nei[i])
return nei[i];
}
return -1;
}
Compilation message
stations.cpp: In function 'void rt(int)':
stations.cpp:20:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(i=0;i<con[x].size();i++){
| ~^~~~~~~~~~~~~~
stations.cpp: In function 'pii wk(int, int, int)':
stations.cpp:34:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(i=0;i<son[x].size();i++){
| ~^~~~~~~~~~~~~~
stations.cpp:42:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(i=0;i<son[x].size();i++){
| ~^~~~~~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:79:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(i=0;i<nei.size();i++)
| ~^~~~~~~~~~~
stations.cpp:84:9: error: 'sort' was not declared in this scope; did you mean 'qsort'?
84 | sort(nei.begin(), nei.end());
| ^~~~
| qsort
stations.cpp:95:9: error: 'sort' was not declared in this scope; did you mean 'qsort'?
95 | sort(nei.begin(), nei.end());
| ^~~~
| qsort
stations.cpp:101:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for(i=0;i<nei.size();i++)
| ~^~~~~~~~~~~