標籤:style blog http color io os ar for sp
HDU 3715 Go Deeper
題目連結
題意:根據題意那個函數,構造x數組,問最大能遞迴層數
思路:轉化為2-sat問題,由於x只能是0,1,c只能是0,1,2那麼問題就好辦了,對於0, 1, 2對應分別是3種運算式,然後二分深度,搞2-sat即可
代碼:
#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <algorithm>using namespace std;const int MAXNODE = 205;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;const int N = 10005;int t, n, m;int a[N], b[N], c[N];bool judge(int dep) {gao.init(n);for (int i = 0; i < dep; i++) {if (c[i] == 0)gao.add_Edge(a[i], 1, b[i], 1);else if (c[i] == 1) {gao.add_Edge(a[i], 0, a[i], 1);gao.add_Edge(a[i], 0, b[i], 1);gao.add_Edge(b[i], 0, a[i], 1);gao.add_Edge(b[i], 0, b[i], 1);} elsegao.add_Edge(a[i], 0, b[i], 0);}return gao.solve();}int main() {scanf("%d", &t);while (t--) {scanf("%d%d", &n, &m);for (int i = 0; i < m; i++)scanf("%d%d%d", &a[i], &b[i], &c[i]);int l = 0, r = m + 1;while (l < r) {int mid = (l + r) / 2;if (judge(mid)) l = mid + 1;else r = mid;}printf("%d\n", l - 1);}return 0;}
HDU 3715 Go Deeper(2-sat)