HDU 3715 Go Deeper(2-sat)

來源:互聯網
上載者:User

標籤: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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.