47cb6fde79 2013-06-08 kinaba: #include <iostream> 47cb6fde79 2013-06-08 kinaba: #include <sstream> 47cb6fde79 2013-06-08 kinaba: #include <iomanip> 47cb6fde79 2013-06-08 kinaba: #include <vector> 47cb6fde79 2013-06-08 kinaba: #include <string> 47cb6fde79 2013-06-08 kinaba: #include <map> 47cb6fde79 2013-06-08 kinaba: #include <set> 47cb6fde79 2013-06-08 kinaba: #include <algorithm> 47cb6fde79 2013-06-08 kinaba: #include <numeric> 47cb6fde79 2013-06-08 kinaba: #include <iterator> 47cb6fde79 2013-06-08 kinaba: #include <functional> 47cb6fde79 2013-06-08 kinaba: #include <complex> 47cb6fde79 2013-06-08 kinaba: #include <queue> 47cb6fde79 2013-06-08 kinaba: #include <stack> 47cb6fde79 2013-06-08 kinaba: #include <cmath> 47cb6fde79 2013-06-08 kinaba: #include <cassert> 47cb6fde79 2013-06-08 kinaba: using namespace std; 47cb6fde79 2013-06-08 kinaba: typedef long long LL; 47cb6fde79 2013-06-08 kinaba: typedef long double LD; 47cb6fde79 2013-06-08 kinaba: typedef complex<double> CMP; 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: class TreeUnion { public: 47cb6fde79 2013-06-08 kinaba: double expectedCycles(vector <string> tree1, vector <string> tree2, int K) 47cb6fde79 2013-06-08 kinaba: { 47cb6fde79 2013-06-08 kinaba: vector<int> t1, t2; 47cb6fde79 2013-06-08 kinaba: { 47cb6fde79 2013-06-08 kinaba: stringstream sin(accumulate(tree1.begin(), tree1.end(), string())); 47cb6fde79 2013-06-08 kinaba: for(int v; sin>>v; ) t1.push_back(v); 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: { 47cb6fde79 2013-06-08 kinaba: stringstream sin(accumulate(tree2.begin(), tree2.end(), string())); 47cb6fde79 2013-06-08 kinaba: for(int v; sin>>v; ) t2.push_back(v); 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: int N = t1.size() + 1; 47cb6fde79 2013-06-08 kinaba: vector<vector<int> > d1 = make_d(t1, N); 47cb6fde79 2013-06-08 kinaba: vector<vector<int> > d2 = make_d(t2, N); 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: map<int, int> len_to_cnt_2; 47cb6fde79 2013-06-08 kinaba: for(int u=0; u<N; ++u) 47cb6fde79 2013-06-08 kinaba: for(int v=u+1; v<N; ++v) 47cb6fde79 2013-06-08 kinaba: len_to_cnt_2[d2[u][v]] += 2; 47cb6fde79 2013-06-08 kinaba: int total = N * (N-1); 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: double e = 0.0; 47cb6fde79 2013-06-08 kinaba: for(int u=0; u<N; ++u) 47cb6fde79 2013-06-08 kinaba: for(int v=u+1; v<N; ++v) 47cb6fde79 2013-06-08 kinaba: { 47cb6fde79 2013-06-08 kinaba: e += (double)len_to_cnt_2[K - 2 - d1[u][v]] / total; 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: return e; 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: vector<vector<int> > make_d(const vector<int>& x, int N) 47cb6fde79 2013-06-08 kinaba: { 47cb6fde79 2013-06-08 kinaba: vector<vector<int> > d(N, vector<int>(N, 0x3fffffff)); 47cb6fde79 2013-06-08 kinaba: for(int i=0; i<N; ++i) d[i][i] = 0; 47cb6fde79 2013-06-08 kinaba: for(int i=0; i<x.size(); ++i) { 47cb6fde79 2013-06-08 kinaba: d[i+1][x[i]] = 1; 47cb6fde79 2013-06-08 kinaba: d[x[i]][i+1] = 1; 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: for(int k=0; k<N; ++k) 47cb6fde79 2013-06-08 kinaba: for(int i=0; i<N; ++i) 47cb6fde79 2013-06-08 kinaba: for(int j=0; j<N; ++j) 47cb6fde79 2013-06-08 kinaba: d[i][j] = min(d[i][j], d[i][k]+d[k][j]); 47cb6fde79 2013-06-08 kinaba: return d; 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: }; 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: // BEGIN CUT HERE 47cb6fde79 2013-06-08 kinaba: #include <ctime> 47cb6fde79 2013-06-08 kinaba: double start_time; string timer() 47cb6fde79 2013-06-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 47cb6fde79 2013-06-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 47cb6fde79 2013-06-08 kinaba: { os << "{ "; 47cb6fde79 2013-06-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 47cb6fde79 2013-06-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 47cb6fde79 2013-06-08 kinaba: void verify_case(const double& Expected, const double& Received) { 47cb6fde79 2013-06-08 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 47cb6fde79 2013-06-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 47cb6fde79 2013-06-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 47cb6fde79 2013-06-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 47cb6fde79 2013-06-08 kinaba: #define END verify_case(_, TreeUnion().expectedCycles(tree1, tree2, K));} 47cb6fde79 2013-06-08 kinaba: int main(){ 47cb6fde79 2013-06-08 kinaba: 47cb6fde79 2013-06-08 kinaba: CASE(0) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = {"0"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = {"0"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = 4; 47cb6fde79 2013-06-08 kinaba: double _ = 1.0; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: CASE(1) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = {"0 1"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = {"0 1"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = 4; 47cb6fde79 2013-06-08 kinaba: double _ = 1.3333333333333333; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: CASE(2) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = {"0 1"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = {"0 1"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = 6; 47cb6fde79 2013-06-08 kinaba: double _ = 0.3333333333333333; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: CASE(3) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = {"0 ", "1 1 1"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = {"0 1 0 ", "1"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = 5; 47cb6fde79 2013-06-08 kinaba: double _ = 4.0; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: CASE(4) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = {"0 1 2 0 1 2 0 1 2 5 6 1", "0 11", " 4"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = {"0 1 1 0 2 3 4 3 4 6 6", " 10 12 12"}; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = 7; 47cb6fde79 2013-06-08 kinaba: double _ = 13.314285714285713; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: /* 47cb6fde79 2013-06-08 kinaba: CASE(5) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = ; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = ; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = ; 47cb6fde79 2013-06-08 kinaba: double _ = ; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: CASE(6) 47cb6fde79 2013-06-08 kinaba: string tree1_[] = ; 47cb6fde79 2013-06-08 kinaba: vector <string> tree1(tree1_, tree1_+sizeof(tree1_)/sizeof(*tree1_)); 47cb6fde79 2013-06-08 kinaba: string tree2_[] = ; 47cb6fde79 2013-06-08 kinaba: vector <string> tree2(tree2_, tree2_+sizeof(tree2_)/sizeof(*tree2_)); 47cb6fde79 2013-06-08 kinaba: int K = ; 47cb6fde79 2013-06-08 kinaba: double _ = ; 47cb6fde79 2013-06-08 kinaba: END 47cb6fde79 2013-06-08 kinaba: */ 47cb6fde79 2013-06-08 kinaba: } 47cb6fde79 2013-06-08 kinaba: // END CUT HERE