Submission #835337

#TimeUsernameProblemLanguageResultExecution timeMemory
835337welleythStations (IOI20_stations)C++17
100 / 100
869 ms940 KiB
#include "stations.h"
#include <bits/stdc++.h>
using namespace std;

constexpr int N = (int)1e3;

int tin[N],tout[N];
int timer = 0;

vector<int> g[N];
int id[N];
int d[N];
int c = 0;
vector<int> labels;

mt19937 rnd(time(nullptr));

int sz[N];
vector<int> values;

void dfs(int v,int pr = -1){
	tin[v] = timer++;
	for(auto& to : g[v]){
		if(to == pr) continue;
		if(id[v] == 0)
			id[to] = ++c;
		else
			id[to] = id[v];
		d[to] = d[v] + 1;
		dfs(to,v);
	}
	tout[v] = timer++;
	if(d[v] % 2 == 0){
		values.push_back(tin[v]);
	} else {
		values.push_back(tout[v]);
	}
	return;
}

std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
	for(int i = 0; i < n; i++) g[i].clear();
	timer = 0;
	labels.clear();
	labels.resize(n);
	memset(id,0,sizeof id);
	memset(d,0,sizeof d);
	c = 0;
	for(int i = 0; i < n-1; i++){
		g[u[i]].push_back(v[i]);
		g[v[i]].push_back(u[i]);
	}
	values.clear();
	d[0] = 0;
	dfs(0);
	sort(values.begin(),values.end());
	values.erase(unique(values.begin(),values.end()),values.end());
	for(int i = 0; i < n; i++){
		if(d[i] % 2 == 0){
			labels[i] = lower_bound(values.begin(),values.end(),tin[i]) - values.begin();
		} else {
			labels[i] = lower_bound(values.begin(),values.end(),tout[i]) - values.begin();
		}
	}
	return labels;
}

bool upper(int a,int b){
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int find_next_station(int s, int t, std::vector<int> c) {
	int tinS = 2*N, toutS = -1, tinT = 2*N, toutT = -1;
	bool isToutS = true;
	for(auto& to : c){
		isToutS &= s > to; 
	}
	if(isToutS)
		toutS = s;
	else
		tinS = s;
	tinT = toutT = t;
	int pr = -1;
	for(auto& to : c){
		int in,out;
		if(!isToutS){
			out = to;
			if(pr == -1 || out > pr)
				pr = to;
		}
		else{
			in = to;
			if(pr == -1 || in < pr)
				pr = to;
		}
	}

	int go = -1;

	for(auto& to : c){
		// if(to == pr) continue;
		int in,out;
		if(!isToutS){
			out = to;
			if(tinT < tinS)
				return pr;
			if(out >= tinT && (go == -1 || out < go))
				go = to;
		}
		else{
			in = to;
			if(toutT > toutS)
				return pr;
			if(in <= toutT && (go == -1 || in > go))
				go = to;
		}
	}
	if(go == -1)
		return pr;

	return go;
}
#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...