5cb1f42e86 2016-02-23 kinaba: #include <iostream> 5cb1f42e86 2016-02-23 kinaba: #include <sstream> 5cb1f42e86 2016-02-23 kinaba: #include <iomanip> 5cb1f42e86 2016-02-23 kinaba: #include <vector> 5cb1f42e86 2016-02-23 kinaba: #include <string> 5cb1f42e86 2016-02-23 kinaba: #include <map> 5cb1f42e86 2016-02-23 kinaba: #include <set> 5cb1f42e86 2016-02-23 kinaba: #include <algorithm> 5cb1f42e86 2016-02-23 kinaba: #include <numeric> 5cb1f42e86 2016-02-23 kinaba: #include <iterator> 5cb1f42e86 2016-02-23 kinaba: #include <functional> 5cb1f42e86 2016-02-23 kinaba: #include <complex> 5cb1f42e86 2016-02-23 kinaba: #include <queue> 5cb1f42e86 2016-02-23 kinaba: #include <stack> 5cb1f42e86 2016-02-23 kinaba: #include <cmath> 5cb1f42e86 2016-02-23 kinaba: #include <cassert> 5cb1f42e86 2016-02-23 kinaba: #include <tuple> 5cb1f42e86 2016-02-23 kinaba: using namespace std; 5cb1f42e86 2016-02-23 kinaba: typedef long long LL; 5cb1f42e86 2016-02-23 kinaba: typedef complex<double> CMP; 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: class BearSpans { public: 5cb1f42e86 2016-02-23 kinaba: vector <int> findAnyGraph(int n, int m, int k) 5cb1f42e86 2016-02-23 kinaba: { 5cb1f42e86 2016-02-23 kinaba: if(k>29 || (1<<k)>n) 5cb1f42e86 2016-02-23 kinaba: return vector<int>(1, -1); 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: // generate basic 5cb1f42e86 2016-02-23 kinaba: vector<int> ans = gen_base(1, 1<<k); 5cb1f42e86 2016-02-23 kinaba: for(int p=1, v=(1<<k)+1; v<=n; p=v++) { 5cb1f42e86 2016-02-23 kinaba: ans.push_back(p); 5cb1f42e86 2016-02-23 kinaba: ans.push_back(v); 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: vector<vector<bool>> used(n+1, vector<bool>(n+1)); 5cb1f42e86 2016-02-23 kinaba: for(int i=0; i<ans.size(); i+=2) { 5cb1f42e86 2016-02-23 kinaba: used[ans[i]][ans[i+1]] = true; 5cb1f42e86 2016-02-23 kinaba: used[ans[i+1]][ans[i]] = true; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: for(int s=1; s<=n && ans.size()<2*m; ++s) 5cb1f42e86 2016-02-23 kinaba: for(int e=s+1; e<=n && ans.size()<2*m; ++e) 5cb1f42e86 2016-02-23 kinaba: if(!used[s][e]) { 5cb1f42e86 2016-02-23 kinaba: ans.push_back(s); 5cb1f42e86 2016-02-23 kinaba: ans.push_back(e); 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: return ans; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: vector<int> gen_base(int vS, int vE) 5cb1f42e86 2016-02-23 kinaba: { 5cb1f42e86 2016-02-23 kinaba: if(vS+1 == vE) { 5cb1f42e86 2016-02-23 kinaba: vector<int> ans; 5cb1f42e86 2016-02-23 kinaba: ans.push_back(vS); 5cb1f42e86 2016-02-23 kinaba: ans.push_back(vE); 5cb1f42e86 2016-02-23 kinaba: return ans; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: int vM1 = vS + (vE-vS+1)/2 -1; 5cb1f42e86 2016-02-23 kinaba: int vM2 = vS + (vE-vS+1)/2; 5cb1f42e86 2016-02-23 kinaba: vector<int> a = gen_base(vS, vM1); 5cb1f42e86 2016-02-23 kinaba: vector<int> b = gen_base(vM2, vE); 5cb1f42e86 2016-02-23 kinaba: a.insert(a.end(), b.begin(), b.end()); 5cb1f42e86 2016-02-23 kinaba: a.push_back(vM1); 5cb1f42e86 2016-02-23 kinaba: a.push_back(vM2); 5cb1f42e86 2016-02-23 kinaba: return a; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: }; 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: // BEGIN CUT HERE 5cb1f42e86 2016-02-23 kinaba: struct UnionFind 5cb1f42e86 2016-02-23 kinaba: { 5cb1f42e86 2016-02-23 kinaba: vector<int> uf, sz; 5cb1f42e86 2016-02-23 kinaba: int nc; 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 5cb1f42e86 2016-02-23 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 5cb1f42e86 2016-02-23 kinaba: int size() 5cb1f42e86 2016-02-23 kinaba: { return nc; } 5cb1f42e86 2016-02-23 kinaba: int size(int a) 5cb1f42e86 2016-02-23 kinaba: { return sz[Find(a)]; } 5cb1f42e86 2016-02-23 kinaba: int Find(int a) 5cb1f42e86 2016-02-23 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 5cb1f42e86 2016-02-23 kinaba: // Semi-compression w.o. recursion. 5cb1f42e86 2016-02-23 kinaba: //{ while(uf[a] != a) a=uf[a]=uf[uf[a]]; return a; } 5cb1f42e86 2016-02-23 kinaba: bool Union(int a, int b) 5cb1f42e86 2016-02-23 kinaba: { 5cb1f42e86 2016-02-23 kinaba: a = Find(a); 5cb1f42e86 2016-02-23 kinaba: b = Find(b); 5cb1f42e86 2016-02-23 kinaba: if( a != b ) 5cb1f42e86 2016-02-23 kinaba: { 5cb1f42e86 2016-02-23 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 5cb1f42e86 2016-02-23 kinaba: uf[a] = b; 5cb1f42e86 2016-02-23 kinaba: sz[b] += sz[a]; 5cb1f42e86 2016-02-23 kinaba: --nc; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: return (a!=b); 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: }; 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: int verify(vector<int> ans) 5cb1f42e86 2016-02-23 kinaba: { 5cb1f42e86 2016-02-23 kinaba: int m = ans.size() / 2; 5cb1f42e86 2016-02-23 kinaba: int n = *max_element(ans.begin(), ans.end()); 5cb1f42e86 2016-02-23 kinaba: if(n == -1) 5cb1f42e86 2016-02-23 kinaba: return -1; 5cb1f42e86 2016-02-23 kinaba: vector<tuple<int,int,int>> cft; 5cb1f42e86 2016-02-23 kinaba: for(int i=0; i<m; ++i) 5cb1f42e86 2016-02-23 kinaba: cft.emplace_back(i+1, ans[2*i+0]-1, ans[2*i+1]-1); 5cb1f42e86 2016-02-23 kinaba: vector<bool> added(cft.size()); 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: UnionFind uf(n); 5cb1f42e86 2016-02-23 kinaba: int LoopCount=0; 5cb1f42e86 2016-02-23 kinaba: while(uf.size() > 1) { 5cb1f42e86 2016-02-23 kinaba: vector<int> to_add; 5cb1f42e86 2016-02-23 kinaba: vector<bool> done(n); 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: for(int e=0; e<cft.size(); ++e) if(!added[e]) { 5cb1f42e86 2016-02-23 kinaba: int a = uf.Find(get<1>(cft[e])); 5cb1f42e86 2016-02-23 kinaba: int b = uf.Find(get<2>(cft[e])); 5cb1f42e86 2016-02-23 kinaba: if(a!=b) { 5cb1f42e86 2016-02-23 kinaba: if(!done[a] || !done[b]) { 5cb1f42e86 2016-02-23 kinaba: to_add.push_back(e); 5cb1f42e86 2016-02-23 kinaba: done[a] = true; 5cb1f42e86 2016-02-23 kinaba: done[b] = true; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: for(int e: to_add) { 5cb1f42e86 2016-02-23 kinaba: added[e] = true; 5cb1f42e86 2016-02-23 kinaba: uf.Union(get<1>(cft[e]), get<2>(cft[e])); 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: LoopCount++; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: return LoopCount; 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: #include <ctime> 5cb1f42e86 2016-02-23 kinaba: double start_time; string timer() 5cb1f42e86 2016-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 5cb1f42e86 2016-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 5cb1f42e86 2016-02-23 kinaba: { os << "{ "; 5cb1f42e86 2016-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 5cb1f42e86 2016-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 5cb1f42e86 2016-02-23 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: bool ok = (verify(Expected) == verify(Received)); 5cb1f42e86 2016-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 5cb1f42e86 2016-02-23 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 5cb1f42e86 2016-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 5cb1f42e86 2016-02-23 kinaba: #define END verify_case(_, BearSpans().findAnyGraph(n, m, k));} 5cb1f42e86 2016-02-23 kinaba: int main(){ 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: CASE(0) 5cb1f42e86 2016-02-23 kinaba: int n = 17; 5cb1f42e86 2016-02-23 kinaba: int m = 22; 5cb1f42e86 2016-02-23 kinaba: int k = 1; 5cb1f42e86 2016-02-23 kinaba: int __[] = {1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 2, 3, 2, 8, 3, 9, 8, 9, 10, 12, 12, 14 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(1) 5cb1f42e86 2016-02-23 kinaba: int n = 9; 5cb1f42e86 2016-02-23 kinaba: int m = 12; 5cb1f42e86 2016-02-23 kinaba: int k = 3; 5cb1f42e86 2016-02-23 kinaba: int __[] = {6, 5, 7, 6, 1, 2, 3, 4, 8, 9, 3, 5, 7, 4, 1, 9, 6, 2, 6, 1, 1, 8, 4, 5 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(2) 5cb1f42e86 2016-02-23 kinaba: int n = 9; 5cb1f42e86 2016-02-23 kinaba: int m = 12; 5cb1f42e86 2016-02-23 kinaba: int k = 7; 5cb1f42e86 2016-02-23 kinaba: int __[] = {-1 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(3) 5cb1f42e86 2016-02-23 kinaba: int n = 1000; 5cb1f42e86 2016-02-23 kinaba: int m = 999; 5cb1f42e86 2016-02-23 kinaba: int k = 970; 5cb1f42e86 2016-02-23 kinaba: int __[] = {-1 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(4) 5cb1f42e86 2016-02-23 kinaba: int n = 2; 5cb1f42e86 2016-02-23 kinaba: int m = 1; 5cb1f42e86 2016-02-23 kinaba: int k = 1; 5cb1f42e86 2016-02-23 kinaba: int __[] = {1, 2 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(5) 5cb1f42e86 2016-02-23 kinaba: int n = 3; 5cb1f42e86 2016-02-23 kinaba: int m = 2; 5cb1f42e86 2016-02-23 kinaba: int k = 1; 5cb1f42e86 2016-02-23 kinaba: int __[] = {1, 2, 1, 3 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(6) 5cb1f42e86 2016-02-23 kinaba: int n = 3; 5cb1f42e86 2016-02-23 kinaba: int m = 3; 5cb1f42e86 2016-02-23 kinaba: int k = 2; 5cb1f42e86 2016-02-23 kinaba: int __[] = {-1 }; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: /* 5cb1f42e86 2016-02-23 kinaba: CASE(7) 5cb1f42e86 2016-02-23 kinaba: int n = ; 5cb1f42e86 2016-02-23 kinaba: int m = ; 5cb1f42e86 2016-02-23 kinaba: int k = ; 5cb1f42e86 2016-02-23 kinaba: int __[] = ; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: CASE(8) 5cb1f42e86 2016-02-23 kinaba: int n = ; 5cb1f42e86 2016-02-23 kinaba: int m = ; 5cb1f42e86 2016-02-23 kinaba: int k = ; 5cb1f42e86 2016-02-23 kinaba: int __[] = ; 5cb1f42e86 2016-02-23 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 5cb1f42e86 2016-02-23 kinaba: END 5cb1f42e86 2016-02-23 kinaba: */ 5cb1f42e86 2016-02-23 kinaba: 5cb1f42e86 2016-02-23 kinaba: } 5cb1f42e86 2016-02-23 kinaba: // END CUT HERE