跳到正文

题解

题解:P9798 [NERC 2018] Harder Satisfiability

sjchsk1094约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年6月14日
收录
2025年12月12日
更新
2026年9月12日
篇幅
约 7 分钟
获赞
1
题目
P9798

版本

  1. v1当前

2-SAT 好题。

由题意不难建出图,考虑找出原式值为 00 的所有情况。

Observation 1 一个点在其反点所在的 SCC 中时原式必然为 00,证明显然。

Observation 2 如果一个 ∃\exists 点 uu 能到达一个 ∀\forall 点 vv,且 u<vu < v,则原式为 00。

Observation 3 如果一个 ∀\forall 点能到达另一个 ∀\forall 点,则原式必然为 00。

不难发现不满足这些条件时必然可以通过先将含有全称变量的 SCC 标记为 any,再依次确定剩余 SCC 的方法构造出一个解。

分别判断每一个条件是否成立即可。

代码如下:

C++
#include<bits/stdc++.h>
#define pos(i) (i)
#define neg(i) (i + n)
#define rev(i) ((i <= n) ? i + n : i - n)
#define id(i) ((i > n) ? i - n : i)
#define put(x) return printf("%s\n", x ? "TRUE" : "FALSE"), void()
using namespace std;
const int N = 3e5 + 3;
int n, m, ind[N];
bool forall[N], vis_ex[N];
bool ok = 1;
vector<int> s[N], s2[N];

char get(){
    char x;cin >> x;
    while(!isalpha(x))cin >> x;
    return x;
}

namespace tarjan{
    int dfn[N], low[N], scc[N], pdfn, pscc;
    int stk[N], pstk;
    int cnt[N], tmp[N];
    bool used[N];
    void init(){
        for(int i = 1; i <= n * 2; i++){
            dfn[i] = low[i] = 0;
            stk[i] = scc[i] = 0, used[i] = 0;
            cnt[i] = 0, tmp[i] = 0;
        }
        pdfn = pscc = pstk = 0;
    }
    void dfs(int u){
        dfn[u] = low[u] = ++pdfn, stk[++pstk] = u, used[u] = 1;
        for(int v: s[u]){
            if(!dfn[v])
                dfs(v), low[u] = min(low[u], low[v]);
            else if(used[v])
                low[u] = min(low[u], low[v]);
        }
        if(dfn[u] == low[u]){
            pscc++;
            while(used[u]){
                int x = stk[pstk--];
                used[x] = 0, scc[x] = pscc;
                if(forall[id(x)])cnt[pscc]++;
            }
            if(cnt[pscc] > 1)ok = 0;
        }
    }
}using namespace tarjan;

queue<int> q;
void topo(){
    for(int i = 1; i <= pscc; i++)
        if(ind[i] == 0)q.push(i);
    while(!q.empty()){
        int u = q.front();q.pop();
        cnt[u] += tmp[u];
        for(int v: s2[u]){
            if(cnt[u] && cnt[v])ok = 0;
            tmp[v] += cnt[u], ind[v]--;
            if(!ind[v])q.push(v);
        }
    }
}

void solve(){
    ok = 1;
    for(int i = 0; i <= n * 2; i++)
        forall[i] = 0, s[i].clear(), s2[i].clear(), vis_ex[i] = 0, ind[i] = 0;
    init();
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        char x = get();
        if(x == 'A')forall[i] = 1;
    }
    for(int i = 1; i <= m; i++){
        int u, v;cin >> u >> v;
        if(u < 0)u = neg(-u);
        if(v < 0)v = neg(-v);
        s[rev(u)].emplace_back(v);
        s[rev(v)].emplace_back(u);
    }
    for(int i = 1; i <= n * 2; i++)
        if(!dfn[i])dfs(i);
    if(!ok)put(0);
    for(int i = 1; i <= n * 2; i++)
        for(int v: s[i])
            if(scc[i] != scc[v])s2[scc[i]].emplace_back(scc[v]);
    for(int i = 1; i <= n * 2; i++){
        sort(s2[i].begin(), s2[i].end());
        s2[i].erase(unique(s2[i].begin(), s2[i].end()), s2[i].end());
    }
    for(int i = 1; i <= n * 2; i++)
        for(int v: s2[i])ind[v]++;
    for(int i = 1; i <= n; i++){
        if(scc[i] == scc[i + n])put(0);
    }
    for(int i = 1; i <= n; i++){
        if(!forall[i])
            vis_ex[scc[i]] = 1,
            vis_ex[scc[i + n]] = 1;
        else if(vis_ex[scc[i]] || vis_ex[scc[i + n]])
            put(0);
    }
    topo();
    if(!ok)put(0);
    put(1);
}

main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int T;cin >> T;
    while(T--)solve();
}
原文