Submission #436420

#TimeUsernameProblemLanguageResultExecution timeMemory
436420frodakcinStations (IOI20_stations)C++17
100 / 100
1265 ms776 KiB
#include "stations.h"
#include <algorithm>
#include <vector>
#include <functional>

std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
	std::vector<int> labels(n, -1);
	std::vector<std::vector<int> > a(n, std::vector<int>());
	for(int i=0;i<n-1;++i)
		a[u[i]].push_back(v[i]), a[v[i]].push_back(u[i]);

	int ctr=0;
	std::function<void(int,int,bool)> dfs=[&](int n, int p, bool t)
	{
		if(t) labels[n]=ctr++;
		for(int x:a[n])
			if(x!=p)
				dfs(x, n, !t);
		if(!t) labels[n]=ctr++;
	};
	dfs(0, -1, 0);
	return labels;
}

int find_next_station(int s, int t, std::vector<int> c) {
	if(s < c[0]) // all neighbors postorder
	{
		if(t<s) return c.back();
		if(t>c.back()) return c.back();
		return *std::lower_bound(c.begin(), c.end(), t);
	}
	else // all neighbors preorder
	{
		if(t>s) return c[0];
		if(t<c[0]) return c[0];
		return *std::prev(std::upper_bound(c.begin(), c.end(), t));
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...