• 满汉全席[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
    ", 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, &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
    ", 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
    ", col[node(i, 0)], col[node(i, 1)]);
                if(col[node(i, 0)] == col[node(i, 1)]){
                    flag = 0; break;
                } 
            }
            if(flag) printf("GOOD
    "); else printf("BAD
    ");
        } 
        return 0;   
    }
    
    
  • 相关阅读:
    Python3.5 Day2作业:购物车程序
    Python3.5 Day1作业:实现用户密码登录,输错三次锁定。
    Python3.5 day3作业二:修改haproxy配置文件。
    Python3.5 day3作业一:实现简单的shell sed替换功能
    Python3.5 day4作业:对员工信息文件,实现增删改查操作。
    栈的数组实现
    栈的链式实现
    20101217
    traits
    DES加密算法中的IP置换算法
  • 原文地址:https://www.cnblogs.com/hjmmm/p/10673087.html
Copyright © 2020-2023  润新知