QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#449283 | #8359. travel | definieren | 4 | 10ms | 52384kb | C++14 | 7.6kb | 2024-06-20 21:25:45 | 2024-06-20 21:25:46 |
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 (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 {
vector<int> G[N];
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);
}
Puts("-1");
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: 51048kb
input:
0
output:
result:
ok Accepted.
Subtask #2:
score: 2
Acceptable Answer
Test #2:
score: 2
Acceptable Answer
time: 3ms
memory: 52384kb
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 8 1 3 6 9 7 5 2 7 9 2 4 6 3 1 5 8 -1 -1 -1
result:
points 0.50 Partially Correct | type = 2
Test #3:
score: 2
Acceptable Answer
time: 6ms
memory: 52108kb
input:
5 9 16 2 4 3 3 1 7 6 2 8 8 6 6 1 1 5 9 5 9 12 1 6 7 5 4 4 1 1 3 3 2 2 7 8 7 7 9 9 14 1 1 2 2 3 6 4 8 9 9 3 3 4 5 4 4 7 9 12 1 9 4 4 1 1 5 3 2 2 6 6 5 8 5 5 7 9 9 1 8 7 7 3 3 2 2 9 4 9 9 1 1 6 6 5
output:
3 4 6 7 8 2 5 9 1 -1 -1 -1 -1
result:
points 0.50 Partially Correct | type = 2
Test #4:
score: 2
Acceptable Answer
time: 7ms
memory: 52200kb
input:
5 9 13 1 4 7 7 8 8 5 1 9 2 5 3 5 5 9 9 6 9 16 2 7 3 1 5 5 8 8 9 2 3 4 9 9 3 3 6 9 26 1 1 3 3 8 8 9 7 4 2 6 6 9 9 4 4 5 9 25 1 5 3 7 2 2 4 4 9 6 1 1 9 9 3 3 8 9 14 1 3 8 8 5 5 4 7 6 6 4 4 2 2 9 9 1
output:
-1 8 5 1 4 3 7 2 6 9 -1 -1 -1
result:
points 0.50 Partially Correct | type = 2
Test #5:
score: 2
Acceptable Answer
time: 4ms
memory: 50588kb
input:
5 9 27 1 8 2 6 9 9 1 4 5 5 1 1 2 2 7 7 3 9 16 2 2 6 6 9 7 8 8 4 1 4 5 4 4 9 9 3 9 30 1 6 7 9 4 4 2 2 5 1 5 5 3 3 7 7 8 9 12 1 4 1 8 6 6 3 2 5 5 1 1 3 3 7 7 9 9 22 1 2 3 7 8 8 1 1 9 9 5 5 3 3 4 6 4
output:
-1 4 8 7 1 5 9 6 2 3 -1 -1 -1
result:
points 0.50 Partially Correct | type = 2
Test #6:
score: 2
Acceptable Answer
time: 8ms
memory: 50680kb
input:
5 9 18 2 7 3 3 8 8 4 4 9 9 1 1 5 2 6 5 6 9 26 2 8 3 3 9 9 5 4 2 2 5 6 7 7 5 5 1 9 25 1 1 5 5 9 9 6 2 4 7 6 6 8 8 4 4 3 9 26 2 1 9 9 4 4 6 5 7 3 7 7 6 6 2 2 8 9 26 2 1 4 4 6 6 8 3 9 9 2 5 8 8 7 7 2
output:
8 3 7 1 5 6 2 4 9 5 8 4 3 2 9 6 1 7 -1 1 3 9 5 4 7 2 8 6 3 6 4 1 9 5 7 2 8
result:
points 0.50 Partially Correct | type = 2
Test #7:
score: 2
Acceptable Answer
time: 8ms
memory: 50624kb
input:
5 9 13 1 2 4 5 8 8 1 6 9 9 7 7 1 1 4 4 3 9 29 2 3 2 1 8 8 4 4 6 6 7 5 2 2 9 9 7 9 15 2 1 3 3 7 7 2 2 9 4 5 5 9 9 6 6 8 9 31 1 2 9 9 8 8 6 6 5 5 4 7 4 4 1 1 3 9 21 2 7 2 2 5 1 4 3 6 6 5 9 4 4 5 5 8
output:
-1 -1 -1 -1 -1
result:
points 0.50 Partially Correct | type = 2
Test #8:
score: 4
Accepted
time: 8ms
memory: 50420kb
input:
5 1 0 1 1 0 2 1 0 2 1 0 1 1 0 2
output:
1 1 1 1 1
result:
ok Accepted.
Subtask #3:
score: 0
Wrong Answer
Dependency #2:
50%
Acceptable Answer
Test #9:
score: 4
Acceptable Answer
time: 4ms
memory: 50636kb
input:
5 13 37 1 3 12 12 7 4 13 13 11 11 8 8 6 1 2 10 9 9 7 7 5 5 6 6 2 13 42 1 2 4 4 13 13 5 5 7 7 12 8 6 6 9 1 10 10 11 3 9 9 12 12 11 13 15 1 9 2 2 11 12 4 4 5 5 6 6 3 3 11 8 13 13 1 7 11 11 10 10 1 13 48 1 11 3 3 4 12 1 7 5 5 8 8 2 2 1 10 1 1 4 4 6 13 9 9 6 13 68 2 13 4 6 5 2 1 1 9 9 11 11 12 12 7 7 5 ...
output:
-1 -1 -1 -1 2 10 1 3 9 13 11 8 4 12 6 7 5
result:
points 0.50 Partially Correct | type = 2
Test #10:
score: 0
Wrong Answer
time: 10ms
memory: 50584kb
input:
5 13 34 2 2 7 5 11 13 1 1 3 8 9 9 6 6 11 11 3 3 7 7 12 12 4 4 10 13 17 1 2 7 7 11 11 9 10 12 12 4 6 13 13 4 4 8 8 3 3 1 1 9 9 5 13 33 1 7 6 6 1 1 10 10 13 9 4 11 12 12 13 8 13 2 4 4 13 13 3 5 3 13 17 1 8 11 11 13 13 3 3 5 10 2 2 1 1 6 12 7 6 5 5 4 4 9 9 7 13 15 1 5 1 8 3 2 13 13 11 11 6 6 7 7 9 9 1 ...
output:
11 8 1 13 6 9 7 2 12 4 10 5 3 -1 -1 -1 -1
result:
wrong answer Wrong Answer.
Subtask #4:
score: 0
Runtime Error
Test #16:
score: 0
Runtime Error
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:
50%
Acceptable Answer
Dependency #3:
0%