BZOJ1823: [JSOI2010]满汉全席

2-SAT…

 1 /**************************************************************
 2     Problem: 1823
 3     User: zhuohan123
 4     Language: C++
 5     Result: Accepted
 6     Time:4 ms
 7     Memory:1300 kb
 8 ****************************************************************/
 9  
10 #include <iostream>
11 #include <cstdio>
12 #include <cstring>
13 #include <algorithm>
14 using namespace std;
15 inline int imin(int a,int b){return a<b?a:b;}
16 struct food{int h,m;}f[110];
17 struct point{int head,wk,dfn,low,instack;}p[210];int pnum;
18 struct edge{int to,next;}g[2100];int gnum;
19 void addedge(int from,int to)
20 {
21     g[++gnum].to=to;g[gnum].next=p[from].head;p[from].head=gnum;
22 }
23 int blocknum,dfsnum;
24 int s[210],sr;
25 void tarjan(int po)
26 {
27     p[po].dfn=p[po].low=++dfsnum;
28     s[++sr]=po;p[po].instack=true;
29     for(int i=p[po].head;i;i=g[i].next)
30     {
31         if(!p[g[i].to].dfn)
32         {
33             tarjan(g[i].to);
34             p[po].low=imin(p[po].low,p[g[i].to].low);
35         }
36         else if(p[g[i].to].instack)
37             p[po].low=imin(p[po].low,p[g[i].to].dfn);
38     }
39     if(p[po].dfn==p[po].low)
40     {
41         blocknum++;
42         while(s[sr]!=po)
43         {
44             p[s[sr]].wk=blocknum;
45             p[s[sr]].instack=false;
46             sr--;
47         }
48         p[s[sr]].wk=blocknum;
49         p[s[sr]].instack=false;
50         sr--;
51     }
52 }
53 int main(int argc, char *argv[])
54 {
55     int T;scanf("%d",&T);
56     while(T--)
57     {
58         pnum=gnum=blocknum=dfsnum=0;
59         memset(p,0,sizeof p);
60         int n,m;scanf("%d%d",&n,&m);
61         for(int i=1;i<=n;i++)f[i].h=++pnum,f[i].m=++pnum;
62         while(m--)
63         {
64             char str[60]={0};while(!str[0])gets(str);
65             int a=0,b=0,ha,hb,now=0;
66             while(str[now]!='h'&&str[now]!='m')now++;
67             ha=(str[now]=='h');
68             while(str[now]>'9'||str[now]<'0')now++;
69             while(str[now]>='0'&&str[now]<='9')a=a*10+str[now]-'0',now++;
70             while(str[now]!='h'&&str[now]!='m')now++;
71             hb=(str[now]=='h');
72             while(str[now]>'9'||str[now]<'0')now++;
73             while(str[now]>='0'&&str[now]<='9')b=b*10+str[now]-'0',now++;
74             if(ha&&hb)addedge(f[a].m,f[b].h),addedge(f[b].m,f[a].h);
75             if(!ha&&hb)addedge(f[a].h,f[b].h),addedge(f[b].m,f[a].m);
76             if(ha&&!hb)addedge(f[a].m,f[b].m),addedge(f[b].h,f[a].h);
77             if(!ha&&!hb)addedge(f[a].h,f[b].m),addedge(f[b].h,f[a].m);
78         }
79         for(int i=1;i<=pnum;i++)
80             if(!p[i].dfn)tarjan(i);
81         bool isgood=true;;
82         for(int i=1;i<=n;i++)
83             if(p[f[i].h].wk==p[f[i].m].wk)
84             isgood=false;
85         if(isgood)puts("GOOD");
86         else puts("BAD");
87     }
88     return 0;
89 }

 

posted @ 2013-08-27 21:08  zhuohan123  阅读(375)  评论(0编辑  收藏  举报