hdu 2sat题集

【HDU】

3062 Party

  1 /*Author :usedrose  */
  2 /*Created Time :2015/6/2 11:26:32*/
  3 /*File Name :2.cpp*/
  4 #include <cstdio>
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <sstream>
  8 #include <cstdlib>
  9 #include <cstring>
 10 #include <climits>
 11 #include <vector>
 12 #include <string>
 13 #include <ctime>
 14 #include <cmath>
 15 #include <deque>
 16 #include <queue>
 17 #include <stack>
 18 #include <set>
 19 #include <map>
 20 #define INF 0x3f3f3f3f
 21 #define eps 1e-8
 22 #define pi acos(-1.0)
 23 #define MAXN 2110
 24 #define OK cout << "ok" << endl;
 25 #define o(a) cout << #a << " = " << a << endl
 26 #define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
 27 using namespace std;
 28 typedef long long LL;
 29 
 30 int n, m;
 31 vector<int> G[2*MAXN];
 32 int instk[MAXN], dfn[MAXN], low[MAXN], scc[MAXN];
 33 int Time, cnt;
 34 stack<int> S;
 35 
 36 void init()
 37 {
 38     while (!S.empty()) S.pop();
 39     Time = cnt = 0;
 40     for (int i = 0;i < n; ++ i) {
 41         G[i].clear();
 42         scc[i] = dfn[i] = low[i] = instk[i] = 0;
 43     }
 44 }
 45 
 46 void Tarjan(int u)
 47 {
 48     dfn[u] = low[u] = ++Time;
 49     S.push(u);
 50     instk[u] = 1;
 51     for (int i = 0;i < G[u].size(); ++ i) {
 52         int v = G[u][i];
 53         if (!dfn[v]) {
 54             Tarjan(v);
 55             low[u] = min(low[u], low[v]);
 56         }
 57         else if (instk[v])
 58             low[u] = min(low[u], dfn[v]); 
 59     }
 60     if (low[u] == dfn[u]) {
 61         cnt ++;
 62         int v;
 63         do {
 64             v = S.top();
 65             S.pop();
 66             instk[v] = 0;
 67             scc[v] = cnt; 
 68         }while (v != u);
 69     }
 70 }
 71 
 72 
 73 void find_scc()
 74 {
 75     for (int i = 0;i < n; ++ i)
 76         if (!dfn[i]) 
 77             Tarjan(i);
 78 }
 79 
 80 void gao()
 81 {
 82     for (int i = 0;i < n; i += 2)
 83         if (scc[i] == scc[i^1]) {
 84             puts("NO");        
 85             return;
 86         }
 87     puts("YES");
 88     return;
 89 }
 90 
 91 int main()
 92 {
 93     //freopen("data.in","r",stdin);
 94     //freopen("data.out","w",stdout);
 95     cin.tie(0);
 96     ios::sync_with_stdio(false);
 97     int a1, a2, c1, c2;
 98     while (cin >> n) {
 99         n*= 2;
100         init();
101         cin >> m;
102         for (int i = 0;i < m; ++ i) {
103             cin >> a1 >> a2 >> c1 >> c2;
104             G[2*a1+c1].push_back(2*a2 + 1 - c2);
105             G[2*a2+c2].push_back(2*a1 + 1 - c1);
106         }
107         find_scc();
108         gao();
109     } 
110        return 0;
111 }
View Code

1824 Let's go home

  1 /*Author :usedrose  */
  2 /*Created Time :2015/6/2 11:26:32*/
  3 /*File Name :2.cpp*/
  4 #include <cstdio>
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <sstream>
  8 #include <cstdlib>
  9 #include <cstring>
 10 #include <climits>
 11 #include <vector>
 12 #include <string>
 13 #include <ctime>
 14 #include <cmath>
 15 #include <deque>
 16 #include <queue>
 17 #include <stack>
 18 #include <set>
 19 #include <map>
 20 #define INF 0x3f3f3f3f
 21 #define eps 1e-8
 22 #define pi acos(-1.0)
 23 #define MAXN 6110
 24 #define OK cout << "ok" << endl;
 25 #define o(a) cout << #a << " = " << a << endl
 26 #define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
 27 using namespace std;
 28 typedef long long LL;
 29 
 30 int n, m;
 31 vector<int> G[2*MAXN];
 32 int instk[MAXN], dfn[MAXN], low[MAXN], scc[MAXN];
 33 int Time, cnt;
 34 stack<int> S;
 35 
 36 void init(int k)
 37 {
 38     while (!S.empty()) S.pop();
 39     Time = cnt = 0;
 40     for (int i = 0;i < k; ++ i) {
 41         G[i].clear();
 42         scc[i] = dfn[i] = low[i] = instk[i] = 0;
 43     }
 44 }
 45 
 46 void Tarjan(int u)
 47 {
 48     dfn[u] = low[u] = ++Time;
 49     S.push(u);
 50     instk[u] = 1;
 51     for (int i = 0;i < G[u].size(); ++ i) {
 52         int v = G[u][i];
 53         if (!dfn[v]) {
 54             Tarjan(v);
 55             low[u] = min(low[u], low[v]);
 56         }
 57         else if (instk[v])
 58             low[u] = min(low[u], dfn[v]); 
 59     }
 60     if (low[u] == dfn[u]) {
 61         cnt ++;
 62         int v;
 63         do {
 64             v = S.top();
 65             S.pop();
 66             instk[v] = 0;
 67             scc[v] = cnt; 
 68         }while (v != u);
 69     }
 70 }
 71 
 72 
 73 void find_scc()
 74 {
 75     for (int i = 0;i < 2*n; ++ i)
 76         if (!dfn[i])
 77             Tarjan(i);
 78 }
 79 
 80 void gao()
 81 {
 82     for (int i = 0;i < n; i ++)
 83         if (scc[i] == scc[i+n]) {
 84             puts("no");        
 85             return;
 86         }
 87     puts("yes");
 88     return;
 89 }
 90 
 91 int main()
 92 {
 93     int a, b, c, t;
 94     while (~scanf("%d%d", &t, &m)) {
 95         n = 3*t;
 96         init(2*n);
 97         for (int i = 0;i < t; ++ i) {
 98             scanf("%d%d%d", &a, &b, &c);
 99             G[a+n].push_back(b);
100             G[a+n].push_back(c);
101             G[c+n].push_back(a);
102             G[b+n].push_back(a);
103         }
104         for (int i = 0;i < m; ++ i) {
105             scanf("%d%d", &a, &b);
106             G[a].push_back(b+n);
107             G[b].push_back(a+n);
108         }
109         find_scc();
110         gao();
111     } 
112        return 0;
113 }
View Code

3622 Bomb Game
3715 Go Deeper
1815 Building roads
1816 Get Luffy Out *
1814 Peaceful Commission

4115 Eliminate the Conflict

4421 Bit Magic

posted @ 2015-08-27 03:17  UsedRose  阅读(192)  评论(0编辑  收藏  举报