HDU 3062 Party

Party

Time Limit: 1000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3062
64-bit integer IO format: %I64d      Java class name: Main

有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?

Input

n: 表示有n对夫妻被邀请 (n<= 1000)
m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))

在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2 
A1,A2分别表示是夫妻的编号 
C1,C2 表示是妻子还是丈夫 ,0表示妻子 ,1是丈夫
夫妻编号从 0 到 n -1

Output

如果存在一种情况 则输出YES 
否则输出 NO

Sample Input

2
1
0 1 1 1 

Sample Output

YES

Source

2009 Multi-University Training Contest 16 - Host by NIT

解题:2-SAT。模板题。给每对夫妻编号为2*i 和2*i+1.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 2010;
18 struct arc{
19     int u,v,next;
20     arc(int x = 0,int y = 0,int z = 0){
21         u = x;
22         v = y;
23         next = z;
24     }
25 };
26 arc e[2000100];
27 int dfn[maxn],low[maxn],belong[maxn];
28 bool instack[maxn];
29 int head[maxn],tot,scc,index,n,m;
30 stack<int>stk;
31 void add(int u,int v){
32     e[tot] = arc(u,v,head[u]);
33     head[u] = tot++;
34 }
35 void tarjan(int u){
36     dfn[u] = low[u] = ++index;
37     instack[u] = true;
38     stk.push(u);
39     for(int i = head[u]; ~i; i = e[i].next){
40         if(!dfn[e[i].v]){
41             tarjan(e[i].v);
42             if(low[e[i].v] < low[u])
43                 low[u] = low[e[i].v];
44         }else if(instack[e[i].v] && low[u] > dfn[e[i].v])
45             low[u] = dfn[e[i].v];
46     }
47     if(dfn[u] == low[u]){
48         scc++;
49         int now;
50         do{
51             now = stk.top();
52             stk.pop();
53             belong[now] = scc;
54             instack[now] = false;
55         }while(now != u);
56     }
57
58 }
59 bool solve(){
60     int k = n<<1;
61     while(!stk.empty()) stk.pop();
62     for(int i = 0; i < k; i++)
63         if(!dfn[i]) tarjan(i);
64     for(int i = 0; i < n; i++)
65         if(belong[i<<1] == belong[i<<1|1]) return false;
66     return true;
67 }
68 int main() {
69     int u,v,x,y;
70     while(~scanf("%d %d",&n,&m)){
71         for(int i = 0; i < maxn; i++){
72             dfn[i] = belong[i] = 0;
73             head[i] = -1;
74             instack[i] = false;
75         }
76         tot = scc = index = 0;
77         for(int i = 0; i < m; i++){
78             scanf("%d %d %d %d",&x,&y,&u,&v);
79             x = (x<<1)+u;
80             y = (y<<1)+v;
81             add(x,y^1);
82             add(y,x^1);
83         }
84         solve()?puts("YES"):puts("NO");
85     }
86     return 0;
87 }

时间: 2025-01-16 15:04:05

HDU 3062 Party的相关文章

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 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 3062 Party(2-sat 模板题 tarjan )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3062 Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的.有没有可能会有n 个人同时列席? Input n: 表示有n对夫妻被邀请 (n<= 1000) m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1))

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,

hdu 3062 2-SAT

题目描述:有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的.有没有可能会有n个人同时列席? 思路:2-SAT入门题,直接建模即可. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 20001; 6 const int M = 200000

图论(2-sat):HDU 3062 Party

Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5558    Accepted Submission(s): 1809 Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同

(2sat) hdu 3062

http://wenku.baidu.com/link?url=xo0rr2Euamxkz3WOs7Nq66hZ4vrYfRQ3FWw98Z-fy37O8fOOBLUOnNpFNfS6WtfrAIUGZG2coxcrZhIqrDsFmLP1PboOr3XIGq9QFwn1b67 Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s):