문제

Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.

Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

입력

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

출력

The output for every scenario is a line containing “Scenario #i:”, where i is the number of the scenario starting at 1, followed by one line saying either “No suspicious bugs found!” if the experiment is consistent with his assumption about the bugs’ sexual behavior, or ”Suspicious bugs found!” if Professor Hopper’s assumption is definitely wrong.

예제 입력

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

예제 출력

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

문제 풀이

전형적인 2-SAT 문제이다.
서로 XOR 관계로 같은 값이 될 수 없도록 명제를 추가하면 된다.
명제를 추가하여 그래프를 구성하고 scc(강한 연결 요소) 를 찾아서 풀었다.

답안 코드

// false->true, true->false
// true->true 이 상황은 발생하면 안됨
// 명제 4개로 정의하여 둘다 거짓, 둘다 참일수는 없음

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

#define MAX_N   2000

int T;
int n, m;

vector<int> adj[MAX_N*2+1];
int dfs_num[MAX_N*2+1];
int dfs_low[MAX_N*2+1];
int dfs_count;
bool scc_visited[MAX_N*2+1];
int stack[MAX_N*2], s_top;

void scc(int u){
    dfs_num[u] = dfs_low[u] = ++dfs_count;
    stack[s_top++] = u;
    for(int v : adj[u]){
        if(dfs_num[v] == 0)
            scc(v);
        if(!scc_visited[v])
            dfs_low[u] = min(dfs_low[u], dfs_low[v]);
    }
    if(dfs_num[u] == dfs_low[u]){
        while(1){
            int v = stack[--s_top];
            scc_visited[v] = true;
            if(u == v)  break;
        }
    }
}

int main(){
    scanf("%d\n", &T);
    for(int t = 1; t <= T; ++t){
        scanf("%d %d\n", &n, &m);
        for(int i = 0,a,b; i < m; ++i){
            scanf("%d %d\n", &a, &b);
            adj[a+n].push_back(b);
            adj[b+n].push_back(a);
            adj[a].push_back(b+n);
            adj[b].push_back(a+n);
        }
        
        for(int i = 1; i <= n*2; ++i)
            if(dfs_num[i] == 0)
                scc(i);
        
        bool vaild = true;
        for(int i = 1; i <= n; ++i)
            if(dfs_low[i] == dfs_low[i+n]){
                vaild = false;
                break;
            }
        
        printf("Scenario #%d:\n%s\n\n", t, vaild?"No suspicious bugs found!":"Suspicious bugs found!");
        
        memset(dfs_num, 0, sizeof(dfs_num));
        memset(dfs_low, 0, sizeof(dfs_low));
        memset(scc_visited, false, sizeof(scc_visited));
        dfs_count = 0;
        for(int i = 1; i <= n*2; ++i)
            adj[i].clear();
    }
    return 0;
}

문제 위치 – https://www.acmicpc.net/problem/7535