QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#150596 | #6380. LaLa and Divination Magic | LeticiaFCS | RE | 1ms | 3580kb | C++20 | 8.7kb | 2023-08-25 21:21:32 | 2023-08-25 21:21:33 |
Judging History
answer
#include"bits/stdc++.h"
using lint = int64_t;
constexpr int MOD = int(1e9) + 7;
constexpr int INF = 0x63636363;
constexpr int NINF = 0xcfcfcfcf;
constexpr lint LINF = 0x6363636363636363;
using namespace std;
struct scc_t {
int n, t, scc_num;
vector<vector<int>> adj;
vector<int> low, id, stk, in_stk, cc_id;
scc_t(const vector<vector<int>>& g) : n(int(g.size())), t(0), scc_num(0),
adj(g), low(n,-1), id(n,-1), in_stk(n, false), cc_id(n) {}
template<class F> void dfs(int cur, F f) {
id[cur] = low[cur] = t++;
stk.push_back(cur);
in_stk[cur] = true;
for (int nxt : adj[cur]) {
if (id[nxt] == -1) {
dfs(nxt, f);
low[cur] = min(low[cur], low[nxt]);
} else if (in_stk[nxt])
low[cur] = min(low[cur], id[nxt]);
}
if (low[cur] == id[cur]) {
vector<int> cc; cc.reserve(stk.size());
while (true) {
int v = stk.back(); stk.pop_back();
in_stk[v] = false;
cc.push_back(v);
cc_id[v] = scc_num;
if (v == cur) break;
} f(cc); scc_num ++;
}
}
template<class F> void solve(F f) {
stk.reserve(n);
for (int r = 0; r < n; ++r)
if (id[r] == -1) dfs(r, f);
}
};
const int M = 2002;
using B = bitset<M>;
B mat[M], matRev[M];
int always[M];
struct TwoSat {
int N;
vector<vector<int>> gr;
vector<int> values; // 0 = false, 1 = true
TwoSat(int n = 0) : N(n), gr(2*n) {}
int add_var() { // (optional)
gr.emplace_back();
gr.emplace_back();
return N++;
}
void either(int f, int j) {
f = max(2*f, -1-2*f);
j = max(2*j, -1-2*j);
gr[f].push_back(j^1);
gr[j].push_back(f^1);
}
void implies(int f, int j) { either(~f, j); }
void set_value(int x) { either(x, x); }
void at_most_one(const vector<int>& li) { // (optional)
if (int(li.size()) <= 1) return;
int cur = ~li[0];
for (int i = 2; i < int(li.size()); ++i) {
int next = add_var();
either(cur, ~li[i]);
either(cur, next);
either(~li[i], next);
cur = ~next;
}
either(cur, ~li[1]);
}
int solve() {
scc_t s(gr);
vector<int> ordem; ordem.reserve(2*N);
s.solve([&ordem](const vector<int> &v){
ordem.insert(ordem.end(), v.begin(), v.end());
} );
values.assign(N, -1);
for (int i = 0; i < N; ++i) if (s.cc_id[2*i] == s.cc_id[2*i+1]) return 0;
for (int i = 0; i < N; ++i){
if (s.cc_id[2*i] < s.cc_id[2*i+1]) values[i] = false;
else values[i] = true;
}
reverse(ordem.begin(), ordem.end());
vector<B> adj(2*N);
vector<B> alcanca(2*N);
for(int o =0; o<2*N; o++){
int u = ordem[o];
for(int v : gr[u]){
adj[u][v] = true;
alcanca[u][v] = true;
}
}
for(int o = 2*N-1; o >= 0; o--){
int u = ordem[o];
for(int v : gr[u])
alcanca[u] |= alcanca[v];
}
vector<bool> settado(2*N);
vector<bool> valor(2*N);
for(int u =0; u<2*N; u++){
int x = 2*u, notx = 2*u+1;
if(alcanca[x][notx]){
settado[x] = true;
settado[notx] = true;
valor[x] = false;
valor[notx] = true;
for(int v = 0; v < 2*N; v++)
if(alcanca[notx][v]){
settado[v] = true;
valor[v] = true;
}
} else if(alcanca[notx][x]){
settado[x] = true;
settado[notx] = true;
valor[x] = true;
valor[notx] = false;
for(int v = 0; v < 2*N; v++)
if(alcanca[x][v]){
settado[v] = true;
valor[v] = true;
}
}
}
int ans = 0;
auto count_sol = [&](int o, auto &&self) -> void {
if(o == 2*N){
ans++;
// cout<<"SOL \n";
// for(int u=0; u<N; u++){
// cout<<u<<" "<<valor[2*u]<<" "<<valor[2*u+1]<<"\n";
// }
return;
}
int u = ordem[o];
if(settado[u]){
self(o+1, self);
} else {
vector<int> alc;
bool ok = true;
if(ok)
for(int v : gr[u]){
if(settado[v] && valor[v] == false){
ok = false;
break;
}
if(!settado[v]){
settado[v] = true;
valor[v] = true;
settado[v^1] = true;
valor[v^1] = false;
alc.push_back(v);
}
}
settado[u] = true;
settado[u^1] = true;
if(ok){
valor[u] = true;
valor[u^1] = false;
self(o+1, self);
}
for(int v : alc){
settado[v] = settado[v^1] = false;
}
valor[u] = false;
valor[u^1] = true;
self(o+1, self);
settado[u] = false;
settado[u^1] = false;
}
};
count_sol(0, count_sol);
return ans;
}
};
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin>>n>>m;
for(int j=0; j<m; j++){
always[j] = -1;
}
for(int i =0; i<n; i++){
string s; cin>>s;
for(int j=0; j<m; j++){
mat[j][i] = int(s[j] - '0');
matRev[j][i] = !mat[j][i];
}
}
TwoSat sat(m);
vector<tuple<int, int, int>> op;
op.reserve(2*m);
for(int u =0; u<m; u++){
if(int c = mat[u].count(); c == n){
sat.either(u, u);
op.emplace_back(u, u, 4);
always[u] = 1;
} else if(c == 0){
sat.either(~u, ~u);
op.emplace_back(u, u, 1);
always[u] = 0;
}
}
for(int u =0; u<m; u++){
for(int v=u+1; v<m; v++){
if((~always[u]) && (~always[v])) continue;
vector<vector<int>> ok(2, vector<int>(2));
int cnt = 0;
if(( matRev[u] & matRev[v]).count() ){
ok[0][0] = true;
cnt++;
}
if(( matRev[u] & mat[v]).count() ){
ok[0][1] = true;
cnt++;
}
if(( mat[u] & matRev[v]).count() ){
ok[1][0] = true;
cnt++;
}
if(( mat[u] & mat[v]).count() ){
ok[1][1] = true;
cnt++;
}
if(cnt == 4) continue;
if(!ok[0][0]){
sat.implies(~u, v);
sat.implies(~v, u);
op.emplace_back(u, v, 4);
}
if(!ok[0][1]){
sat.implies(~u, ~v);
sat.implies(v, u);
op.emplace_back(u, v, 2);
}
if(!ok[1][0]){
sat.implies(u, v);
sat.implies(~v, ~u);
op.emplace_back(u, v, 3);
}
if(!ok[1][1]){
sat.implies(u, ~v);
sat.implies(v, ~u);
op.emplace_back(u, v, 1);
}
// cout<<"u "<<u<<" v "<<v<<"\n";
// for(int i=0;i<2; i++){
// for(int j=0;j<2; j++){
// if(ok[i][j])
// cout<<" "<<i<<" "<<j<<"\n";
// }
// }
}
}
if(int count_sol = sat.solve(); count_sol == n){
cout<<op.size()<<'\n';
for(const auto &[u,v,t] : op)
cout<<u<<' '<<v<<' '<<t<<'\n';
} else {
cout<<"-1\n";
}
return 0;
}
/*
1-either(~x, ~y): x -> ~y, y -> ~x
2-either(x, ~y): ~x -> ~y, y -> x
3-either(~x, y): x -> y, ~y -> ~x
4-either(x, y): ~x -> y, ~y -> x
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3456kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #2:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
3 3 101 011 111
output:
6 2 2 4 0 1 4 0 2 4 0 2 3 1 2 4 1 2 3
result:
ok Kout = 6, Kans = 6
Test #3:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #4:
score: 0
Accepted
time: 0ms
memory: 3504kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #5:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #6:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #7:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #8:
score: 0
Accepted
time: 1ms
memory: 3444kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #9:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
1 1 1
output:
1 0 0 4
result:
ok Kout = 1, Kans = 1
Test #10:
score: 0
Accepted
time: 0ms
memory: 3416kb
input:
1 1 0
output:
1 0 0 1
result:
ok Kout = 1, Kans = 1
Test #11:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #12:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #13:
score: 0
Accepted
time: 1ms
memory: 3456kb
input:
2 4 0111 0010
output:
12 0 0 1 2 2 4 0 1 3 0 1 1 0 3 3 0 3 1 1 2 4 1 2 3 1 3 2 1 3 3 2 3 4 2 3 2
result:
ok Kout = 12, Kans = 15
Test #14:
score: 0
Accepted
time: 0ms
memory: 3508kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #15:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
4 2 10 11 01 00
output:
0
result:
ok Kout = 0, Kans = 0
Test #16:
score: 0
Accepted
time: 1ms
memory: 3492kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #17:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
2 4 0010 1000
output:
12 1 1 1 3 3 1 0 1 2 0 1 1 0 2 4 0 2 1 0 3 2 0 3 1 1 2 3 1 2 1 2 3 2 2 3 1
result:
ok Kout = 12, Kans = 15
Test #18:
score: 0
Accepted
time: 1ms
memory: 3564kb
input:
2 5 11101 00000
output:
21 3 3 1 0 1 2 0 1 3 0 2 2 0 2 3 0 3 2 0 3 1 0 4 2 0 4 3 1 2 2 1 2 3 1 3 2 1 3 1 1 4 2 1 4 3 2 3 2 2 3 1 2 4 2 2 4 3 3 4 3 3 4 1
result:
ok Kout = 21, Kans = 21
Test #19:
score: 0
Accepted
time: 1ms
memory: 3444kb
input:
5 4 0010 1001 0011 0101 1011
output:
-1
result:
ok Kout = -1, Kans = -1
Test #20:
score: 0
Accepted
time: 1ms
memory: 3500kb
input:
3 2 01 00 10
output:
1 0 1 1
result:
ok Kout = 1, Kans = 1
Test #21:
score: 0
Accepted
time: 1ms
memory: 3424kb
input:
3 2 10 11 00
output:
1 0 1 2
result:
ok Kout = 1, Kans = 1
Test #22:
score: 0
Accepted
time: 0ms
memory: 3440kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #23:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
3 27 111010110011101010011110110 010001110100000110100101101 000011111000000010011111001
output:
-1
result:
ok Kout = -1, Kans = -1
Test #24:
score: 0
Accepted
time: 1ms
memory: 3456kb
input:
3 7 1000100 0001100 0101111
output:
36 2 2 1 4 4 4 0 1 1 0 2 2 0 2 1 0 3 4 0 3 1 0 4 4 0 4 3 0 5 1 0 6 1 1 2 2 1 2 1 1 3 3 1 4 4 1 4 3 1 5 2 1 5 3 1 6 2 1 6 3 2 3 3 2 3 1 2 5 3 2 5 1 2 6 3 2 6 1 3 4 4 3 4 3 3 5 2 3 6 2 4 5 4 4 5 2 4 6 4 4 6 2 5 6 2 5 6 3
result:
ok Kout = 36, Kans = 39
Test #25:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
1 19 1010110011001101000
output:
19 0 0 4 1 1 1 2 2 4 3 3 1 4 4 4 5 5 4 6 6 1 7 7 1 8 8 4 9 9 4 10 10 1 11 11 1 12 12 4 13 13 4 14 14 1 15 15 4 16 16 1 17 17 1 18 18 1
result:
ok Kout = 19, Kans = 532
Test #26:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
5 32 10101101001001001101111100100110 00110110010111010101011000011010 01010101110100000110001000010100 11010011000110101101110001011111 00111001110011110000000010000111
output:
-1
result:
ok Kout = -1, Kans = -1
Test #27:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
3 12 110010101000 110001011000 110101011001
output:
100 0 0 4 1 1 4 2 2 1 8 8 4 9 9 1 10 10 1 0 3 4 0 3 2 0 4 4 0 4 2 0 5 4 0 5 2 0 6 4 0 6 2 0 7 4 0 7 2 0 11 4 0 11 2 1 3 4 1 3 2 1 4 4 1 4 2 1 5 4 1 5 2 1 6 4 1 6 2 1 7 4 1 7 2 1 11 4 1 11 2 2 3 3 2 3 1 2 4 3 2 4 1 2 5 3 2 5 1 2 6 3 2 6 1 2 7 3 2 7 1 2 11 3 2 11 1 3 4 1 3 5 3 3 6 1 3 7 3 3 8 4 3 8 3 ...
result:
ok Kout = 100, Kans = 145
Test #28:
score: 0
Accepted
time: 1ms
memory: 3556kb
input:
3 25 1110100100011100101100111 0100000001011101101010101 0111110111111001001110111
output:
-1
result:
ok Kout = -1, Kans = -1
Test #29:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
1 5 10110
output:
5 0 0 4 1 1 1 2 2 4 3 3 4 4 4 1
result:
ok Kout = 5, Kans = 35
Test #30:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
5 17 01011100010100110 01001101111011001 00100111001101010 10101000001010110 00101011010010001
output:
-1
result:
ok Kout = -1, Kans = -1
Test #31:
score: 0
Accepted
time: 1ms
memory: 3504kb
input:
3 30 010100010011100011010001010100 011111100101001100010101010010 011000010111111111000101101110
output:
-1
result:
ok Kout = -1, Kans = -1
Test #32:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
5 30 110010101001001100010110010000 011011111000011001101000100000 110101010111000000100100111000 001111011110101101101001101011 101100001101011110101010110000
output:
-1
result:
ok Kout = -1, Kans = -1
Test #33:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
10 10 0110101111 1100100000 1000101100 1000010101 1001011101 1011101101 1011111011 0101010000 0111011010 1111010110
output:
-1
result:
ok Kout = -1, Kans = -1
Test #34:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
9 10 1000001101 0110010110 1011101111 1010001110 1110001000 1001110110 1101010010 0001011111 1000010100
output:
-1
result:
ok Kout = -1, Kans = -1
Test #35:
score: 0
Accepted
time: 1ms
memory: 3420kb
input:
3 5 11111 01000 01100
output:
18 1 1 4 0 1 4 0 1 3 0 2 3 0 3 2 0 3 3 0 4 2 0 4 3 1 2 4 1 2 2 1 3 4 1 3 2 1 4 4 1 4 2 2 3 2 2 4 2 3 4 2 3 4 3
result:
ok Kout = 18, Kans = 18
Test #36:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
7 9 000010100 101001100 110010111 000000110 100010101 101000100 101101100
output:
-1
result:
ok Kout = -1, Kans = -1
Test #37:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
1 4 0000
output:
4 0 0 1 1 1 1 2 2 1 3 3 1
result:
ok Kout = 4, Kans = 22
Test #38:
score: 0
Accepted
time: 1ms
memory: 3508kb
input:
9 8 10011110 10111101 11001010 01000101 10110011 00101001 00101100 11010110 01000000
output:
-1
result:
ok Kout = -1, Kans = -1
Test #39:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
3 10 0000000111 1011011111 0101111010
output:
-1
result:
ok Kout = -1, Kans = -1
Test #40:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #41:
score: 0
Accepted
time: 1ms
memory: 3552kb
input:
1 5 00110
output:
5 0 0 1 1 1 1 2 2 4 3 3 4 4 4 1
result:
ok Kout = 5, Kans = 35
Test #42:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
6 9 100101111 100001110 100101010 001101000 101100010 010101110
output:
-1
result:
ok Kout = -1, Kans = -1
Test #43:
score: -100
Runtime Error
input:
6 836 001111110001001001101010101010011100010100100100111110110100101000100100000000011101110001011100111111111001101111111101101110010011000100100111111101011010101101011101010000100011100011000011111011011110000001010101001101110100001111111001000110111000010110001100110010010000101011001010101100...