# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
313446 | qiangbao | Stations (IOI20_stations) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}