HDU 1824 Let's go home
思路:把表达式拆分掉,一队拆成a^b & a^c, 然后建图跑2-sat即可
代码:
#include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; const int MAXNODE = 3005; struct TwoSet { int n; vector<int> g[MAXNODE * 2]; bool mark[MAXNODE * 2]; int S[MAXNODE * 2], sn; void init(int tot) { n = tot * 2; for (int i = 0; i < n; i += 2) { g[i].clear(); g[i^1].clear(); } memset(mark, false, sizeof(mark)); } void add_Edge(int u, int uval, int v, int vval) { u = u * 2 + uval; v = v * 2 + vval; g[u^1].push_back(v); g[v^1].push_back(u); } void delete_Edge(int u, int uval, int v, int vval) { u = u * 2 + uval; v = v * 2 + vval; g[u^1].pop_back(); g[v^1].pop_back(); } bool dfs(int u) { if (mark[u^1]) return false; if (mark[u]) return true; mark[u] = true; S[sn++] = u; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!dfs(v)) return false; } return true; } bool solve() { for (int i = 0; i < n; i += 2) { if (!mark[i] && !mark[i + 1]) { sn = 0; if (!dfs(i)){ for (int j = 0; j < sn; j++) mark[S[j]] = false; sn = 0; if (!dfs(i + 1)) return false; } } } return true; } } gao; int n, m; int main() { while (~scanf("%d%d", &n, &m)) { gao.init(n * 3); int a, b, c; while (n--) { scanf("%d%d%d", &a, &b, &c); gao.add_Edge(a, 1, b, 1); gao.add_Edge(a, 1, c, 1); } while (m--) { scanf("%d%d", &a, &b); gao.add_Edge(a, 0, b, 0); gao.add_Edge(a, 0, b, 0); } printf("%s\n", gao.solve() ? "yes" : "no"); } return 0; }
有疑问加站长微信联系(非本文作者)