poj 3740 Easy Finding 精确匹配

时间:2021-08-19 04:24:27

题目链接

dlx的第一题, 真是坎坷.....

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
const int maxNode = ;
struct DLX {
int L[maxNode], R[maxNode], U[maxNode], D[maxNode], row[maxNode], col[maxNode];
int S[maxn], H[maxn], deep, ans[maxn], sz, n, m;
void remove(int c) {
L[R[c]] = L[c] ;
R[L[c]] = R[c] ;
for(int i = D[c]; i!=c; i = D[i])
for(int j = R[i]; j!=i; j = R[j]) {
U[D[j]] = U[j] ;
D[U[j]] = D[j] ;
S[col[j]]--;
}
}
void resume(int c) {
for(int i = U[c]; i!=c; i = U[i])
for(int j = L[i]; j!=i; j = L[j]) {
S[col[j]]++;
D[U[j]] = j;
U[D[j]] = j;
}
R[L[c]] = c;
L[R[c]] = c;
}
int dfs(int d) {
if(R[] == ) {
deep = d;
return ;
}
int c = R[];
for(int i = R[]; i!=; i = R[i])
if(S[c]>S[i])
c = i;
remove(c);
for(int i = D[c]; i!=c; i = D[i]) {
ans[d] = row[i];
for(int j = R[i]; j!=i; j = R[j])
remove(col[j]);
if(dfs(d+))
return ;
for(int j = L[i]; j!=i; j = L[j])
resume(col[j]);
}
resume(c);
return ;
}
void add(int r, int c) {
sz++;
row[sz] = r;
col[sz] = c;
S[c]++;
U[sz] = U[c];
D[sz] = c;
D[U[c]] = sz;
U[c] = sz;
if(~H[r]) {
R[sz] = H[r];
L[sz] = L[H[r]];
L[R[sz]] = sz;
R[L[sz]] = sz;
} else {
H[r] = L[sz] = R[sz] = sz;
}
}
void init(){
mem1(H);
for(int i = ; i<=n; i++) {
R[i] = i+;
L[i] = i-;
U[i] = i;
D[i] = i;
}
R[n] = ;
L[] = n;
sz = n;
}
void solve () {
init() ;
for(int i = ; i<=m; i++) {
for(int j = ; j<=n; j++) {
int x;
scanf("%d", &x);
if(x) {
add(i, j);
}
}
}
if(dfs()) {
printf("Yes, I found it\n");
} else {
printf("It is impossible\n");
}
}
}dlx;
int main()
{
while(~scanf("%d%d", &dlx.m, &dlx.n)) {
dlx.solve();
}
return ;
}