Submission #1205457

#TimeUsernameProblemLanguageResultExecution timeMemory
1205457banganStations (IOI20_stations)C++20
100 / 100
302 ms576 KiB
#include "stations.h"
#include <vector>
#include <bits/stdc++.h>

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

	int timer = 0;
	std::vector<int> st(n), ft(n), dep(n);
	auto dfs = [&](auto self, int v, int f) -> void {
		st[v] = timer++;
		for (int u : adj[v]) {
			if (u != f) {
				dep[u] = dep[v] ^ 1;
				self(self, u, v);
			}
		}
		ft[v] = timer++;
	};
	dfs(dfs, 0, 0);

	std::vector<int> all(n);
	std::vector<int> id(n);
	for (int i = 0; i < n; i++) {
		id[i] = (dep[i] ? ft[i] : st[i]);
		all[i] = id[i];
	}

	std::sort(all.begin(), all.end());
	std::vector<int> res(2 * n + 1);
	for (int i = 0; i < n; i++) {
		res[all[i]] = i;
	}
	for (int i = 0; i < n; i++) {
		id[i] = res[id[i]];
	}

	return id;
}

int find_next_station(int s, int t, std::vector<int> c) {
	if (c.size() == 1) {
		return c[0];
	}

	if (s >= *std::max_element(c.begin(), c.end())) {
		for (int i = 1; i < c.size(); i++) {
			int st = c[i];
			int ft = (i + 1 == c.size() ? s - 1 : c[i + 1] - 1);

			if (st <= t && t <= ft) {
				return c[i];
			}
		}
		return c[0];
	} else {
		for (int i = 0; i + 1 < c.size(); i++) {
			int st = (i == 0 ? s + 1 : c[i - 1] + 1);
			int ft = c[i];

			if (st <= t && t <= ft) {
				return c[i];
			}
		}
		return c.back();
	}	
}
#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...