Artifact Content
Not logged in

Artifact fc06f1f14b091ef5f3cbc302d2fb0b52dfba65f0


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

class TheKingsRoadsDiv1 { public:
	string getAnswer(int h, vector <int> a, vector <int> b)
	{
		return solve(h, a, b) ? "Correct" : "Incorrect";
	}

	bool solve(int h, const vector<int>& a, const vector<int>& b)
	{
		// remove obvious abnormality
		set<pair<int,int>> e;
		for(int i=0; i<a.size(); ++i)
			if(a[i] != b[i])
				e.emplace(min(a[i]-1, b[i]-1), max(a[i]-1,b[i]-1)); // I love 0-based indexing.

		if(e.size()+3 < a.size())
			return false;

		const int added = e.size() - ((1<<h)-2);
		const int N = (1<<h)-1;
		vector<vector<int>> g(N);
		for(auto ab: e) {
			g[ab.first].emplace_back(ab.second);
			g[ab.second].emplace_back(ab.first);
		}

		set<pair<int,int>> susp_edges = e;

		vector<int> det_height(N, 0);
		vector<vector<int>> det_child(N);
		vector<int> det_parent(N, -1);
		vector<int> Q;
		for(int v=0; v<N; ++v) {
			if(g[v].empty())
				return false;
			if(g[v].size() == 1) {
				det_height[v] = 1;
				Q.emplace_back(g[v].front());
			}
		}

		for(int dh=2; dh<=h; ++dh) {
			vector<int> Q2;
			for(int v: Q) {
				if(det_height[v] == dh)
					continue;
				else if(det_height[v] == 0) {
					det_height[v] = dh;
					vector<int> cand;
					for(int u: g[v])
						if(det_height[u] == 0)
							cand.push_back(u);
					if(dh==h)
						det_parent[v] = -2;
					if(cand.size() == 1 && dh<h) {
						int u = cand.front();
						Q2.push_back(u);
						det_child[u].emplace_back(v);
						det_parent[v] = u;
						if(det_child[u].size() > 2)
							return false;
						susp_edges.erase(make_pair(min(v,u), max(v,u)));
					}
				} else {
					return false;
				}
			}
			Q.swap(Q2);
		}

		queue<int> QQ;
		for(int v=0; v<N; ++v)
			QQ.push(v);
		while(!QQ.empty()){
			int v = QQ.front(); QQ.pop();
			if(det_height[v]==0 || det_parent[v]!=-1)
				continue;

			vector<int> cand;
			for(int u: g[v])
				if(det_height[u] == det_height[v]+1 || det_height[u]==0)
					cand.push_back(u);
			if(cand.size() == 1) {
				int u = cand.front();
				QQ.push(u);
				det_child[u].emplace_back(v);
				det_parent[v] = u;
				if(det_child[u].size() > 2)
					return false;
				susp_edges.erase(make_pair(min(v,u), max(v,u)));
			}
		}

		int cnt = 0;
		for(int dh: det_height)
			if(dh)
				++cnt;
		cerr << cnt << " / " << N << " @ " << susp_edges.size() << endl;
		return susp_edges.size() == added;
	}
};

// 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(_, TheKingsRoadsDiv1().getAnswer(h, a, b));}
int main(){

CASE(0)
	int h = 3; 
	int a_[] = {1, 3, 2, 2, 3, 7, 1, 5, 4};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {6, 5, 4, 7, 4, 3, 3, 1, 7};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = "Correct"; 
END
CASE(1)
	int h = 2; 
	int a_[] = {1, 2, 1, 3, 3};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {2, 1, 2, 3, 3};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = "Incorrect"; 
END
CASE(2)
	int h = 3; 
	int a_[] = {1, 3, 2, 2, 6, 6, 4, 4, 7};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {2, 1, 4, 5, 4, 4, 7, 7, 6};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = "Incorrect"; 
END
CASE(3)
	int h = 2; 
	int a_[] = {1, 2, 2, 1, 1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1, 2, 2, 1, 2};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = "Incorrect"; 
END
CASE(4)
	int h = 5; 
	int a_[] = {6, 15, 29, 28, 7, 13, 13, 23, 28, 13, 30, 27, 14, 4, 14, 19, 27, 20, 20, 19, 10, 15, 7, 7, 19, 29, 4, 24, 10, 23, 30, 6, 24};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {9, 22, 30, 20, 26, 25, 8, 7, 24, 21, 27, 31, 4, 28, 29, 6, 16, 1, 17, 10, 3, 12, 30, 18, 14, 23, 19, 21, 5, 13, 15, 2, 11};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = "Correct"; 
END
CASE(5)
	int h = 2; 
	int a_[] = {1,1,1,2,1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {2,3,1,2,2};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = "Correct"; 
END
/*
CASE(6)
	int h = ; 
	int a_[] = ;
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = ;
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = ; 
END
CASE(7)
	int h = ; 
	int a_[] = ;
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = ;
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	string _ = ; 
END
*/
}
// END CUT HERE