满汉全席[2-SAT]
对不起我又写了一个板题qvq
和洛谷那道模板题没区别。。。两样菜至少做一样即可
不过注意define和函数的区别!!!
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <complex>
#include <ctime>
#include <vector>
#define mp(x, y) make_pair(x, y)
#define node(x, y) ((x << 1) - y)
using namespace std;
const int N = 2e4 + 5;
struct Edge{
int v, next;
}edge[N];
int head[N], esize;
inline void addedge(int x, int y){
//printf("%d %d\n", x, y);
edge[++esize] = (Edge){y, head[x]}; head[x] = esize;
}
int n, m;
int dfn[N], low[N], tim;
int col[N], colsize, stk[N], top;
bool vis[N];
void tarjan(int x){
stk[++top] = x; vis[x] = 1;
dfn[x] = low[x] = ++tim;
for(int i = head[x], vv; ~i; i = edge[i].next){
vv = edge[i].v;
if(!dfn[vv]) tarjan(vv), low[x] = min(low[x], low[vv]);
else if(vis[vv]) low[x] = min(low[x], dfn[vv]);
}
if(low[x] == dfn[x]){
++colsize;
do{
col[stk[top]] = colsize; vis[stk[top]] = 0;
}while(top && stk[top--] != x);
}
}
inline void clear(){
memset(head, -1, sizeof(head));
memset(col, 0, sizeof(col));
memset(vis, 0, sizeof(vis));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
esize = tim = top = colsize = 0;//!!!
}
int main(){
int T; scanf("%d", &T);
while(T--){
clear();
scanf("%d %d\n", &n, &m);
char c1, c2; int x1, x2;
for(int i = 1; i <= m; ++i){
do{c1 = getchar();}while(c1 != 'm' && c1 != 'h');
scanf("%d", &x1);
do{c2 = getchar();}while(c2 != 'm' && c2 != 'h');
scanf("%d", &x2);
int f1 = (c1 == 'm'), f2 = (c2 == 'm');
addedge(node(x1, (f1 ^ 1)), node(x2, f2));
addedge(node(x2, (f2 ^ 1)), node(x1, f1));
//注意define和函数的区别!
//printf("%d %d %d %d %d %d %d\n", x1, f1 ^ 1, x1 << 1, f1 ^ 1, x2, f2, node(x2, f2));
}
for(int i = 1; i <= (n << 1); ++i){
if(!dfn[i]) tarjan(i);
}
bool flag = 1;
for(int i = 1; i <= n; ++i){
// printf("%d %d\n", col[node(i, 0)], col[node(i, 1)]);
if(col[node(i, 0)] == col[node(i, 1)]){
flag = 0; break;
}
}
if(flag) printf("GOOD\n"); else printf("BAD\n");
}
return 0;
}