HDU 3062:Party(2-SAT入门)

http://acm.hdu.edu.cn/showproblem.php?pid=3062

题意:中文。

思路:裸的2-SAT。判断二元组的两个人是否在同一个强连通分量。

学习地址:http://www.cnblogs.com/ambition/archive/2011/07/30/2-sat.html

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define N 2010
 4 struct Edge {
 5     int u, v, nxt;
 6 } edge[N*N*2];
 7 int head[N], tot, belong[N], num, dfn[N], low[N], vis[N], tid;
 8 stack<int> sta;
 9
10 void Add(int u, int v) {
11     edge[tot] = (Edge) { u, v, head[u] }; head[u] = tot++;
12 }
13
14 void init() {
15     memset(head, -1, sizeof(head));
16     memset(vis, 0, sizeof(vis));
17     memset(dfn, 0, sizeof(dfn));
18     memset(low, 0, sizeof(low));
19     tot = num = tid = 0;
20     while(!sta.empty()) sta.pop();
21 }
22
23 void tarjan(int u) {
24     dfn[u] = low[u] = ++tid;
25     vis[u] = 1; sta.push(u);
26     for(int i = head[u]; ~i; i = edge[i].nxt) {
27         int v = edge[i].v;
28         if(!dfn[v]) {
29             tarjan(v);
30             if(low[u] > low[v]) low[u] = low[v];
31         } else if (vis[v]) {
32             if(low[u] > dfn[v]) low[u] = dfn[v];
33         }
34     }
35     if(low[u] == dfn[u]) {
36         num++;
37         while(true) {
38             int x = sta.top(); sta.pop();
39             belong[x] = num;
40             vis[x] = 0;
41             if(x == u) break;
42         }
43     }
44 }
45
46 int main() {
47     int n, m;
48     while(~scanf("%d", &n)) {
49         scanf("%d", &m); init();
50         for(int i = 0; i < m; i++) {
51             int a, b, c, d;
52             scanf("%d%d%d%d", &a, &b, &c, &d);
53             Add(a * 2 + c, b * 2 + (d + 1) % 2);
54             Add(b * 2 + d, a * 2 + (c + 1) % 2);
55         }
56         for(int i = 0; i < 2 * n; i++) if(!dfn[i]) tarjan(i);
57         bool flag = 1;
58         for(int i = 0; i < n; i++)
59             if(belong[i*2] == belong[i*2+1]) flag = 0;
60         if(flag) puts("YES");
61         else puts("NO");
62     }
63     return 0;
64 }
时间: 2024-08-05 18:30:42

HDU 3062:Party(2-SAT入门)的相关文章

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

HDU 1231 最大连续子序列 --- 入门DP

HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #include <cstdio> #include <cstring> int dp[10005]; int main() { #ifdef _LOCAL freopen("D:\\input.txt", "r", stdin); #endif int n

HDU 3062 Party (2-sat)

题目地址:HDU 3062 2-sat第一发.水题.. 首先假设A,A'为同一组的两个布尔变量且不能同时选择同一组的两个变量.如果存在一种同时选择了A和A'的方案,则该方案无解. 设<X,Y>为选择X就必须选择Y,则基本的建图如下: A,B不能同时选:<A,B'><B,A'>,表示选择A就必须不能选择B,选择B就不能选择A A,B不能同时不选:<A',B><B',A>,表示不选A则必须选B,不选B则必须选A A,B必须同时选或同时不选:<A

HDU 3062 &amp;&amp; HDU 1824 &amp;&amp; POJ 3578 &amp;&amp; BZOJ 1997 2-SAT

一条边<u,v>表示u选那么v一定被选. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxm=21000; 7 const int Maxn=2010; 8 struct EDGE{int to,next;}edge[Maxm]; 9 int T,m

hdu 2147 kiki&#39;s game, 入门基础博弈

博弈的一些概念: 必败点(P点) : 前一个选手(Previous player)将取胜的位置称为必败点. 必胜点(N点) : 下一个选手(Next player)将取胜的位置称为必胜点. 必败(必胜)点属性 (1) 所有终结点是必败点(P点): (2) 从任何必胜点(N点)操作,至少有一种方法可以进入必败点(P点): (3)无论如何操作, 从必败点(P点)都只能进入必胜点(N点). hdu 2147 kiki's game 题意: 在一个m*n的棋盘内,从(1,m)点出发,每次可以进行的移动是

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

HDU 3062

http://acm.hdu.edu.cn/showproblem.php?pid=3062 2sat判定性问题模板 #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <queue> #include <map> using namespace std ; struct node { int s,t,nxt ;

HDU 3062 简单的2-SAT问题

在2-SAT,最让我纠结的还是添加有向线段的函数了 void add_clause(int i,int a,int j,int b){    int m=2*i+a;    int n=2*j+b;    G[m^1].push_back(n);    G[n^1].push_back(m);} 这里a,b因为只有真假两种情况,所以只取0或1,这里表示m V n是正确的,那么意思是取到m^1时,那么n必然得取到 同理取到n^1时,m必然取到,所以两条有向线段就添加成功了 例如这道题给所有夫妻排好

hdu 2767 Proving Equivalences(强连通入门题)

1 /************************************************* 2 Proving Equivalences(hdu 2767) 3 强连通入门题 4 给个有向图,求至少加多少条边使得图是所有点都是强连通的 5 由a->b->c->a易知n个点至少要n条边,每个出度和入度都要大 6 于1.先求所有所有强连通分量,把每个强连通分量看成一个点 7 在找每个点的出度和入度,最后还差的出度和入度的最大值就是 8 答案. 9 10 ************