QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344525 | #837. Giant Penguin | JWRuixi | WA | 40ms | 21896kb | C++17 | 3.3kb | 2024-03-04 18:30:12 | 2024-03-04 18:30:12 |
Judging History
answer
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
using namespace std;
using vi = vector<int>;
constexpr int N = 1e5 + 9;
constexpr int M = 2e5 + 9;
constexpr int K = 12;
constexpr int INF = 1e9;
int n, m, k, Q, as[M];
vi G[N];
struct qNode {
int o, x, y;
qNode (int o = 0, int x = 0, int y = 0) : o(o), x(x), y(y) {}
};
vector<qNode> q[N], qq;
bool h[N];
vi e[N], g[N];
IL void dfs (int u, int fa) {
h[u] = 1;
for (int v : G[u]) {
if (v ^ fa) {
if (h[v]) {
e[u].eb(v);
} else {
g[u].eb(v);
dfs(v, u);
}
}
}
if (fa) {
g[u].eb(fa);
}
}
bool used[N], vis[N];
vi vc, key;
IL void dfs1 (int u, int fa) {
vc.eb(u);
if (!q[u].empty()) {
int sz = qq.size();
qq.insert(qq.end(), q[u].begin(), q[u].end());
inplace_merge(qq.begin(), qq.begin() + sz, qq.end(), [] (auto i, auto j) {
return i.y < j.y;
});
}
bool fl = 0;
for (int v : g[u]) {
if (used[v] || v == fa) {
continue;
}
if (vis[v]) {
fl = 1;
} else {
dfs1(v, u);
}
}
if (fl) {
key.eb(u);
}
}
IL void dfs2 (int u, int fa) {
vis[u] = 1;
for (int v : g[u]) {
if (!used[v] && v ^ fa) {
dfs2(v, u);
}
}
}
int R, sz[N], S;
IL void dfs3 (int u, int fa) {
sz[u] = 1;
int mx = 0;
for (int v : g[u]) {
if (used[v] || v == fa) {
continue;
}
dfs3(v, u);
sz[u] += sz[v];
mx = max(mx, sz[v]);
}
mx = max(mx, S - sz[u]);
if (mx <= S / 2) {
R = u;
}
}
IL void bfs (int s, int *d) {
for (auto v : vc) {
d[v] = INF;
}
queue<int> q;
d[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : G[u]) {
if (vis[v] && d[v] > d[u] + 1) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
}
int d[K][N], mn[K];
IL void dfz (int u) {
vc = key = {u};
qq = q[u];
vis[u] = 1;
for (int v : G[u]) {
if (!used[v] && !vis[v]) {
dfs1(v, u);
dfs2(v, u);
}
}
int kk = key.size();
L (i, 0, kk - 1) {
bfs(key[i], d[i]);
mn[i] = INF;
}
L (i, 1, (int)qq.size() - 1) {
assert(qq[i].y > qq[i - 1].y);
}
for (auto [o, x, y] : qq) {
if (o & 1) {
L (i, 0, kk - 1) {
mn[i] = min(mn[i], d[i][x]);
}
} else {
L (i, 0, kk - 1) {
as[y] = min(as[y], mn[i] + d[i][x]);
}
}
}
for (int v : vc) {
vis[v] = 0;
}
used[u] = 1;
dfs3(u, 0);
for (int v : G[u]) {
if (!used[v]) {
S = sz[v];
dfs3(v, u);
dfz(R);
}
}
}
int main () {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
L (i, 1, m) {
int u, v;
cin >> u >> v;
G[u].eb(v);
G[v].eb(u);
}
cin >> Q;
fill(as + 1, as + Q + 1, -1);
L (i, 1, Q) {
int o, x;
cin >> o >> x;
q[x].eb(o, x, i);
if (o ^ 1) {
as[i] = INF;
}
}
dfs(1, 0);
S = n;
dfs3(1, 0);
dfz(R);
L (i, 1, Q) {
if (~as[i]) {
cout << as[i] << '\n';
}
}
}
// I love WHQ!
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 14820kb
input:
5 4 0 1 2 2 3 3 4 4 5 7 1 1 1 5 2 1 2 2 2 3 2 4 2 5
output:
0 1 2 1 0
result:
ok 5 number(s): "0 1 2 1 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 15928kb
input:
5 6 2 1 2 2 3 1 3 3 4 4 5 3 5 3 1 1 2 4 2 5
output:
2 2
result:
ok 2 number(s): "2 2"
Test #3:
score: 0
Accepted
time: 34ms
memory: 21896kb
input:
100 99 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...
output:
99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 99 98 97 9...
result:
ok 199968 numbers
Test #4:
score: 0
Accepted
time: 31ms
memory: 21212kb
input:
100 99 0 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61...
output:
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
result:
ok 199966 numbers
Test #5:
score: 0
Accepted
time: 31ms
memory: 20876kb
input:
100 99 0 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 2 25 1 26 1 27 1 28 2 29 1 30 1 31 1 32 1 33 2 34 1 35 1 36 2 37 1 38 4 39 1 40 1 41 2 42 2 43 1 44 1 45 2 46 1 47 1 48 1 49 2 50 2 51 1 52 1 53 1 54 2 55 3 56 2 57 1 58 2 59 2 60 3 61...
output:
2 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 4 3 3 3 3 4 3 3 4 3 2 3 3 4 4 3 3 4 3 3 3 4 4 3 3 3 4 4 4 3 4 4 4 3 3 4 3 3 3 4 4 4 3 3 3 3 3 4 3 3 3 3 3 3 3 3 3 3 4 4 3 0 3 3 4 3 4 3 3 4 3 4 2 3 3 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 3 4 3 3 3 3 4 3 3 4 3 2 3 3 4 4 3 3 4 3 3 3 4 ...
result:
ok 199964 numbers
Test #6:
score: 0
Accepted
time: 36ms
memory: 21256kb
input:
100 99 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 28 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 39 41 41 42 41 43 41 44 44 45 44 46 46 47 44 48 48 49 48 50 50 51 50 52 51...
output:
68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 42 40 39 38 37 36 35 34 33 32 31 32 30 31 31 29 30 30 31 28 29 27 28 26 29 25 24 23 24 22 25 26 21 22 23 20 19 20 18 17 18 16 15 14 16 13 14 15 12 16 11 10 9 10 8 16 7 6 5 6 7 4 7 8 9 10 11 3 10 2 1 0 68 67 66 65 64 ...
result:
ok 199966 numbers
Test #7:
score: -100
Wrong Answer
time: 40ms
memory: 20452kb
input:
100 102 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 ...
output:
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 39 38 37 36 35 34 33 32 31 30 29 30 31 32 33 34 35 36 37 38 39 40 39 38 37 36 35 34 33 32 31 30 29 28 27 24 23 22 21 20 19 1...
result:
wrong answer 64th numbers differ - expected: '37', found: '39'