QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#142845 | #6380. LaLa and Divination Magic | UFRJ# | WA | 1ms | 3596kb | C++17 | 3.8kb | 2023-08-20 00:24:47 | 2023-08-20 00:24:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using lint = int64_t;
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(size(g)), 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);
}
};
struct TwoSat{
int N;
vector<vector<int>> gr;
vector<int> values;
TwoSat(int n = 0) : N(n), gr(2*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); }
bool solve(){
scc_t s(gr);
s.solve([](const vector<int> &v){ return; });
values.assign(N, -1);
for(int i = 0; i<N; i++) if(s.cc_id[2*i] == s.cc_id[2*i+1]) return false;
return true;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin>>n>>m;
vector<string> g(n);
for(int i =0; i<n; i++) cin>>g[i];
vector<int> always(m, -1);
for(int j =0; j<m; j++){
int um = 0;
for(int i =0; i<n; i++){
um += (g[i][j] == '1');
}
if(um == n) always[j] = 1;
else if(um == 0) always[j] = 0;
}
using T = tuple<int, int, int>;
vector<T> edges;
edges.reserve(2*m*m);
for(int a =0 ; a<m; a++){
if(always[a] != -1){
if(always[a] == 1)
edges.emplace_back(a,a,4);
else
edges.emplace_back(a,a,1);
continue;
}
for(int b = a+1; b<m; b++){
if(always[b] != -1) continue;
vector<vector<bool>> ok(2, vector<bool>(2));
for(int i=0; i<n; i++){
int x = g[i][a] - '0';
int y = g[i][b] - '0';
ok[x][y] = true;
}
if(ok[0][0] && ok[0][1] && ok[1][0] && ok[1][1])
continue;
if(ok[0][0] && ok[1][1]){
edges.emplace_back(a, b, 2);
edges.emplace_back(a, b, 3);
}
if(ok[0][1] && ok[1][0]){
edges.emplace_back(a, b, 1);
edges.emplace_back(a, b, 4);
}
}
}
TwoSat sat(m);
for(auto [i, j, t] : edges){
if(t == 1){
sat.implies(i, ~j);
sat.implies(j, ~i);
} else if(t == 2){
sat.implies(~i, ~j);
sat.implies(j, i);
} else if(t == 3){
sat.implies(i, j);
sat.implies(~j, ~i);
} else if(t == 4){
sat.implies(~i, j);
sat.implies(~j, i);
}
}
if(!sat.solve()){
cout<<"-1\n";
return 0;
}
cout<<edges.size()<<"\n";
for(auto [a, b, t] : edges){
cout<<a<<" "<<b<<" "<<t<<"\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
input:
2 1 1 0
output:
0
result:
ok Kout = 0, Kans = 0
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3516kb
input:
3 3 101 011 111
output:
3 0 1 1 0 1 4 2 2 4
result:
wrong answer The output contains less solutions than intended.