| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1313218 | BigBadBully | 기지국 (IOI20_stations) | C++20 | 0 ms | 0 KiB |
#include "stations.h"
#include <bits/stdc++.h>
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
const int inf = 1e9;
vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
vector<int> bi(n,0);
vector<int> sbt(n,1),c(n,1);
vector<vector<int>> graph(n);
for(int i = 0; i < n-1; i++)
{
graph[u[i]].push_back(v[i]);
graph[v[i]].push_back(u[i]);
}
function<void(int,int)> dfs = [&](int cur,int prev)
{
c[cur] = 1-c[prev];
for(int a : graph[cur])
if(a!=prev)
dfs(a,cur),sbt[cur]+=sbt[a];
};
dfs(0,0);
int t = 0;
dfs =[&](int cur,int prev)
{
if(c[cur])
bi[cur]=t+sbt[cur]-1;
else
bi[cur]=t++;
for(int a : graph[cur])
if(a!=prev)
dfs(a,cur);
if(c[cur])t++;
};
dfs(0,0);
return bi;
}
int find_next_station(int s, int t, vector<int> c) {
if(find(c.begin(),c.end(),t)!=c.end())
return t;
if(s==0)
return *upper_bound(c.begin(),c.end(),t);
if(s < c[0])
{
c.insert(c.begin(),s);
if(t<c[0])
return c.back();
if(t>c.back())
return c.back();
int x = *upper_bound(c.begin(),c.end(),t);
return x;
}
else
{
c.push_back(s);
if(t<c[0])
return c[0];
if(t>c.back())
return c[0];
int x = *--upper_bound(c.begin(),c.end(),t);
return x;
}
return c[0];
}
static int max_label = 0;
static int r, n, k, q;
static std::vector<int> u, v, labels, answers;
static std::map<int, int> reverse_labels;
static std::vector<std::vector<int>> adjlist;
static int s, t, w;
static std::vector<int> c;
int main() {
assert(scanf("%d", &r) == 1);
//srand(time(0));
cin >> n >> k >> q;
for (int tc = 0; tc < r; tc++) {
u.resize(n - 1);
v.resize(n - 1);
for(int i = 0; i < n; i++)
v[i] = i+1;
for(int i = 0; i < n; i++)
u[i] = i;
adjlist.clear();
adjlist.resize(n);
for (int i = 0; i < n - 1; i++) {
adjlist[u[i]].push_back(v[i]);
adjlist[v[i]].push_back(u[i]);
}
labels = label(n, k, u, v);
if ((int)labels.size() != n) {
printf("Number of labels not equal to %d\n", n);
exit(0);
}
reverse_labels.clear();
for (int i = 0; i < n; i++) {
if (labels[i] < 0 || labels[i] > k) {
printf("Label not in range 0 to %d\n", k);
exit(0);
}
if (reverse_labels.find(labels[i]) != reverse_labels.end()) {
printf("Labels not unique\n");
exit(0);
}
reverse_labels[labels[i]] = i;
if (labels[i] > max_label) {
max_label = labels[i];
}
}
for (int i = 0; i < q; i++) {
int cnt= 0;
s = rand()%n;
t = rand()%n;
while(t==s)
t = rand()%n;
c.clear();
for (int v : adjlist[s]) {
c.push_back(labels[v]);
}
std::sort(c.begin(), c.end());
int answer = find_next_station(labels[s], labels[t], c);
if (!std::binary_search(c.begin(), c.end(), answer)) {
printf("Label %d returned by find_next_station not found in c\n", answer);
exit(0);
}
answers.push_back(reverse_labels[answer]);
}
}
printf("%d\n", max_label);
for (int index : answers) {
printf("%d\n", index);
}
exit(0);
}
