QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#342542#4084. 카드 뒤집기 게임duongnc0000 28ms22328kbC++205.2kb2024-03-01 12:29:592024-03-01 12:29:59

Judging History

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

  • [2024-03-01 12:29:59]
  • 评测
  • 测评结果:0
  • 用时:28ms
  • 内存:22328kb
  • [2024-03-01 12:29:59]
  • 提交

answer

/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

struct two_sat_solver{
private:
    int n;
    vector<vector<int>> adj;
    vector<int> val, comp, z;
public:
    vector<int> value;
    two_sat_solver(int n = 0): n(n), adj(n << 1){ }
    int add_variable(){
        adj.emplace_back();
        adj.emplace_back();
        return n ++;
    }
    void either(int u, int v){
        u = max(2 * u, -1 - 2 * u);
        v = max(2 * v, -1 - 2 * v);
        adj[u].push_back(v ^ 1);
        adj[v].push_back(u ^ 1);
    }
    void implies(int u, int v){
        either(~u, v);
    }
    void equals(int u, int v){
        either(~u, v), either(u, ~v);
    }
    void differs(int u, int v){
        either(u, v), either(~u, ~v);
    }
    void set_value(int u, bool x = true){
        x ? either(u, u) : either(~u, ~u);
    }
    void at_most_one(const vector<int> &arr){
        if((int)arr.size() <= 1) return;
        int cur = ~arr[0];
        for(auto u = 2; u < (int)arr.size(); ++ u){
            int next = add_variable();
            either(cur, ~arr[u]), either(cur, next), either(~arr[u], next);
            cur = ~next;
        }
        either(cur, ~arr[1]);
    }
    int time, comp_cnt;
    int dfs(int u){
        int low = val[u] = ++ time, v;
        z.push_back(u);
        for(auto v: adj[u]) if(!~comp[v]) low = min(low, val[v] ?: dfs(v));
        ++ time;
        if(low == val[u]){
            do{
                v = z.back();
                z.pop_back();
                comp[v] = comp_cnt;
                if(value[v >> 1] == -1) value[v >> 1] = v & 1;
            }while(v != u);
            comp_cnt ++;
        }
        return val[u] = low;
    }
    // O(n)
    bool solve(){
        value.assign(n, -1);
        val.assign(2 * n, 0);
        comp.assign(2 * n, -1);
        time = comp_cnt = 0;
        for(auto u = 0; u < n << 1; ++ u) if(!~comp[u]) dfs(u);
        for(auto u = 0; u < n; ++ u) if(comp[u << 1] == comp[u << 1 ^ 1]) return false;
        return true;
    }
    // Enumerate solutions while act_while() returns true.
    // O(n^2 + (m + n * (# of solutions found))*S/w)
    template<size_t S>
    bool enumerate_solutions(auto act_while){
        assert(2 * n <= S);
        if(!solve()) return false;
        fill(value.begin(), value.end(), -1);
        bitset<S> has_value;
        vector<vector<int>> has(comp_cnt);
        vector<bitset<S>> reachable(n << 1);
        for(auto u = 0; u < n << 1; ++ u) has[comp[u]].push_back(u);
        vector<int> vis(comp_cnt, -1);
        for(auto t = 0; t < comp_cnt; ++ t){
            int u = has[t][0];
            vis[t] = t;
            for(auto v: has[t]){
                reachable[u].set(v);
                for(auto w: adj[v]) if(vis[comp[w]] != t){
                    vis[comp[w]] = t;
                    reachable[u] |= reachable[w];
                }
            }
            for(auto v: has[t]) reachable[v] = reachable[u];
        }
        for(auto u = 0; u < n << 1; ++ u){
            if(!reachable[u][u ^ 1]) continue;
            has_value[u] = has_value[u ^ 1] = true;
            value[u >> 1] = ~u & 1;
            for(auto v = 0; v < n << 1; ++ v) if(reachable[u ^ 1][v]){
                has_value[v] = has_value[v ^ 1] = true;
                value[v >> 1] = v & 1;
            }
        }
        vector<bitset<S>> delta(comp_cnt);
        auto dfs = [&](auto self, int t)->bool{
            if(!~t) return act_while(value);
            int u = has[t][0];
            if(~value[u >> 1]) return self(self, t - 1);
            for(auto v: has[t]){
                has_value[v] = has_value[v ^ 1] = true;
                value[v >> 1] = ~v & 1;
            }
            if(!self(self, t - 1)) return false;
            for(auto v: has[t]) value[v >> 1] = v & 1;
            delta[t] = reachable[u] & ~has_value;
            for(auto v = delta[t]._Find_first(); v != S; v = delta[t]._Find_next(v)){
                has_value[v] = has_value[v ^ 1] = true;
                value[v >> 1] = v & 1;
            }
            if(!self(self, t - 1)) return false;
            for(auto v: has[t]){
                has_value[v] = has_value[v ^ 1] = false;
                value[v >> 1] = -1;
            }
            for(auto v = delta[t]._Find_first(); v != S; v = delta[t]._Find_next(v)){
                has_value[v] = has_value[v ^ 1] = false;
                value[v >> 1] = -1;
            }
            return true;
        };
        dfs(dfs, comp_cnt - 1);
        return true;
    }
};

bool reversal(int n, int m, vector<string> a) {
    two_sat_solver tss(2 * m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (a[i][j] == 'O') tss.differs(i % m, j % m + m);
            else tss.equals(i % m, j % m + m);
        }
    }
    return tss.solve();
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 0ms
memory: 3632kb

input:

10 1
OOOOOOOOOX
XOXXOOOOOO
XOOXOOOXOO
OOOOOOOOOO
OOOXOOOOOO
OOXOOXXOOO
OOOOXOOOOO
XOOOOOOOXO
OOOXXOOOXO
OOOOOOOOOO

output:

0

result:

ok single line: '0'

Test #2:

score: -11
Wrong Answer
time: 0ms
memory: 3568kb

input:

10 1
OOOOOOOOXX
OOOOOOOOXX
XXXXXXXXOO
XXXXXXXXOO
OOOOOOOOXX
OOOOOOOOXX
OOOOOOOOXX
OOOOOOOOXX
OOOOOOOOXX
OOOOOOOOXX

output:

0

result:

wrong answer 1st lines differ - expected: '1', found: '0'

Subtask #2:

score: 0
Wrong Answer

Test #46:

score: 50
Accepted
time: 23ms
memory: 21976kb

input:

1000 1
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOOOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOOOXOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOXOOOOOOXOOOOOOOOOOOOOOOOXOOOOOOXOOOOOOOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOOOOOOOXOOOOO...

output:

0

result:

ok single line: '0'

Test #47:

score: -50
Wrong Answer
time: 28ms
memory: 22132kb

input:

1000 1
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOOOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOOOXOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOXOOOOOOXOOOOOOOOOOOOOOOOXOOOOOOXOOOOOOOOOOOOOXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXOOOOOOOOOOOOOOOXOOOOOO...

output:

0

result:

wrong answer 1st lines differ - expected: '1', found: '0'

Subtask #3:

score: 0
Wrong Answer

Test #66:

score: 39
Accepted
time: 19ms
memory: 22328kb

input:

1000 1
XXOOXXOXXXOOXXOXOXXXXXXXOOXXOXXXXXXXXOXXOXXXXOXXXXOOOOXXOXOOXOXXXXXOXXXXXOOXXXOOOXOXOXXXXXXXXXXXOXOXXOOXXXOOXXXXOOXXOOOXXOXXOXOXOXXXXXOXXXXOXXXOXXOXXXOXXXXXOXXXXXXOOXOXOXOXOXOXOXXXXXXXOXXXXOXXXXXXXXXOOOOOOXXXXOOOOXXXOXXOOXOXXXXXXXXXOXXOOXXXXOXXXXXXXXXOXXXOOXXOOXOOOXXXXOXOXXOXXXXOOXOXXOOXXXXXO...

output:

0

result:

ok single line: '0'

Test #67:

score: 0
Accepted
time: 28ms
memory: 21976kb

input:

1000 1
XXXXXXXXXOOXXXXOXXXXXXXXXXXOXXXXXOOOXXXOXOXXXXXXXXOXXXXOOXOXXOXXXXOOOXXXXXXXXXXOOXOXXXOOXOXXXXXXXXXXOOXXXXXXOXXOOXXXXXOOXXXXXXXXXOXXXXXXXXXXXXOXXXXXXOXXOXXOXXOOXXXXOOXOXOXXXXXXOOXOXOXXXXXOXXOXOXOXOXXOXOXOXXXXXXXOXXOXXXXXXXXXXOXXXXOOXXOXXOXXXXXXXXOXOOXXXOXOOXXXOXXXXXXXXOXXXXXXOXXXXXXOXXXXXOXXO...

output:

0

result:

ok single line: '0'

Test #68:

score: -39
Wrong Answer
time: 27ms
memory: 21920kb

input:

1000 1
OOOOOOOOXXOOOOXOOOOOOOOOOOXOOOOOXXXOOOXOXOOOOOOOOXOOOOXXOXOOXOOOOXXXOOOOOOOOOOXXOXOOOXXOXOOOOOOOOOOXXOOOOOOXOOXXOOOOOXXOOOOOOOOOXOOOOOOOOOOOOXOOOOOOXOOXOOXOOXXOOOOXXOXOXOOOOOOXXOXOXOOOOOXOOXOXOXOXOOXOXOXOOOOOOOXOOXOOOOOOOOOOXOOOOXXOOXOOXOOOOOOOOXOXXOOOXOXXOOOXOOOOOOOOXOOOOOOXOOOOOOXOOOOOXOOXO...

output:

0

result:

wrong answer 1st lines differ - expected: '1', found: '0'