QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371452 | #997. 2-SAT 问题 | Isrothy | Compile Error | / | / | C++23 | 2.2kb | 2024-03-30 12:30:52 | 2024-03-30 12:30:52 |
Judging History
answer
#include <optional>
#include <stack>
#include <vector>
struct TwoSAT {
std::stack<int> stk{};
std::vector<int> sccno, dfn, low;
std::vector<std::vector<int>> adj;
std::vector<bool> on_stack;
int n, scc_cnt, idx;
explicit TwoSAT(int n)
: sccno(2 * n), dfn(2 * n), low(2 * n), adj(2 * n), on_stack(2 * n), n(n), scc_cnt(0),
idx(0) {}
void add_clause(int x, int xval, int y, int yval) {
x = x * 2 + xval;
y = y * 2 + yval;
adj[x ^ 1].push_back(y);
adj[y ^ 1].push_back(x);
}
void Tarjan(int u) {
dfn[u] = low[u] = ++idx;
stk.push(u);
on_stack[u] = true;
for (auto v: adj[u]) {
if (!dfn[v]) {
Tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (on_stack[v]) {
low[u] = std::min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
++scc_cnt;
while (true) {
auto x = stk.top();
stk.pop();
on_stack[x] = false;
sccno[x] = scc_cnt;
if (x == u) {
break;
}
}
}
}
auto solve() -> std::optional<std::vector<bool>> {
for (int i = 0; i < 2 * n; ++i) {
if (!dfn[i]) {
Tarjan(i);
}
}
std::vector<bool> res(n);
for (int i = 0; i < n; ++i) {
if (sccno[i * 2] == sccno[i * 2 + 1]) {
return std::nullopt;
}
res[i] = sccno[i * 2] > sccno[i * 2 + 1];
}
return res;
}
};
int main() {
int n, m;
scanf("%d%d", &n, &m);
auto sat = TwoSAT(n);
for (int i = 0; i < m; ++i) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
sat.add_clause(a - 1, b, c - 1, d);
}
auto answer = sat.solve();
if (answer.has_value()) {
puts("Yes");
for (auto x: answer.value()) {
printf("%d ", x ? 1 : 0);
}
puts("");
} else {
puts("No");
}
return 0;
}
Details
answer.code: In function ‘int main()’: answer.code:63:5: error: ‘scanf’ was not declared in this scope 63 | scanf("%d%d", &n, &m); | ^~~~~ answer.code:72:9: error: ‘puts’ was not declared in this scope 72 | puts("Yes"); | ^~~~ answer.code:74:13: error: ‘printf’ was not declared in this scope 74 | printf("%d ", x ? 1 : 0); | ^~~~~~ answer.code:4:1: note: ‘printf’ is defined in header ‘<cstdio>’; did you forget to ‘#include <cstdio>’? 3 | #include <vector> +++ |+#include <cstdio> 4 | struct TwoSAT { answer.code:78:9: error: ‘puts’ was not declared in this scope 78 | puts("No"); | ^~~~