萌新 2-SAT 40 分求助

P4171 [JSOI2010] 满汉全席

shinzanmono @ 2023-01-16 12:37:17

#include<iostream>
#include<algorithm>
#include<stack>
const int sz = 210;
struct edge {
    int nxt, to;
} graph[sz * 10];
int hpp, head[sz];
void addEdge(int from, int to) {
    graph[++hpp] = edge{head[from], to};
    head[from] = hpp;
}
int dfn[sz], low[sz], dpp, cnt[sz], belong[sz], num;
bool vis[sz];
std::stack<int> s;
void tarjan(int u) {
    dfn[u] = low[u] = ++dpp;
    s.push(u), vis[u] = true;
    for (int p = head[u]; p; p = graph[p].nxt) {
        int v = graph[p].to;
        if (!dfn[v]) tarjan(v), low[u] = std::min(low[u], low[v]);
        else if (vis[v]) low[u] = std::min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u]) {
        num++;
        while (!s.empty()) {
            int v = s.top();
            s.pop();
            cnt[num]++, belong[v] = num, vis[v] = false;
            if (u == v) break;
        }
    }
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--) {
        int n, m; 
        std::cin >> n >> m;
        std::fill(vis + 1, vis + n + 1, 0);
        std::fill(head + 1, head + n + 1, 0), hpp = 0;
        std::fill(dfn + 1, dfn + n + 1, 0), dpp = 0;
        std::fill(low + 1, low + n + 1, 0);
        std::fill(cnt + 1, cnt + n + 1, 0), num = 0;
        std::fill(belong + 1, belong + n + 1, 0);
        for (int i = 1; i <= m; i++) {
            char su, sv;
            int u, v;
            std::cin >> su >> u >> sv >> v;
            int a = su == 'h', b = sv == 'h';
            if (a == 1 && b == 0) std::swap(a, b), std::swap(u, v);
            if (a == 0 && b == 1) addEdge(u, v), addEdge(v + n, u + n);
            else if (a == 1 && b == 1) addEdge(u + n, v), addEdge(v + n, u);
            else addEdge(u, v + n), addEdge(v, u + n);
        }
        for (int i = 1; i <= n << 1; i++) 
            if (!dfn[i]) dpp = 0, tarjan(i);
        bool flag = true;
        for (int i = 1; i <= n; i++) {
            if (belong[i] == belong[i + n]) {
                flag = false;
                break;
            }
        }    
        if (!flag) std::cout << "BAD\n";
        else std::cout << "GOOD\n";
    }
    return 0;
} 

by pengbonan @ 2024-12-02 18:14:17

是不是要清 2 * n 的范围啊


|