Algorithm review--2-sat (bzoj2199)

Source: Internet
Author: User

Tag:script   false   line   main    contains     ring   div   tar   lib   

Title: Description Due to farmer John's leadership, the cows withdrew from the farm and formed a dairy council. In the principle of "every cow can get what he wants", the Parliament established the following voting system: M-Only cows (1 <= m <= 4000) will vote for n motions (1 <= N <= 1,000). Each cow will throw "yes" or "no" to exactly two motions b_i and c_i (1 <= b_i <= N; 1 <= c_i <= N) (enter ' Y ' and ' n ' in the file). Their voting results were vb_i (vb_i in {' Y ', ' n '}) and Vc_i (vc_i in {' Y ', ' n '}). Finally, the motion will be decided in the following way: at least one of the two votes cast by each cow matches the final result. For example, Bessie to the motion 1 voted in favor of ' Y ', to the motion 2 voted against ' n ', then in any legal bill through the scheme, must satisfy the motion 1 must be ' Y ' or Bill 2 must be ' N ' (or at the same time satisfy). Give each cow a vote, your job is to determine which motions can be passed and which cannot. If such a scenario does not exist, output "impossible". If there is at least one solution, the output: Y if in each solution, the motion must pass N if in each solution, the motion must be dismissed? If there are solutions to this motion that can be passed, some solutions in which this motion will be dismissed consider the following collection of votes:---------------------1 2 3 cows 1 yes No cows 2 no no cows 3 yes Yes Cows 4 Yes Yes below are two available Energy Solution: * Motion 1 passed (satisfying cow 1,3,4) * Motion 2 dismissed (meet cow 2) * Motion 3 can also be dismissed (this is why there are two solutions) in fact, the above problem has only two solutions. So, the answer to the output is as follows: YN? input* line 1th: Two spaces separated by integers: N and M * 2nd to M+1 lines: Section i+1 describes the voting scheme for the first cow: b_i, vb_i, c_i, vc_ioutput* 1th line: A string with n characters, the first character is either ' Y ' (The first motion must pass), or ' N ' (the first motion must be dismissed), or '? '. If there is no solution, output "impossible". Sample Input
3 4
1 Y 2 N
1 N 2 N
1 y 3 y
1 y 2 y


Sample OutputYN?
HINT Source

Gold

Exercises

2-sat problem Template topic, first of all, the basic solution of 2-SAT problem:

Some problems can be turned into Boolean equations to solve the

Our aim is to break each word of its Boolean equation into two points, one point to its own, one point to its non, such as a to a and ┐a, and to convert the various operational symbols into a form that only contains ^ (and) and (A->b, A is true, B is true), such as ∨ into ┐a. b ^┐b A, a must be true to convert to ┐a->a form, and then convert it to an edge, on both sides connected to the corresponding point.

If a and ┐a in the same strong connected component of the last built diagram ... Then the Boolean equation has a solution

If the topological order of the strongly connected component of a is after the topological sequence of the strongly connected component of the ┐a, then A is true, before a is false, and if the equivalence is true or false, it can be taken. In this case, the topological sequence can be directly used Tarjian, and the topological order of the strongly connected components must be larger.

The above is the basic knowledge

But this problem is a little special Because a topological order containing a is equal to the ┐a. With Tarjian words a little trouble ...

But n is very small ... Direct DFS can If a can reach ┐a, then the topological sequence of a possibly with ┐a in the same strong connected component or a strong connected component of a is less than or equal to ┐a.

Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<ctime>#include<cctype>#include<cstring>#include<string>#include<algorithm>using namespacestd;Const intn=2005;Const intm=10005;intfirst[n],next[m],go[m],tot=1, n,m;BOOLVisit[n];inlinevoidComb (intAintb) {next[++tot]=first[a],first[a]=tot,go[tot]=b;} InlineintTraninta) {  return(a%2==1)? A +1: A-1;} InlineintR () {CharC; intf=0;  for(C=getchar ();c<'0'|| C>'9'; c=GetChar ());  for(; c<='9'&&c>='0'; c=GetChar ()) F= (f<<3) + (f<<1) +c-'0'; returnF;} InlinevoidDfsintu) {Visit[u]=true;  for(intE=first[u];e;e=Next[e]) {      if(!Visit[go[e]]) DFS (go[e]); }}inlineBOOLJudintu) {memset (visit,false,sizeof(visit));  DFS (U); if(!visit[tran (U)])return true; Else return false;}intMain () {//freopen ("a.in", "R", stdin);N=r (), m=R (); Chars[5],t[5]; intb;  for(intI=1; i<=m;i++) {scanf ("%d%s%d%s",&a,s,&b,t); intt1,t2; if(s[0]=='Y') T1=a*2-1; ElseT1=a*2; if(t[0]=='Y') T2=b*2-1; ElseT2=b*2;    Comb (Tran (T2), T1);  Comb (Tran (T1), T2); }   for(intI=1; i<=n;i++)  {    BOOLFlag1=jud (i*2-1); BOOLFlag2=jud (i*2); if(!FLAG1&AMP;&AMP;!FLAG2) {cout<<"Impossible"<<endl;return 0;} Else if(!FLAG1) cout<<"N"; Else if(!FLAG2) cout<<"Y"; Elsecout<<"?"; }  return 0;}

Algorithm review--2-sat (bzoj2199)

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.