# HDU 4115 Eliminate the Conflict【2011 ACM Asia Chengdu Regional Problem E】

布(P)， 非布(~P)

R->~P, R->~S, P->~R, P->~S, S->~R, S->~P

~R->P, ~P->R

R1<->R2, P1<->P2, S1<->S2 均是双向边

R1->~R2, R2->~R1, ......

  1 #include<cstdio>  2 #include<cstring>  3 #include<algorithm>  4 using namespace std;  5 #define N 60005  6 #define M 999999  7   8 struct Edge{  9     int vtx,next; 10 }E[M]; 11 int head[N],size; 12 int dfn[N],low[N],blg[N],step,scc; 13 int stk[N],top; 14 bool ins[N]; 15  16 void Init(){ 17     memset(head,-1,sizeof(head)); 18     memset(dfn,-1,sizeof(dfn)); 19     memset(ins,false,sizeof(ins)); 20     step=size=0; top=-1; 21 } 22  23 void Insert(int u,int v){ 24     E[size].vtx=v; 25     E[size].next=head[u]; 26     head[u]=size++; 27 } 28  29 void Tarjan(int u){ 30     stk[++top]=u; ins[u]=true; 31     dfn[u]=low[u]=step++; 32     for(int i=head[u];~i;i=E[i].next){ 33         int v=E[i].vtx; 34         if(dfn[v]==-1){ 35             Tarjan(v); 36             low[u]=min(low[u],low[v]); 37         }else if(ins[v]){ 38             low[u]=min(low[u],dfn[v]); 39         } 40     } 41     if(low[u]==dfn[u]){ 42         for(int v=-1;v!=u;top--){ 43             v=stk[top]; 44             ins[v]=false; 45             blg[v]=scc; 46         } 47         scc++; 48     } 49 } 50  51 int main(){ 52     int t,cas=0; 53     scanf("%d",&t); 54     while(t--){ 55         int n,m; 56         scanf("%d%d",&n,&m); 57          58         Init(); 59         for(int i=0;i<n;i++){ 60             Insert(i*6+0,i*6+3); 61             Insert(i*6+0,i*6+5); 62             Insert(i*6+2,i*6+1); 63             Insert(i*6+2,i*6+5); 64             Insert(i*6+4,i*6+1); 65             Insert(i*6+4,i*6+3); 66         } 67          68         for(int i=0;i<n;i++){ 69             int v; 70             scanf("%d",&v); 71             if(v==1){ 72                 Insert(i*6+3,i*6+0); 73                 Insert(i*6+1,i*6+2); 74             }else if(v==2){ 75                 Insert(i*6+5,i*6+2); 76                 Insert(i*6+3,i*6+4); 77             }else if(v==3){ 78                 Insert(i*6+5,i*6+0); 79                 Insert(i*6+1,i*6+4); 80             } 81         } 82         for(int i=0;i<m;i++){ 83             int a,b,v; 84             scanf("%d%d%d",&a,&b,&v); 85             a--; b--; 86             if(v){ 87                 Insert(a*6+0,b*6+1); 88                 Insert(a*6+2,b*6+3); 89                 Insert(a*6+4,b*6+5); 90              91                 Insert(b*6+0,a*6+1); 92                 Insert(b*6+2,a*6+3); 93                 Insert(b*6+4,a*6+5); 94             }else{ 95                 Insert(a*6+0,b*6+0); 96                 Insert(a*6+2,b*6+2); 97                 Insert(a*6+4,b*6+4); 98              99                 Insert(b*6+0,a*6+0);100                 Insert(b*6+2,a*6+2);101                 Insert(b*6+4,a*6+4);102             }103         }104         for(int i=0;i<n*6;i++){105             if(dfn[i]==-1) Tarjan(i);106         }107         bool flag=true;108         for(int i=0;i<n*3;i++){109             if(blg[i<<1]==blg[i<<1|1]){110                 flag=false;111                 break;112             }113         }114         115         printf("Case #%d: ",++cas);116         puts(flag?"yes":"no");117     }118 }

S同样，S->P2建边，不可能存在的情况

posted @ 2011-11-09 10:55 Amb@HDU 阅读(...) 评论(...) 编辑 收藏