【题解】Head Maker HDU 6809 2020杭电多校4 图论 随机化

代码

#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,pii> Edge;
const int N = 6;
int _w;

int n, m, a[N][N];
vector<Edge> edge;

namespace DSU {
int pa[N*N];

void init() {
for( int i = 0; i < N*N; ++i )
pa[i] = i;
}
int find( int u ) {
return u == pa[u] ? u : pa[u] = find( pa[u] );
}
bool link( int u, int v ) {
u = find(u), v = find(v);
if( u == v ) return false;
return pa[u] = v, true;
}
}

int face[6];

void rotate( int x, int y, int nx, int ny ) {
int tmp[6];
if( nx == x+1 ) {
tmp[0] = face[4];
tmp[5] = face[2];
tmp[1] = face[1];
tmp[3] = face[3];
tmp[4] = face[5];
tmp[2] = face[0];
} else if( nx == x-1 ) {
tmp[0] = face[2];
tmp[5] = face[4];
tmp[1] = face[1];
tmp[3] = face[3];
tmp[4] = face[0];
tmp[2] = face[5];
} else if( ny == y+1 ) {
tmp[0] = face[3];
tmp[5] = face[1];
tmp[1] = face[0];
tmp[3] = face[5];
tmp[4] = face[4];
tmp[2] = face[2];
} else if( ny == y-1 ) {
tmp[0] = face[1];
tmp[5] = face[3];
tmp[1] = face[5];
tmp[3] = face[0];
tmp[4] = face[4];
tmp[2] = face[2];
} else {
assert(0);
}
for( int i = 0; i < 6; ++i )
face[i] = tmp[i];
}

void dfs( int x, int y, int fax, int fay ) {
face[0] = 1;
for( pii nxt : adj[x][y] ) {
int nx = nxt.first;
int ny = nxt.second;
if( nx == fax && ny == fay ) continue;
rotate(x, y, nx, ny);
dfs(nx, ny, x, y);
}
if( fax != -1 ) rotate(x, y, fax, fay);
}

bool chk( int sx, int sy ) {
memset(face, 0, sizeof face);
dfs(sx, sy, -1, -1);
for( int i = 0; i < 6; ++i )
if( face[i] == 0 )
return false;
return true;
}

bool check() {
DSU::init();
for( int i = 0; i < n; ++i )
for( int j = 0; j < m; ++j )
for( Edge e : edge ) {
pii u = e.first;
pii v = e.second;
int uid = u.first * N + u.second;
int vid = v.first * N + v.second;
}
}
for( int i = 0; i < n; ++i )
for( int j = 0; j < m; ++j )
if( a[i][j] )
return chk(i, j);
return false;
}

int main() {
int T;
_w = scanf( "%d", &T );
while( T-- ) {
_w = scanf( "%d%d", &n, &m );
for( int i = 0; i < n; ++i )
for( int j = 0; j < m; ++j ) {
char ch;
_w = scanf( " %c", &ch );
a[i][j] = ch - '0';
}
edge.clear();
for( int i = 0; i < n; ++i )
for( int j = 0; j < m; ++j ) {
if( i != n-1 && a[i][j] && a[i+1][j] ) {
edge.push_back( Edge(pii(i, j), pii(i+1, j)) );
}
if( j != m-1 && a[i][j] && a[i][j+1] ) {
edge.push_back( Edge(pii(i, j), pii(i, j+1)) );
}
}
bool yes = false;
for( int times = 0; times < 10000; ++times ) {
random_shuffle(edge.begin(), edge.end());
if( check() ) {
yes = true;
break;
}
}
puts( yes ? "Yes" : "No" );
}
return 0;
}

posted @ 2020-10-21 23:27  mlystdcall  阅读(159)  评论(0编辑  收藏  举报