QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#704663#7900. Gifts from KnowledgehamsterWA 1ms3640kbC++232.3kb2024-11-02 20:34:162024-11-02 20:34:17

Judging History

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

  • [2024-11-02 20:34:17]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3640kb
  • [2024-11-02 20:34:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;

vector<vector<int>> adj, adj_rev;
vector<int> ord, cmp;
vector<bool> vis;
int r, c;

void dfs1(int v) {
    vis[v] = true;
    for (int u : adj[v]) {
        if (!vis[u]) dfs1(u);
    }
    ord.push_back(v);
}

void dfs2(int v, int cl) {
    cmp[v] = cl;
    for (int u : adj_rev[v]) {
        if (cmp[u] == -1) dfs2(u, cl);
    }
}

bool solve_2SAT(int vars) {
    ord.clear();
    vis.assign(vars * 2, false);
    for (int i = 0; i < vars * 2; ++i) {
        if (!vis[i]) dfs1(i);
    }
    cmp.assign(vars * 2, -1);
    for (int i = 0, j = 0; i < vars * 2; ++i) {
        int v = ord[vars * 2 - i - 1];
        if (cmp[v] == -1) dfs2(v, j++);
    }
    for (int i = 0; i < vars; ++i) {
        if (cmp[i * 2] == cmp[i * 2 + 1]) return false;
    }
    return true;
}

void add_clause(int x, bool xv, int y, bool yv) {
    adj[x * 2 + !xv].push_back(y * 2 + yv);
    adj[y * 2 + !yv].push_back(x * 2 + xv);
    adj_rev[y * 2 + yv].push_back(x * 2 + !xv);
    adj_rev[x * 2 + xv].push_back(y * 2 + !yv);
}

void Solve() {
    cin >> r >> c;
    vector<string> mat(r);
    for (int i = 0; i < r; ++i) {
        cin >> mat[i];
    }
    adj.assign(r * 2, vector<int>());
    adj_rev.assign(r * 2, vector<int>());
    for (int j = 0; j < (c + 1) / 2; ++j) {
        vector<int> ones;
        for (int i = 0; i < r; ++i) {
            if (mat[i][j] == '1') ones.push_back(i);
            if (j != c - j - 1 && mat[i][c - j - 1] == '1') ones.push_back(i);
        }
        if (ones.size() > 2) {
            cout << 0 << "\n";
            return;
        }
        if (ones.size() == 2) {
            int u = ones[0], v = ones[1];
            if (j == c - j - 1) {
                add_clause(u, false, v, true);
                add_clause(u, true, v, false);
            } else {
                add_clause(u, false, v, false);
                add_clause(u, true, v, true);
            }
        }
    }
    if (solve_2SAT(r)) {
        int ways = 1;
        for (int i = 0; i < r; ++i) {
            if (cmp[i * 2] < cmp[i * 2 + 1]) ways = (ways * 2) % MOD;
        }
        cout << ways << "\n";
    } else {
        cout << 0 << "\n";
    }
}

signed main() {
    int T;
    cin >> T;
    while (T--) Solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3640kb

input:

3
3 5
01100
10001
00010
2 1
1
1
2 3
001
001

output:

0
1
2

result:

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