HDU 1824 Let's go home (2-SAT判定)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Let's go home

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1616    Accepted Submission(s): 661


Problem Description小時候,鄉愁是一枚小小的郵票,我在這頭,母親在那頭。
                        —— 余光中

集訓是辛苦的,道路是坎坷的,休息還是必須的。經過一段時間的訓練,lcy決定讓大家回家放鬆一下,但是訓練還是得照常進行,lcy想出了如下回家規定,每一個隊(三人一隊)或者隊長留下或者其餘兩名隊員同時留下;每一對隊員,如果隊員A留下,則隊員B必須回家休息下,或者B留下,A回家。由於今年集訓隊人數突破往年同期最高記錄,管理難度相當大,lcy也不知道自己的決定是否可行,所以這個難題就交給你了,呵呵,好處嘛~,免費**漂流一日。
 
Input第一行有兩個整數,T和M,1<=T<=1000表示隊伍數,1<=M<=5000表示對數。
接下來有T行,每行三個整數,表示一個隊的隊員編號,第一個隊員就是該隊隊長。
然後有M行,每行兩個整數,表示一對隊員的編號。
每個隊員只屬於一個隊。隊員編號從0開始。
 
Output可行輸出yes,否則輸出no,以EOF為結束。
 
Sample Input
1 20 1 20 11 22 40 1 23 4 50 30 41 31 4
 
Sample Output
yesno
 
Author威士忌 
SourceHDOJ 2007 Summer Exercise(3)- Hold by Wiskey解題思路:2-SAT就是兩者有衝突就連邊。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <stack>#include <set>#include <map>#define LL long long using namespace std;const int MAXN = 20010;vector<int>G[MAXN];int pre[MAXN], lowlink[MAXN], sccno[MAXN], dfs_clock, scc_cnt;stack<int> s;void dfs(int u){pre[u] = lowlink[u] = ++dfs_clock;s.push(u); int sz = G[u].size();for(int i=0;i<sz;i++){int v = G[u][i];if(!pre[v]){dfs(v);lowlink[u] = min(lowlink[u], lowlink[v]);}else if(!sccno[v]){lowlink[u] = min(lowlink[u], pre[v]);}}if(lowlink[u] == pre[u]){scc_cnt++;for(;;){int x = s.top(); s.pop();sccno[x] = scc_cnt;if(x == u) break;}}}void find_scc(int n){dfs_clock = scc_cnt = 0;memset(sccno, 0, sizeof(sccno));memset(pre, 0, sizeof(pre));for(int i=1;i<=n;i++) if(!pre[i])dfs(i);}int N, M, T;int main(){while(scanf("%d%d", &T, &M)!=EOF){int x, y, z;N = 3 * T;for(int i=0;i<=2*N;i++) G[i].clear();for(int i=1;i<=T;i++){scanf("%d%d%d", &x, &y, &z);x++; y++; z++;G[x+N].push_back(y);G[x+N].push_back(z);G[y+N].push_back(x);G[z+N].push_back(x);}for(int i=1;i<=M;i++){scanf("%d%d", &x, &y);x++; y++;G[x].push_back(y + N);G[y].push_back(x + N);}find_scc(2 * N);int flag = 1;for(int i=1;i<=N;i++){if(sccno[i] == sccno[i + N]){flag = 0;break;}}if(flag) puts("yes");else puts("no");}return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.