QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#150607 | #6380. LaLa and Divination Magic | LeticiaFCS | TL | 3541ms | 45828kb | C++14 | 8.9kb | 2023-08-25 21:37:52 | 2023-08-25 21:37:54 |
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 = 4002;
using B = bitset<M>;
B mat[M], matRev[M], adj[M], alcanca[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());
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];
alcanca[u][u] = false;
}
vector<vector<int>> adjAlc(2*N);
for(int u = 0; u < 2*N; u++){
for(int v=0; v<2*N; v++){
if(alcanca[u][v])
adjAlc[u].push_back(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] = settado[notx] = true;
valor[x] = false;
valor[notx] = true;
for(int v = 0; v < 2*N; v++)
if(alcanca[notx][v]){
settado[v] = settado[v^1] = true;
valor[v] = true;
valor[v^1] = false;
}
} else if(alcanca[notx][x]){
settado[x] = settado[notx] = true;
valor[x] = true;
valor[notx] = false;
for(int v = 0; v < 2*N; v++)
if(alcanca[x][v]){
settado[v] = settado[v^1] = true;
valor[v] = true;
valor[v^1] = false;
}
}
}
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 : adjAlc[u]){
if(settado[v] && valor[v] == false){
ok = false;
break;
}
if(!settado[v]){
settado[v] = settado[v^1] = true;
valor[v] = true;
valor[v^1] = false;
alc.push_back(v);
}
}
settado[u] = 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] = 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: 0ms
memory: 3540kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #2:
score: 0
Accepted
time: 0ms
memory: 5500kb
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: 3504kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 3424kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #5:
score: 0
Accepted
time: 1ms
memory: 3544kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #6:
score: 0
Accepted
time: 1ms
memory: 3452kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #7:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #8:
score: 0
Accepted
time: 1ms
memory: 3456kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #9:
score: 0
Accepted
time: 1ms
memory: 3516kb
input:
1 1 1
output:
1 0 0 4
result:
ok Kout = 1, Kans = 1
Test #10:
score: 0
Accepted
time: 1ms
memory: 5592kb
input:
1 1 0
output:
1 0 0 1
result:
ok Kout = 1, Kans = 1
Test #11:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #12:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #13:
score: 0
Accepted
time: 0ms
memory: 5568kb
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: 1ms
memory: 5468kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #15:
score: 0
Accepted
time: 0ms
memory: 3472kb
input:
4 2 10 11 01 00
output:
0
result:
ok Kout = 0, Kans = 0
Test #16:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #17:
score: 0
Accepted
time: 0ms
memory: 5512kb
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: 5596kb
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: 0ms
memory: 5572kb
input:
5 4 0010 1001 0011 0101 1011
output:
-1
result:
ok Kout = -1, Kans = -1
Test #20:
score: 0
Accepted
time: 1ms
memory: 5592kb
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: 5552kb
input:
3 2 10 11 00
output:
1 0 1 2
result:
ok Kout = 1, Kans = 1
Test #22:
score: 0
Accepted
time: 1ms
memory: 3508kb
input:
2 1 0 1
output:
0
result:
ok Kout = 0, Kans = 0
Test #23:
score: 0
Accepted
time: 2ms
memory: 5576kb
input:
3 27 111010110011101010011110110 010001110100000110100101101 000011111000000010011111001
output:
-1
result:
ok Kout = -1, Kans = -1
Test #24:
score: 0
Accepted
time: 1ms
memory: 5464kb
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: 5520kb
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: 0ms
memory: 5620kb
input:
5 32 10101101001001001101111100100110 00110110010111010101011000011010 01010101110100000110001000010100 11010011000110101101110001011111 00111001110011110000000010000111
output:
-1
result:
ok Kout = -1, Kans = -1
Test #27:
score: 0
Accepted
time: 1ms
memory: 5604kb
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: 2ms
memory: 5564kb
input:
3 25 1110100100011100101100111 0100000001011101101010101 0111110111111001001110111
output:
-1
result:
ok Kout = -1, Kans = -1
Test #29:
score: 0
Accepted
time: 0ms
memory: 5616kb
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: 2ms
memory: 5592kb
input:
5 17 01011100010100110 01001101111011001 00100111001101010 10101000001010110 00101011010010001
output:
-1
result:
ok Kout = -1, Kans = -1
Test #31:
score: 0
Accepted
time: 2ms
memory: 5720kb
input:
3 30 010100010011100011010001010100 011111100101001100010101010010 011000010111111111000101101110
output:
-1
result:
ok Kout = -1, Kans = -1
Test #32:
score: 0
Accepted
time: 1ms
memory: 5592kb
input:
5 30 110010101001001100010110010000 011011111000011001101000100000 110101010111000000100100111000 001111011110101101101001101011 101100001101011110101010110000
output:
-1
result:
ok Kout = -1, Kans = -1
Test #33:
score: 0
Accepted
time: 0ms
memory: 5504kb
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: 5528kb
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: 0ms
memory: 5516kb
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: 5640kb
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: 5504kb
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: 5488kb
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: 1ms
memory: 5508kb
input:
3 10 0000000111 1011011111 0101111010
output:
-1
result:
ok Kout = -1, Kans = -1
Test #40:
score: 0
Accepted
time: 1ms
memory: 3432kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #41:
score: 0
Accepted
time: 1ms
memory: 5572kb
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: 1ms
memory: 5532kb
input:
6 9 100101111 100001110 100101010 001101000 101100010 010101110
output:
-1
result:
ok Kout = -1, Kans = -1
Test #43:
score: 0
Accepted
time: 263ms
memory: 22716kb
input:
6 836 001111110001001001101010101010011100010100100100111110110100101000100100000000011101110001011100111111111001101111111101101110010011000100100111111101011010101101011101010000100011100011000011111011011110000001010101001101110100001111111001000110111000010110001100110010010000101011001010101100...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #44:
score: 0
Accepted
time: 17ms
memory: 9204kb
input:
1 1680 00110010011001010101000101001110100010100110000110011101101101011011011011011011000000100100001111110111011001000010100101111110011000011110001000000001001010010110001011101000000110011010000001101010010000101111000010110001001010000010001010110000110111011011001011010011100111110000100110110...
output:
1680 0 0 1 1 1 1 2 2 4 3 3 4 4 4 1 5 5 1 6 6 4 7 7 1 8 8 1 9 9 4 10 10 4 11 11 1 12 12 1 13 13 4 14 14 1 15 15 4 16 16 1 17 17 4 18 18 1 19 19 4 20 20 1 21 21 1 22 22 1 23 23 4 24 24 1 25 25 4 26 26 1 27 27 1 28 28 4 29 29 4 30 30 4 31 31 1 32 32 4 33 33 1 34 34 1 35 35 1 36 36 4 37 37 1 38 38 4 39 ...
result:
ok Kout = 1680, Kans = 4232760
Test #45:
score: 0
Accepted
time: 108ms
memory: 14652kb
input:
5 525 010011011010110111000101111001010011110110100011110111000110010010000011011011110001110100110101101111111001100010010011011011011101110010011011001111110100010011011001010111011001100011001000101100111000000100010100011011011110101010000011101110001001000000100101000000101011101010110101010110...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #46:
score: 0
Accepted
time: 634ms
memory: 25724kb
input:
9 1369 10111110110000010001000001110000001000010000101111010111111000100001001011101000101011000111001000010110010100011001110101100010000010000010100010011100110011000011110001001001010100010100001111111000111110100100010000100100110111110101100011010100000011000011010111111101011001011001010010110...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #47:
score: 0
Accepted
time: 809ms
memory: 45828kb
input:
7 1509 10100001100101000101100011100001010001111101001010100101000010000100010000100110001011000011111000111100011100000110100100011010011111011100111010101011110111011011100100101110011000110111100101101011010101100011101011110001011101001011010100000011001001110100111101101001100110101111011010011...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #48:
score: 0
Accepted
time: 3ms
memory: 6372kb
input:
1 354 101000111100100010011000000111001111100100100101111010100100101010111001011110011010111111010010101111101101101010100011010000011011010101000011010000001101110011011000101101111011111001011010111100100001100000110000110011101011010011110011001100101100011101100010101001100101110011001011001011...
output:
354 0 0 4 1 1 1 2 2 4 3 3 1 4 4 1 5 5 1 6 6 4 7 7 4 8 8 4 9 9 4 10 10 1 11 11 1 12 12 4 13 13 1 14 14 1 15 15 1 16 16 4 17 17 1 18 18 1 19 19 4 20 20 4 21 21 1 22 22 1 23 23 1 24 24 1 25 25 1 26 26 1 27 27 4 28 28 4 29 29 4 30 30 1 31 31 1 32 32 4 33 33 4 34 34 4 35 35 4 36 36 4 37 37 1 38 38 1 39 3...
result:
ok Kout = 354, Kans = 187797
Test #49:
score: 0
Accepted
time: 370ms
memory: 33356kb
input:
5 1006 00111100011111100001101011101101000101000011011000111111110010111011001101010000100011001000000111001011010000001110010000101000010111100101100101101000110000011011101010111110101110100110000011001000010000011111000001011111101010110011100110010011010000000000100000111101000001000100101110100...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #50:
score: 0
Accepted
time: 1173ms
memory: 38000kb
input:
9 1851 11000110100010011010000110010000010111101010100110011010100101110011100000101101001010000010001011111110100001101000001101111011011011011110010110000111001101110011001011111001110001011101100110111111100101100101000110010001011001100101010111100000101111010110010000110111010100011010111101110...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #51:
score: 0
Accepted
time: 3541ms
memory: 14916kb
input:
7 695 001100011101000101000011101100000101000000010110110101110110100101010100100111101110100100110101110100111000011000000101111101010100010010101011100101111001100001101111000111000111010110101101001111110111110001010011011111110111111010101010100100111010101010100111110011001100101100110010111101...
output:
-1
result:
ok Kout = -1, Kans = -1
Test #52:
score: -100
Time Limit Exceeded
input:
8 231 010111111111010000001100101001011111011010100101010100111010100110111011111111101110001100001001101110001001000000001110010010010110011001011110100110110110101100101110101100101011000001101111101001110000101110110010000100111101010 00010011100001101010000101100000110001010100100010110011011111...