제출 #1234909

#제출 시각아이디문제언어결과실행 시간메모리
1234909madamadam3기지국 (IOI20_stations)C++20
76 / 100
310 ms640 KiB
#include "stations.h"
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
	#define dbg(x) (cout << (x))
#else
	#define dbg(x)
#endif

typedef long long ll;
using vi =vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
#define pb push_back
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define trace(x) for (auto &el : x) {dbg(el); dbg(" ");}

int n, k;
vi U, V, labels;
vvi adj;
int cur_label = 0;
int timer = 0;
vi tin, tout;

void dfs(int u, int p, bool depth) {
	tin[u] = timer++;

	for (auto &v : adj[u]) {
		if (v == p) continue;
		dfs(v, u, !depth);
	}

	tout[u] = timer++;
	labels[u] = depth ? tin[u] : tout[u];
}

vi label(int N, int K, vi x, vi y) {
	n = N; k = K;
	U = x; V = y;
	cur_label = 0;
	tin.assign(n, 0); tout.assign(n, 0);
	timer = 0;

	adj.assign(n, vi());
	vi deg(n); for (int i = 0; i < n - 1; i++) {
		deg[U[i]]++, deg[V[i]]++;
		adj[U[i]].push_back(V[i]);
		adj[V[i]].pb(U[i]);
	}

	labels.assign(n, 0);
	dfs(0, 0, true);
	trace(labels); dbg("\n");
	return labels;
}

// tin[s] <= tin[t] <= tout[t] <= tout[s]
int find_next_station(int s, int t, vi c) {
	if (s < c[0]) { // we must be tin and everything else tout
		if (t < s) return c.back(); // return parent bc tout[t] < tin[s]
		if (t >= c.back()) return c.back(); // if its out of/only within the parents range, give the parent

		int ans = 0;
		while (c[ans] < t) ans++;
		return c[ans];
	} else { // we tout, everyone else tin
		if (t > s) return c[0]; // return parent bc tin[t] < tout[s]
		if (t <= c[0]) return c[0]; // its either in the parents range or before it 

		int ans = c.size() - 1;
		while (c[ans] > t) ans--;
		return c[ans];
	}
}
#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...