QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#582519#997. 2-SAT 问题starryskymeowWA 37ms11908kbC++202.8kb2024-09-22 16:44:272024-09-22 16:44:27

Judging History

你现在查看的是最新测评结果

  • [2024-09-22 16:44:27]
  • 评测
  • 测评结果:WA
  • 用时:37ms
  • 内存:11908kb
  • [2024-09-22 16:44:27]
  • 提交

answer

#include <bits/stdc++.h>

#ifdef LOCAL
#include "debug.h"
#else
#define deb(...) 114514
#endif

using namespace std;

using LL = long long;
using pii = pair<int, int>;
using pll = pair<LL, LL>;
const int INF = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int N = 3e5 + 10;

struct TWO_SAT {
    int siz;
    std::vector<std::vector<int>> gra;
    TWO_SAT(int size) : siz(size), gra(siz * 2 + 1) {}
    // i -> x_i == 0, i + n -> x_i == 1
    void add(int a, bool ta, int b, bool tb) {
        gra[a + !ta * siz].push_back(b + tb * siz);
        gra[b + !tb * siz].push_back(a + ta * siz);
    }

    std::vector<int> solve() {
        std::vector<int> low(siz * 2 + 1), scc(siz * 2 + 1);
        std::stack<int> sta;
        int idx = 1, cnt = 0;
        auto dfs = [&](int u, auto self) -> void {
            low[u] = idx++;
            int now = low[u];
            sta.push(u);
            for (auto v : gra[u]) {
                if (!low[v])
                    self(v, self), low[u] = min(low[u], low[v]);
                else if (!scc[v])
                    low[u] = min(low[u], low[v]);
            }
            if (low[sta.top()] <= now) {
                cnt++;
                while (sta.size() && low[sta.top()] >= now) {
                    auto t = sta.top();
                    sta.pop();
                    scc[t] = cnt;
                }
            }
        };
        for (int i = 1; i <= 2 * siz; i++) {
            if (!low[i])
                dfs(i, dfs);
        }
        std::vector<int> ans(siz + 1);
        for (int i = 1; i <= siz; i++) {
            if (scc[i] == scc[i + siz]) {
                return std::vector<int>();
            }
            ans[i] = scc[i] > scc[i + siz];
        }
        return ans;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    TWO_SAT ts(n);
    for (int i = 0; i < m; i++) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        ts.add(a, b, c, d);
    }
    auto res = ts.solve();
    if (res.empty()) {
        cout << "NO" << '\n';
    } else {
        cout << "YES" << '\n';
        for (int i = 1; i <= n; i++) {
            cout << res[i] << ' ';
        }
        cout << '\n';
    }
}

signed main() {
#ifdef LOCAL
    clock_t tttttttt = clock();
    freopen("in.txt", "r", stdin);
#endif
#ifndef LOCAL
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#endif
    //*****************************************************************
    int t = 1;
    // cin >> t;
    while (t--) solve();
//*****************************************************************
#ifdef LOCAL
    cerr << "Time Used: " << fixed << setprecision(3) << (clock() - tttttttt) / (CLOCKS_PER_SEC / 1000.0) << " ms" << endl;
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 37ms
memory: 11908kb

input:

86354 86418
14615 0 14903 1
13605 0 4458 0
15604 0 77112 0
52311 1 64996 0
22711 1 74245 1
39042 1 57372 1
2994 1 84183 1
80574 0 58791 1
27780 1 9336 1
61809 0 7216 0
71113 0 42287 1
20073 0 72448 0
73840 0 77048 0
28955 0 4165 0
16322 1 14075 1
43512 0 58600 1
45219 0 53858 0
14919 0 22576 0
16594...

output:

YES
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

wrong answer You didn't find a solution but jury did.