제출 #1033197

#제출 시각아이디문제언어결과실행 시간메모리
1033197thinknoexit기지국 (IOI20_stations)C++17
100 / 100
636 ms1296 KiB
#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<int> adj[1010];
int res[1010], sz[1010], lv[1010];
void dfs_sz(int v, int p = -1) {
	sz[v] = 1;
	for (auto& x : adj[v]) {
		if (x == p) continue;
		lv[x] = lv[v] ^ 1;
		dfs_sz(x, v);
		sz[v] += sz[x];
	}
}
void dfs(int v, int l, int r, int p = -1) {
	if (lv[v] & 1) res[v] = r--;
	else res[v] = l++;
	for (auto& x : adj[v]) {
		if (x == p) continue;
		if (lv[v] & 1) {
			dfs(x, l, l + sz[x] - 1, v);
			l += sz[x];
		}
		else {
			dfs(x, r - sz[x] + 1, r, v);
			r -= sz[x];
		}
	}

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

int find_next_station(int s, int t, vector<int> c) {
	int k = c.size();
	if (k == 1) return c[0];
	bool ch = 1;
	for (auto& x : c) ch &= s < x;
	if (ch) { // is minimum of subtree
		sort(c.rbegin(), c.rend());
		if (s != 1 && (t < s || t > c[0])) return c[0];
		int ans = 0;
		for (auto& x : c) {
			if (t <= x) ans = x;
		}
		return ans;
	}
	else { // is maximum of subtree
		sort(c.begin(), c.end());
		if (t > s || t < c[1]) return c[0];
		int ans = 0;
		for (auto& x : c) {
			if (t >= x) ans = x;
		}
		return 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...