HDU 3062 party

Source: Internet
Author: User
Partytime limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 3062
64-bit integer Io format: % i64d Java class name: Main n couples are invited to a party. Due to venue issues, only one of each couple can attend. Among the 2N individuals, some people have great conflicts (of course, there is no conflict between husband and wife), and the two who have conflicts will not appear at the same time. Is there a possibility that N people will attend at the same time? Inputn: N couples are invited (n <= 1000)
M: represents a conflict between m pairs (M <(n-1) * (N-1 ))

In the next m row, there are four numbers in each row, namely A1, A2, C1, and C2.
A1 and A2 indicate the number of the husband and wife respectively.
C1, C2 indicates the wife or husband, 0 indicates the wife, and 1 indicates the husband.
Husband and wife numbers from 0 to n-1
If output exists, the output is yes.
Otherwise, no
Sample Input
2 10 1 1 1 
Sample output
YES
Source2009 multi-university training contest 16-host by nit solution: 2-sat. Template question. Each couple is numbered 2 * I and 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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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 }
View code

 

HDU 3062 party

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.