满汉全席[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;   
}


转载于:https://www.cnblogs.com/hjmmm/p/10673087.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值