HDU4115. Eliminate the Conflict (scissors stone cloth)--2-sat feasibility judgment

Source: Internet
Author: User

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

Title Description:
Bob, and Alice two in the N-round scissors-stone cloth, known Bob N-wheel out of the case, for Alice given the M-group limit, as long as each round Alice did not lose, after the N-round even if Alice wins. Judging if she can win.

Analysis:
For the first round of Bob's case, Alice has two options (equivalent to the I variable has two states), and then we just deal with contradictory edges

//62ms 2156K 3312 B#include <cstring>#include <cstdio>#include <queue>#include <vector>#include <iostream>Const intmaxn=20010;Const intmaxm=40010;using namespace STD;intLow[maxn],dfn[maxn],sccno[maxn],scc,dfs_clock,top;intSTACK[MAXN];BOOLINSTACK[MAXN];structEdge {intTo,next;} edge[maxm<<1];intHead[maxn],tot;voidAddedge (intUintV) {edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;}voidInit () {tot=0;memset(Head,0xFF,sizeof(head));}voidDfsintu) {intV    Low[u]=dfn[u]=++dfs_clock; instack[u]=1; Stack[top++]=u; for(intI=head[u]; i!=-1; I=edge[i].next) {v=edge[i].to;if(!dfn[v]) {DFS (v);if(Low[u]>low[v]) low[u]=low[v]; }Else if(Instack[v]&&low[u]>dfn[v]) low[u]=dfn[v]; }if(Low[u]==dfn[u]) {scc++; for(;;)            {V=stack[--top]; instack[v]=0; SCCNO[V]=SCC;if(V==u) Break; }    }}BOOLSolveable (intN) {memset(DFN,0,sizeof(DFN));memset(Instack,false,sizeof(Instack)); Dfs_clock=top=scc=0; for(intI=0; i<n; ++i) {if(!dfn[i]) DFS (i); } for(intI=0; i<n; i+=2) {if(sccno[i]==sccno[i^1])return false; }return true;}intMain () {#ifndef Online_judgeFreopen ("In.txt","R", stdin);#endif //Online_judge     intt,n,m,u,v,c,cas=1;inta[maxn],b[maxn][2];scanf("%d", &t); while(t--) {scanf("%d%d", &n,&m); for(intI=0; i<n;++i) {scanf("%d", &a[i]);if(a[i]==1) {b[i][0]=1; b[i][1]=2; }if(a[i]==2) {b[i][0]=2; b[i][1]=3; }if(a[i]==3) {b[i][0]=3; b[i][1]=1; }} init (); while(m--) {scanf("%d%d%d", &u,&v,&c); U--, v--;if(c==0){if(b[u][0]!=b[v][0]) {Addedge (u<<1,v<<1|1); Addedge (v<<1,u<<1|1); }if(b[u][0]!=b[v][1]) {Addedge (u<<1,v<<1); Addedge (v<<1|1,u<<1|1); }if(b[u][1]!=b[v][0]) {Addedge (u<<1|1,v<<1|1); Addedge (v<<1,u<<1); }if(b[u][1]!=b[v][1]) {Addedge (u<<1|1,v<<1); Addedge (v<<1|1,u<<1); }            }Else{if(b[u][0]==b[v][0]) {Addedge (u<<1,v<<1|1); Addedge (v<<1,u<<1|1); }if(b[u][0]==b[v][1]) {Addedge (u<<1,v<<1); Addedge (v<<1|1,u<<1|1); }if(b[u][1]==b[v][0]) {Addedge (u<<1|1,v<<1|1); Addedge (v<<1,u<<1); }if(b[u][1]==b[v][1]) {Addedge (u<<1|1,v<<1); Addedge (v<<1|1,u<<1); }            }        }printf("Case #%d:", cas++);if(Solveable (n<<1))puts("Yes");Else puts("No"); }return 0;}

HDU4115. Eliminate the Conflict (scissors stone cloth)--2-sat feasibility judgment

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.