ADDED SRM/650-U/1A.cpp Index: SRM/650-U/1A.cpp ================================================================== --- SRM/650-U/1A.cpp +++ SRM/650-U/1A.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static const unsigned MODVAL = 1000000007; +struct mint +{ + unsigned val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} + mint(unsigned x):val(x%MODVAL) {} + mint(LL x):val(x%MODVAL) {} +}; +mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } +mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } +mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } +mint operator+(mint x, mint y) { return x+=y; } +mint operator-(mint x, mint y) { return x-=y; } +mint operator*(mint x, mint y) { return x*=y; } + +mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } +mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } +mint operator/(mint x, mint y) { return x/=y; } + +class TaroFillingAStringDiv1 { public: + int getNumber(int, vector position, string value) + { + vector> sorted; + for(int i=0; i +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, TaroFillingAStringDiv1().getNumber(N, position, value));} +int main(){ + +CASE(0) + int N = 3; + int position_[] = {1, 3}; + vector position(position_, position_+sizeof(position_)/sizeof(*position_)); + string value = "AB"; + int _ = 2; +END +CASE(1) + int N = 4; + int position_[] = {2, 1, 3, 4}; + vector position(position_, position_+sizeof(position_)/sizeof(*position_)); + string value = "ABBA"; + int _ = 1; +END +CASE(2) + int N = 25; + int position_[] = {23, 4, 8, 1, 24, 9, 16, 17, 6, 2, 25, 15, 14, 7, 13}; + vector position(position_, position_+sizeof(position_)/sizeof(*position_)); + string value = "ABBBBABABBAAABA"; + int _ = 1; +END +CASE(3) + int N = 305; + int position_[] = {183, 115, 250, 1, 188, 193, 163, 221, 144, 191, 92, 192, 58, 215, 157, 187, 227, 177, 206, 15, 272, 232, 49, 11, 178, 59, 189, 246}; + vector position(position_, position_+sizeof(position_)/sizeof(*position_)); + string value = "ABAABBABBAABABBBBAAAABBABBBA"; + int _ = 43068480; +END +CASE(4) + int N = 1000000000; + int position_[] = {183, 115, 250, 1, 188, 193, 163, 221, 144, 191, 92, 192, 58, 215, 157, 187, 227, 177, 206, 15, 272, 232, 49, 11, 178, 59, 189, 246}; + vector position(position_, position_+sizeof(position_)/sizeof(*position_)); + string value = "ABAABBABBAABABBBBAAAABBABBBA"; + int _ = -1; +END +/* +CASE(5) + int N = ; + int position_[] = ; + vector position(position_, position_+sizeof(position_)/sizeof(*position_)); + string value = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/650-U/1B-U.cpp Index: SRM/650-U/1B-U.cpp ================================================================== --- SRM/650-U/1B-U.cpp +++ SRM/650-U/1B-U.cpp @@ -0,0 +1,206 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class TheKingsRoadsDiv1 { public: + string getAnswer(int h, vector a, vector b) + { + return solve(h, a, b) ? "Correct" : "Incorrect"; + } + + bool solve(int h, const vector& a, const vector& b) + { + // remove obvious abnormality + set> e; + for(int i=0; i> g(N); + for(auto ab: e) { + g[ab.first].emplace_back(ab.second); + g[ab.second].emplace_back(ab.first); + } + + set> susp_edges = e; + + vector det_height(N, 0); + vector> det_child(N); + vector det_parent(N, -1); + vector Q; + for(int v=0; v Q2; + for(int v: Q) { + if(det_height[v] == dh) + continue; + else if(det_height[v] == 0) { + det_height[v] = dh; + vector 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 2) + return false; + susp_edges.erase(make_pair(min(v,u), max(v,u))); + } + } else { + return false; + } + } + Q.swap(Q2); + } + + queue QQ; + for(int v=0; v 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 +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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 a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {6, 5, 4, 7, 4, 3, 3, 1, 7}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Correct"; +END +CASE(1) + int h = 2; + int a_[] = {1, 2, 1, 3, 3}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {2, 1, 2, 3, 3}; + vector 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 a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {2, 1, 4, 5, 4, 4, 7, 7, 6}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Incorrect"; +END +CASE(3) + int h = 2; + int a_[] = {1, 2, 2, 1, 1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {1, 2, 2, 1, 2}; + vector 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 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 b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Correct"; +END +CASE(5) + int h = 2; + int a_[] = {1,1,1,2,1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = {2,3,1,2,2}; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = "Correct"; +END +/* +CASE(6) + int h = ; + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = ; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = ; +END +CASE(7) + int h = ; + int a_[] = ; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int b_[] = ; + vector b(b_, b_+sizeof(b_)/sizeof(*b_)); + string _ = ; +END +*/ +} +// END CUT HERE