QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#449272 | #8359. travel | definieren | 2 | 8ms | 91856kb | C++14 | 7.5kb | 2024-06-20 21:09:55 | 2024-06-20 21:09:56 |
Judging History
answer
#include <bits/stdc++.h>
#define fir first
#define sec second
#ifdef LOCAL
#define dbg(x) cerr << "In Line " << __LINE__ << " the " << #x << " = " << x << '\n';
#define dpi(x, y) cerr << "In Line " << __LINE__ << " the " << #x << " = " << x << " ; " << "the " << #y << " = " << y << '\n';
#define dbgf(fmt, args...) fprintf(stderr, fmt, ##args);
#else
#define dbg(x) void();
#define dpi(x, y) void();
#define dbgf(fmt, args...) void();
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vpii = vector<pii>;
bool Mbe;
constexpr int MOD = 998244353;
template<typename T> T add(T a, T b, T p = MOD) { return (a + b >= p) ? (a + b - p) : (a + b); }
template<typename T> T del(T a, T b, T p = MOD) { return (a - b < 0) ? (a - b + p) : (a - b); }
template<typename T> T mul(T a, T b, T p = MOD) { return 1ll * a * b % p; }
template<typename T> T cadd(T &a, T b, T p = MOD) { return a = add(a, b, p); }
template<typename T> T cdel(T &a, T b, T p = MOD) { return a = del(a, b, p); }
template<typename T> T cmul(T &a, T b, T p = MOD) { return a = mul(a, b, p); }
template<typename T> bool cmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> bool cmin(T &a, T b) { return a > b ? a = b, true : false; }
namespace FastIO {
constexpr int LEN = 1 << 20;
char in[LEN + 1], out[LEN + 1];
char *pin = in, *pout = out, *ein = in, *eout = out + LEN;
char gc() { return pin == ein && (ein = (pin = in) + fread(in, 1, LEN, stdin), ein == in) ? EOF : *pin ++; }
void pc(char c) { pout == eout && (fwrite(out, 1, LEN, stdout), pout = out); (*pout ++) = c; return; }
void Flush() { fwrite(out, 1, pout - out, stdout); pout = out; }
template<typename T> T Read() {
T x = 0; int f = 1; char ch = gc();
while (ch < '0' || ch > '9') f = (ch == '-' ? (~f + 1) : f), ch = gc();
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
return x * f;
}
template<typename T> void Write(T x, char c) {
static char stk[40]; int tp = 0;
if (x < 0) pc('-'), x = ~x + 1;
do stk[tp++] = x % 10 + 48, x /= 10; while (x);
while (tp --) pc(stk[tp]); pc(c); return;
}
void Read(char *s) {
char ch = gc();
while (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))) ch = gc();
while ((ch != EOF) && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))) *s = ch, s ++, ch = gc();
*s = '\0'; return;
}
void Write(char *s) {
while (*s != '\0') pc(*s), s ++; return;
}
void Puts(char *s) {
Write(s), pc('\n'); return;
}
}
#define getchar FastIO::gc
#define putchar FastIO::pc
#define Flush FastIO::Flush
#define Read FastIO::Read
#define Write FastIO::Write
#define Puts FastIO::Puts
constexpr int N = 5e5 + 5, LG = 19;
int n, k, op;
namespace Solve2 {
int rt, sz[N], mxp[N], dfn[N], ord[N], dfc, dep[N], bel[N];
pair<int, int> st[LG][N];
priority_queue<pii, vpii, greater<pii>> Q;
vector<int> son, ans, G[N];
vector<pair<int, int>> dis[N], rng[N];
bool mrk[N];
void Get_Root(int u, int fa) {
sz[u] = 1, mxp[u] = 0;
for (auto v : G[u]) if (v ^ fa)
Get_Root(v, u), sz[u] += sz[v], cmax(mxp[u], sz[v]);
cmax(mxp[u], n - sz[u]), rt = mxp[u] < mxp[rt] ? u : rt;
return;
}
void Dfs1(int u, int fa, int anc) {
sz[ord[dfn[u] = ++ dfc] = u] = 1, bel[u] = anc;
st[0][dfn[u]] = make_pair(dep[u] = dep[fa] + 1, fa);
for (auto v : G[u]) if (v ^ fa) Dfs1(v, u, anc), sz[u] += sz[v];
return;
}
pair<int, int> ST_Query(int l, int r) {
int k = __lg(r - l + 1);
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
int Get_Lca(int u, int v) {
if (u == v) return u;
if ((u = dfn[u]) > (v = dfn[v])) swap(u, v);
return ST_Query(u + 1, v).sec;
}
void Main() {
for (int i = 1; i < n; i ++) {
int u = Read<int>(), v = Read<int>();
G[u].emplace_back(v), G[v].emplace_back(u);
}
mxp[0] = n + 1, Get_Root(1, rt = 0);
sz[rt] = dep[rt] = 0, dfn[rt] = dfc = 1, bel[rt] = rt, ord[1] = rt;
for (auto u : G[rt]) son.emplace_back(u), Dfs1(u, rt, u), sz[rt] += sz[u];
for (int i = 1; i < LG; i ++)
for (int j = 1; j + (1 << i) - 1 <= n; j ++)
st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);
{
int L = 2 * (n - 1), R = 0;
for (int u = 1; u <= n; u ++) R += dep[u]; R <<= 1;
if ((k & 1) || k < L || k > R) { Puts("-1"); return; }
k = (R - k) >> 1;
};
for (int i = 1; i <= n; i ++) {
if (bel[ord[i]] != bel[ord[i + 1]]) continue;
dis[bel[ord[i]]].emplace_back(dep[Get_Lca(ord[i], ord[i + 1])], ord[i]);
}
for (auto u : son) if (dis[u].size())
sort(dis[u].begin(), dis[u].end()), Q.emplace(make_pair(dis[u].size(), u));
// for (auto u : son) {
// cout << u << ": ";
// for (auto dist : dis[u]) cout << dist.fir << ' ';
// cout << endl;
// }
if (k) {
while (Q.size()) {
int u = Q.top().sec; Q.pop();
int dist, v; tie(dist, v) = dis[u].back();
if (dist < k) k -= dist, dis[u].pop_back(), mrk[v] = true;
else {
while (dep[dis[u].back().fir] > k) dis[u].pop_back();
mrk[dis[u].back().sec] = true, k = 0; break;
}
if (dis[u].size()) Q.emplace(dis[u].size(), u);
}
}
for (int l = 1, r; l <= n; l = r + 1) {
r = l;
while (bel[ord[r + 1]] == bel[ord[l]] && mrk[ord[r]]) r ++;
rng[bel[ord[l]]].emplace_back(l, r);
}
{
son.emplace_back(rt);
sort(son.begin(), son.end(), [&](int x, int y) { return rng[x].size() > rng[y].size(); });
// for (auto u : son) for (auto _ : rng[u]) cout << u << ": " << '[' << _.fir << ',' << _.sec << ']' << endl;
// cout << endl;
assert(son.size() >= 2);
int p1 = 0, p2 = 1;
while (p1 < son.size() && p2 < son.size()) {
int u = son[p1], v = son[p2];
while (rng[u].size() && rng[v].size()) {
int l1, r1, l2, r2;
tie(l1, r1) = rng[u].back(), rng[u].pop_back();
tie(l2, r2) = rng[v].back(), rng[v].pop_back();
for (int i = l1; i <= r1; i ++) ans.emplace_back(ord[i]);
for (int i = l2; i <= r2; i ++) ans.emplace_back(ord[i]);
}
if (rng[u].empty()) cmax(p1, p2), p1 ++;
if (rng[v].empty()) cmax(p2, p1), p2 ++;
}
p1 = min(p1, p2);
if (p1 < son.size() && rng[son[p1]].size()) {
assert(rng[son[p1]].size() == 1);
int l, r; tie(l, r) = rng[son[p1]][0];
for (int i = l; i <= r; i ++) Write(ord[i], ' ');
}
for (auto u : ans) Write(u, ' '); Puts("");
};
return;
}
void Clear() {
rt = dfc = 0; while (Q.size()) Q.pop();
son.clear(), ans.clear();
for (int i = 1; i <= n; i ++) {
G[i].clear(), sz[i] = mxp[i] = dfn[i] = ord[i] = dep[i] = 0;
bel[i] = 0, mrk[i] = false, dis[i].clear(), rng[i].clear();
for (int j = 0; j < LG; j ++) st[j][i] = {0, 0};
}
return;
}
}
namespace Solve1 {
void Main() {
return;
}
void Clear() {
return;
}
}
void slv() {
n = Read<int>(), k = Read<int>(), op = Read<int>();
if (n == 1) { k ? Puts("-1") : Puts("1"); return; }
if (n == 2) { k == op ? Puts("1 2") : Puts("-1"); return; }
return op == 1 ? (Solve1::Main(), Solve1::Clear()) : (Solve2::Main(), Solve2::Clear());
}
void clr() {
return;
}
bool Med;
int main() {
#ifdef LOCAL
freopen("!in.in", "r", stdin);
freopen("!out.out", "w", stdout);
fprintf(stderr, "%.3lf Mb\n", fabs((&Mbe - &Med) / 1048576.0));
#endif
// int T = 1;
int T = Read<int>();
while (T --) slv(), clr();
Flush();
#ifdef LOCAL
fprintf(stderr, "%d ms\n", (int)clock());
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 2
Accepted
Test #1:
score: 2
Accepted
time: 7ms
memory: 39000kb
input:
0
output:
result:
ok Accepted.
Subtask #2:
score: 0
Wrong Answer
Test #2:
score: 0
Wrong Answer
time: 8ms
memory: 91856kb
input:
5 9 28 2 8 6 6 3 3 2 9 5 4 1 1 5 5 2 2 7 9 16 2 1 3 7 8 4 2 2 9 9 8 8 6 6 3 3 5 9 12 1 7 5 5 9 9 2 2 8 6 1 1 8 8 4 4 3 9 22 1 8 5 2 4 4 1 1 6 6 9 7 9 9 5 5 3 9 27 1 2 6 4 8 8 1 5 9 9 1 1 7 7 6 6 3
output:
4 6 8 1 3 9 7 5 2 8 3 1 5 7 6 9 2 4 -1 -1
result:
wrong output format Unexpected end of file - int32 expected
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Memory Limit Exceeded
Test #16:
score: 0
Memory Limit Exceeded
input:
200 9 38 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 26 2 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 10 30 2 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 9 30 2 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 10 28 2 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 41 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 44 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #3:
0%
Subtask #7:
score: 0
Skipped
Dependency #4:
0%
Subtask #8:
score: 0
Skipped
Dependency #7:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%