Artifact Content
Not logged in

Artifact 0875dc7104a617f14faae3ebe00ad96745fc0389


#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;

typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;

bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] )
{
	for(int i=0; i<G[v].size(); ++i) {
		vert u = G[v][i];
		if( visited[u] ) continue;
		visited[u] = true;

		if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) )
			{ matchTo[v]=u, matchTo[u]=v; return true; }
	}
	return false;
}

template<int NV>
int biMatch( graph& G, int L ) // [0,L):left, [L,?):right
    // only left->right edges are used during computation
{
	vector<vert> matchTo(G.size(), -1);
	int ans = 0;
	for(vert v=0; v<L; ++v) {
		bool visited[NV] = {};
		if( augment(G, v, matchTo, visited) )
			++ans;
	}
	return ans;
}

class FoxAndGemstone { public:
	string isPossible(vector <string> bags)
	{
		set<char> gems;
		for(auto& bag: bags)
			for(char gem: bag)
				gems.insert(gem);

		vector<char> g(gems.begin(), gems.end());
		do {
			bool ok = false;
			for(int x=0; x<bags.size(); ++x) {
				bool fail = false;
				for(int y=0; y<bags.size(); ++y) if(x!=y)
					if(!surely_heavy(bags[x], bags[y], g))
						fail=true;
				if(!fail)
					ok = true;
			}
			if(!ok)
				return "Impossible";
		} while(next_permutation(g.begin(), g.end()));
		return "Possible";
	}

	// w[a] >= w[b] for sure
	bool surely_heavy(const string& a, const string& b, vector<char> g)
	{
		if(a.size() < b.size())
			return false;

		graph G(a.size()+b.size());
		for(int x=0; x<a.size(); ++x)
		for(int y=0; y<b.size(); ++y)
			if(find(g.begin(),g.end(),a[x]) >= find(g.begin(),g.end(),b[y]))
				G[x].emplace_back(a.size()+y);

		return biMatch<100>(G, a.size()) == b.size();
	}
};

// 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(_, FoxAndGemstone().isPossible(bags));}
int main(){

CASE(0)
	string bags_[] = {"AB", "AC"};
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = "Possible"; 
END
CASE(1)
	string bags_[] = {"A", "BC"};
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = "Impossible"; 
END
CASE(2)
	string bags_[] = {"A", "B", "C", "AB", "AC", "BC", "ABC"};
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = "Possible"; 
END
CASE(3)
	string bags_[] = {"AB","AC","AD","BC","BD","CD"};
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = "Possible"; 
END
CASE(4)
	string bags_[] = {"AB", "CD"};
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = "Impossible"; 
END
CASE(5)
	string bags_[] = {"A", "A", "A"};
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = "Possible"; 
END
/*
CASE(6)
	string bags_[] = ;
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = ; 
END
CASE(7)
	string bags_[] = ;
	  vector <string> bags(bags_, bags_+sizeof(bags_)/sizeof(*bags_)); 
	string _ = ; 
END
*/
}
// END CUT HERE