Artifact Content
Not logged in

Artifact 607f8314241c61a9311a5fc84db4e96c92e2eb6b


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class StrangeCountry { public:
	int transform(vector <string> g) 
	{
		int nConn=g.size(), nEdge=0;

		vector<int> uf(g.size(), -1); // union_find
		for(int x=0; x<g.size(); ++x)
		{
			if( g[x].find('Y')==-1 )
				return -1; // orphan

			for(int y=x+1; y<g.size(); ++y)
				if( g[x][y]=='Y' ) {
					++nEdge;
					int rx=x; while( uf[rx]>=0 ) rx=uf[rx];
					int ry=y; while( uf[ry]>=0 ) ry=uf[ry];
					if( rx != ry ) uf[rx]=ry, --nConn;
				}
		}

		int redundant = nEdge - (g.size() - nConn);
		return redundant>=nConn-1 ? nConn-1 : -1;
	}
};

// 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 int& Expected, const int& 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(_, StrangeCountry().transform(g));}
int main(){

CASE(0)
	string g_[] = {"NY",
 "YN"};
	  vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); 
	int _ = 0; 
END
CASE(1)
	string g_[] = {"NYYNN",
 "YNYNN",
 "YYNNN",
 "NNNNY",
 "NNNYN"};
	  vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); 
	int _ = 1; 
END
CASE(2)
	string g_[] = {"NYYNNNN",
 "YNYNNNN",
 "YYNNNNN",
 "NNNNYYN",
 "NNNYNYY",
 "NNNYYNY",
 "NNNNYYN"};
	  vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); 
	int _ = 1; 
END
CASE(3)
	string g_[] = {"NYNYNNNNNNNN",
 "YNYNNNNNNNNN",
 "NYNYYNNNNNNN",
 "YNYNNNNNNNNN",
 "NNYNNYYNNNNN",
 "NNNNYNYNNNNN",
 "NNNNYYNNNNNN",
 "NNNNNNNNYYNN",
 "NNNNNNNYNYNN",
 "NNNNNNNYYNNN",
 "NNNNNNNNNNNY",
 "NNNNNNNNNNYN"};
	  vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); 
	int _ = 2; 
END
CASE(4)
	string g_[] = {"NYNNNN",
 "YNYNNN",
 "NYNYNN",
 "NNYNNN",
 "NNNNNY",
 "NNNNYN"};
	  vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); 
	int _ = -1; 
END

}
// END CUT HERE