首页 > 代码库 > HihoCoder #1467 : 2-SAT·hihoCoder音乐节

HihoCoder #1467 : 2-SAT·hihoCoder音乐节

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

int m,n,T;
int map[201][201];
int rever(int i){
    if (i % 2 == 0) {
        return i - 1;
    }
    else
        return i + 1;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    scanf("%d",&T);
    int time;
    for (time=1;time<=T;time++){
        scanf("%d %d",&n,&m);
        int i,j,k;
        memset(map,0,sizeof(map));
        for(i=1;i<=m;i++){
            int a,b;
            char requirement[2];
            scanf("%s",requirement);
            int num;
            char temp;
            sscanf(requirement,"%c%d",&temp,&num);
            if (requirement[0] == m){
                a = 2*num;
            }
            else {
                a = 2*num-1;
            }
            scanf("%s",requirement);
            sscanf(requirement,"%c%d",&temp,&num);
            if (requirement[0] == m){
                b = 2*num;
            }
            else {
                b = 2*num - 1;
            }
            map[rever(a)][b] = 1;
            map[rever(b)][a] = 1;
        }
        for (k=1;k<=2*n;k++){
            for (i=1;i<=2*n;i++){
                for (j=1;j<=2*n;j++){
                    if (map[i][j]) continue;
                    if (map[i][k] && map[k][j])
                        map[i][j] = 1;
                }
            }
        }
        int ok = 1;
        for (i=1;i<=n;i++){
            if (map[2*i][2*i-1] && map[2*i-1][2*i]){
                ok = 0;
                break;
            }
        }
        if (ok) printf("GOOD\n");
        else printf("BAD\n");
    }
    
    return 0;
}

2SAT问题模板

又犯蠢啦啊啊啊啊啊

想当然的直接读取字符串以第二位作为a和b了,找了半天错误,欸

--正文

因为n很小直接使用floyd来判断连通性

 

HihoCoder #1467 : 2-SAT·hihoCoder音乐节