QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#739651 | #9570. Binary Tree | yzhx | Compile Error | / | / | C++20 | 3.5kb | 2024-11-12 22:35:26 | 2024-11-12 22:35:30 |
Judging History
answer
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
void solve() {
int n;
cin >> n;
cout.flush();
vector<vector<int>> g(n + 1);
for (int i = 1; i <= n; i++) {
int ls, rs;
cin >> ls >> rs;
cout.flush();
if (ls) {
g[i].pb(ls);
g[ls].pb(i);
}
if (rs) {
g[i].pb(rs);
g[rs].pb(i);
}
}
vector<int> siz(n + 1);
vector<int> w(n + 1);
int p = 1, tot = n;
vector<int> ban(n + 1);
std::function<void(int, int)> dfs1 = [&](int u, int fa) {
siz[u] = 1;
for (int v : g[u]) {
if (ban[v] or v == fa) continue;
dfs1(v, u);
siz[u] += siz[v];
}
};
std::function<void(int, int)> dfs2 = [&](int u, int fa) {
w[u] = 0;
for (int v : g[u]) {
if (ban[v] or v == fa) continue;
dfs2(v, u);
w[u] = max(w[u], siz[v]);
}
w[u] = max(w[u], tot - siz[u]);
if (w[u] <= tot / 2) {
p = u;
}
};
using std::endl;
auto qry = [&](int x, int y) {
cout << "? " << x << " " << y << endl;
cout.flush();
int t; cin >> t;
cout.flush();
return t;
};
while (tot > 1) {
dfs1(p, 0);
tot = siz[p];
dfs2(p, 0);
int d = 0;
for (int v : g[p]) {
if (!ban[v]) d++;
}
if (d == 0) {
cout << "! " << p << endl;
cout.flush();
return;
} else if (d == 1) {
int x = p, y = 0;
for (int tmp : g[p]) {
if (!ban[tmp]) {
y = tmp;
break;
}
}
int t = qry(x, y);
if (t == 0) {
cout << "! " << x << endl;
cout.flush();
return;
} else if (t == 2) {
ban[x] = 1;
p = y;
}
} else if (d == 2) {
int x = 0, y = 0;
for (int v : g[p]) {
if (!ban[v]) {
if (!x) x = v;
else y = v;
}
}
int t = qry(x, y);
if (t == 0) {
ban[p] = ban[y] = 1;
p = x;
} else if (t == 1) {
cout << "! " << p << endl;
cout.flush();
return;
} else {
ban[p] = ban[x] = 1;
p = y;
}
} else if (d == 3) {
int x = g[p][0], y = g[p][1], z = g[p][2];
int sx = siz[x], sy = siz[y], sz = siz[z];
if (sx < sy) swap(x, y), swap(sx, sy);
if (sx < sz) swap(x, z), swap(sx, sz);
if (sy < sz) swap(y, z), swap(sy, sz);
int t = qry(x, y);
if (t == 1) {
ban[x] = ban[y] = 1;
} else if (t == 0) {
ban[y] = ban[z] = ban[p] = 1;
p = x;
} else {
ban[p] = ban[x] = ban[z] = 1;
p = y;
}
}
}
}
int main() {
int times;
std::cin >> times;
while (times--) {
solve();
}
return 0;
}
/*
1
7
2 0
3 4
0 0
5 6
0 0
0 7
0 0
*/
Details
answer.code: In function ‘void solve()’: answer.code:9:5: error: ‘cin’ was not declared in this scope; did you mean ‘std::cin’? 9 | cin >> n; | ^~~ | std::cin In file included from answer.code:4: /usr/include/c++/13/iostream:62:18: note: ‘std::cin’ declared here 62 | extern istream cin; ///< Linked to standard input | ^~~ answer.code:10:5: error: ‘cout’ was not declared in this scope; did you mean ‘std::cout’? 10 | cout.flush(); | ^~~~ | std::cout /usr/include/c++/13/iostream:63:18: note: ‘std::cout’ declared here 63 | extern ostream cout; ///< Linked to standard output | ^~~~ answer.code:12:12: error: ‘vector’ was not declared in this scope 12 | vector<vector<int>> g(n + 1); | ^~~~~~ answer.code:12:12: note: suggested alternatives: In file included from /usr/include/c++/13/vector:66, from answer.code:5: /usr/include/c++/13/bits/stl_vector.h:425:11: note: ‘std::vector’ 425 | class vector : protected _Vector_base<_Tp, _Alloc> | ^~~~~~ /usr/include/c++/13/vector:86:13: note: ‘std::pmr::vector’ 86 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>; | ^~~~~~ answer.code:12:19: error: expected primary-expression before ‘int’ 12 | vector<vector<int>> g(n + 1); | ^~~ answer.code:20:13: error: ‘g’ was not declared in this scope 20 | g[i].pb(ls); | ^ answer.code:24:13: error: ‘g’ was not declared in this scope 24 | g[i].pb(rs); | ^ answer.code:29:12: error: expected primary-expression before ‘int’ 29 | vector<int> siz(n + 1); | ^~~ answer.code:30:12: error: expected primary-expression before ‘int’ 30 | vector<int> w(n + 1); | ^~~ answer.code:33:12: error: expected primary-expression before ‘int’ 33 | vector<int> ban(n + 1); | ^~~ answer.code:35:10: error: ‘function’ is not a member of ‘std’ 35 | std::function<void(int, int)> dfs1 = [&](int u, int fa) { | ^~~~~~~~ answer.code:6:1: note: ‘std::function’ is defined in header ‘<functional>’; did you forget to ‘#include <functional>’? 5 | #include <vector> +++ |+#include <functional> 6 | answer.code:35:32: error: expression list treated as compound expression in functional cast [-fpermissive] 35 | std::function<void(int, int)> dfs1 = [&](int u, int fa) { | ^ answer.code:35:19: error: expected primary-expression before ‘void’ 35 | std::function<void(int, int)> dfs1 = [&](int u, int fa) { | ^~~~ answer.code:43:10: error: ‘function’ is not a member of ‘std’ 43 | std::function<void(int, int)> dfs2 = [&](int u, int fa) { | ^~~~~~~~ answer.code:43:10: note: ‘std::function’ is defined in header ‘<functional>’; did you forget to ‘#include <functional>’? answer.code:43:32: error: expression list treated as compound expression in functional cast [-fpermissive] 43 | std::function<void(int, int)> dfs2 = [&](int u, int fa) { | ^ answer.code:43:19: error: expected primary-expression before ‘void’ 43 | std::function<void(int, int)> dfs2 = [&](int u, int fa) { | ^~~~ answer.code:66:9: error: ‘dfs1’ was not declared in this scope 66 | dfs1(p, 0); | ^~~~ answer.code:67:15: error: ‘siz’ was not declared in this scope; did you mean ‘sin’? 67 | tot = siz[p]; | ^~~ | sin answer.code:68:9: error: ‘dfs2’ was not declared in this scope 68 | dfs2(p, 0); | ^~~~ answer.code:71:22: error: ‘g’ was not declared in this scope 71 | for (int v : g[p]) { | ^ answer.code:72:18: error: ‘ban’ was not declared in this scope; did you mean ‘tan’? 72 | if (!ban[v]) d++; | ^~~ | tan answer.code:81:28: error: ‘g’ was not declared in this scope 81 | for (int tmp : g[p]) { | ^ answer.code:82:22: error: ‘ban’ was not declared in this scope; did you mean ‘tan’? 82 | if (!ban[tmp]) { | ^~~ | tan answer.code:94:17: error: ‘ban’ was not declared in this scope; did you mean ‘tan’? 94 | ban[x] = 1; | ^~~ | tan answer.code:99:26: error: ‘g’ was not declared in this scope 99 | for (int v : g[p]) { | ^ answer.code:100:22: error: ‘ban’ was not declared in this scope; did you mean ‘tan’? 100 | if (!ban[v]) { | ^~~ | tan answer.code:108:17...