This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef Nhoksocqt1
#include "fun.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
#ifdef Nhoksocqt1
vector<int> adjp[MAXN];
int dist[5003][5003];
void bfs(int dist[], int y) {
queue<int> qu;
dist[y] = 1;
qu.push(y);
while(sz(qu)) {
int x(qu.front()); qu.pop();
for (int it = 0; it < sz(adjp[x]); ++it) {
int y(adjp[x][it]);
if(dist[y] == 0) {
dist[y] = dist[x] + 1;
qu.push(y);
}
}
}
}
int hoursRequired(int x, int y) {
return dist[x][y] - 1;
}
#endif // Nhoksocqt1
vector<int> adj[MAXN], B[2][MAXN];
int depth[MAXN], numNode, maxAsk;
bool dx[MAXN];
int askHoursRequired(int x, int y) {
return hoursRequired(x, y);
}
ii dfsFar(int u, int p) {
ii res = {0, u};
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it]);
if(v != p && !dx[v]) {
ii tmp = dfsFar(v, u); ++tmp.fi;
res = max(res, tmp);
}
}
return res;
}
vector<int> sub2(void) {
for (int i = 0; i < numNode; ++i) {
for (int j = i + 1; j < numNode; ++j) {
if(askHoursRequired(i, j) == 1) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
vector<int> ans;
int u = dfsFar(0, -1).se;
ans.push_back(u);
for (int i = 1; i < numNode; ++i) {
int v = dfsFar(u, -1).se;
dx[u] = 1;
u = v;
ans.push_back(v);
}
return ans;
}
bool check_sub3(void) {
for (int i = 1; i < numNode; ++i) {
if(askHoursRequired((i - 1) / 2, i) > 1)
return false;
}
return true;
}
void dfs3(int u, vector<int> B[]) {
B[depth[u]].push_back(u);
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it]);
depth[v] = depth[u] + 1;
dfs3(v, B);
}
}
vector<int> sub3(void) {
for (int i = 1; i < numNode; ++i)
adj[(i - 1) / 2].push_back(i);
depth[1] = depth[2] = 1;
dfs3(1, B[0]);
if(numNode > 2)
dfs3(2, B[1]);
vector<int> ans;
int j(numNode);
for (int i = numNode; i > 0; --i) {
while(sz(B[0][i])) {
int u(B[0][i].back());
B[0][i].pop_back();
while(j > 0 && !sz(B[1][j]))
--j;
ans.push_back(u);
if(sz(B[1][j])) {
int v(B[1][j].back());
B[1][j].pop_back();
ans.push_back(v);
}
}
}
ans.push_back(0);
return ans;
}
vector<int> createFunTour(int _N, int _Q) {
numNode = _N, maxAsk = _Q;
if(numNode <= 500)
return sub2();
if(check_sub3())
return sub3();
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "fun"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int _N, _Q = 400000;
cin >> _N >> _Q;
for (int i = 0; i + 1 < _N; ++i) {
int u, v;
cin >> u >> v;
adjp[u].push_back(v);
adjp[v].push_back(u);
}
for (int i = 0; i < _N; ++i)
bfs(dist[i], i);
vector<int> funTour = createFunTour(_N, _Q);
cout << "ANSWER: ";
for (int it = 0; it < _N; ++it)
cout << funTour[it] << ' '; cout << '\n';
return 0;
}
#endif // Nhoksocqt1
Compilation message (stderr)
fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:160:1: warning: control reaches end of non-void function [-Wreturn-type]
160 | }
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |