Artifact Content
Not logged in

Artifact 21a69859a51bc35b865745c99e8fca648a8c7d37


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int size(int a)
		{ return sz[Find(a)]; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

class ShadowSculpture { public:
	string possible(vector <string> XY, vector <string> YZ, vector <string> ZX)
	{
		const int N = XY.size();

		// Init
		bool XYZ[10][10][10];
		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y)
		for(int z=0; z<N; ++z)
			XYZ[x][y][z] = true;

		// Grave
		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y)
		for(int z=0; z<N; ++z)
			if(XY[x][y] == 'N' || YZ[y][z] == 'N' || ZX[z][x] == 'N')
				XYZ[x][y][z] = false;

		// Sanity check
		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y) {
			bool a=false,b=false,c=false;
			for(int z=0; z<N; ++z) {
				a |= XYZ[x][y][z];
				b |= XYZ[z][x][y];
				c |= XYZ[y][z][x];
			}
			if(XY[x][y]=='Y'&&!a) return "Impossible";
			if(YZ[x][y]=='Y'&&!b) return "Impossible";
			if(ZX[x][y]=='Y'&&!c) return "Impossible";
		}

		// Judge
		return is_connected(XYZ, N) ? "Possible" : "Impossible";
	}

	bool is_connected(bool XYZ[10][10][10], int N)
	{
		UnionFind uf(N*N*N);
		function<int(int,int,int)> id = [&](int x, int y, int z) {
			return (x*N+y)*N+z;
		};

		const int dx[] = {-1,+1,0,0,0,0};
		const int dy[] = {0,0,-1,+1,0,0};
		const int dz[] = {0,0,0,0,-1,+1};
		int cnt = 0;
		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y)
		for(int z=0; z<N; ++z) if(XYZ[x][y][z]) {
			++cnt;
			for(int d=0; d<6; ++d) {
				int xx=x+dx[d], yy=y+dy[d], zz=z+dz[d];
				if(0<=xx&&xx<N&&0<=yy&&yy<N&&0<=zz&&zz<N&&XYZ[xx][yy][zz])
					uf.Union(id(x,y,z), id(xx,yy,zz));
			}
		}

		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y)
		for(int z=0; z<N; ++z) if(XYZ[x][y][z] && uf.size(id(x,y,z))!=cnt)
			return false;
		return true;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, ShadowSculpture().possible(XY, YZ, ZX));}
int main(){

CASE(0)
	string XY_[] = {"YN","NN"};
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = {"YN","NN"};
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = {"YN","NN"};
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = "Possible"; 
END
CASE(1)
	string XY_[] = {"YN","NY"};
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = {"YN","NY"};
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = {"YN","NY"};
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = "Impossible"; 
END
CASE(2)
	string XY_[] = {"YYY","YNY","YYY"};
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = {"YYY","YNY","YYY"};
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = {"YYY","YNY","YYY"};
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = "Possible"; 
END
CASE(3)
	string XY_[] = {"YYY","YNY","YYY"};
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = {"NYY","YNY","YYY"};
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = {"YYY","YNY","YYN"};
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = "Impossible"; 
END
CASE(4)
	string XY_[] = {"N"};
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = {"N"};
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = {"N"};
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = "Possible"; 
END
/*
CASE(5)
	string XY_[] = ;
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = ;
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = ;
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = ; 
END
CASE(6)
	string XY_[] = ;
	  vector <string> XY(XY_, XY_+sizeof(XY_)/sizeof(*XY_)); 
	string YZ_[] = ;
	  vector <string> YZ(YZ_, YZ_+sizeof(YZ_)/sizeof(*YZ_)); 
	string ZX_[] = ;
	  vector <string> ZX(ZX_, ZX_+sizeof(ZX_)/sizeof(*ZX_)); 
	string _ = ; 
END
*/
}
// END CUT HERE