# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1053661 | nightfal | Stations (IOI20_stations) | C++17 | 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 "stations.h"
#include <cstdio>
#include <iostream>
#include <cassert>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
template <typename T> void print(T elem) {cout << elem << " ";}
template <typename T> void print(vector<T> &v) {for(auto elem: v) print(elem); cout << endl;};
template <typename T> void print(vector<vector<T>> &v) {for(auto elem: v) print(elem); cout << endl;};
bool subtask1(vector<vector<int>> &g) {
int n = g.size();
for(auto elem: g)
if (elem.size() > 2) return false;
return true;
}
bool subtask2(vector<int> &u, vector<int> &v) {
for(int i=0; i<n; i++)
if (!(u[i]==i+1 && v[i]==i/2 || u[i]==i/2 && v[i]==i+1))
return false;
return true;
}
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
std::vector<int> labels(n,-1);
vector<vector<int>> g(n);
for (int i=0; i<n-1; i++) {
g[u[i]].push_back(v[i]);
g[v[i]].push_back(u[i]);
}
// print(g);
if (subtask1(g)==1) {
int node = 0;
for (int i=0; i<n; i++)
if(g[i].size()==1) node = i;
int prev = node;
for (int i=0; i<n; i++) {
labels[node] = i;
for(int next: g[node])
if(labels[next]== -1) {node = next;}
}
}
if (subtask2(u,v)==2) {
for(int i=0; i<n; i++)
labels[i] = i;
}
// print(labels);
return labels;
}
bool ancestor(int s, int t) {
if (s==0) return true;
while (t) {
if (s==t) return true;
t >>= 1;
}
return false;
}
int find_next_station(int s, int t, std::vector<int> c) {
int subtask = 1;
if (subtask==1) {
for(int next: c)
if (s<t && s<next) return next;
else if (s>t && s>next) return next;
}
if (subtask==2) {
if (ancestor(2*s+1,t)) return 2*s+1;
else if (ancestor(2*s+2,t)) return 2*s+2;
else return s/2;
}
}