155c399d58 2012-12-08 kinaba: #include <iostream> 155c399d58 2012-12-08 kinaba: #include <sstream> 155c399d58 2012-12-08 kinaba: #include <iomanip> 155c399d58 2012-12-08 kinaba: #include <vector> 155c399d58 2012-12-08 kinaba: #include <string> 155c399d58 2012-12-08 kinaba: #include <map> 155c399d58 2012-12-08 kinaba: #include <set> 155c399d58 2012-12-08 kinaba: #include <algorithm> 155c399d58 2012-12-08 kinaba: #include <numeric> 155c399d58 2012-12-08 kinaba: #include <iterator> 155c399d58 2012-12-08 kinaba: #include <functional> 155c399d58 2012-12-08 kinaba: #include <complex> 155c399d58 2012-12-08 kinaba: #include <queue> 155c399d58 2012-12-08 kinaba: #include <stack> 155c399d58 2012-12-08 kinaba: #include <cmath> 155c399d58 2012-12-08 kinaba: #include <cassert> 155c399d58 2012-12-08 kinaba: using namespace std; 155c399d58 2012-12-08 kinaba: typedef long long LL; 155c399d58 2012-12-08 kinaba: typedef long double LD; 155c399d58 2012-12-08 kinaba: typedef complex<LD> CMP; 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: static const unsigned MODVAL = 1000000009; 155c399d58 2012-12-08 kinaba: struct mint 155c399d58 2012-12-08 kinaba: { 155c399d58 2012-12-08 kinaba: unsigned val; 155c399d58 2012-12-08 kinaba: mint():val(0){} 155c399d58 2012-12-08 kinaba: mint(int x):val(x%MODVAL) {} 155c399d58 2012-12-08 kinaba: mint(unsigned x):val(x%MODVAL) {} 155c399d58 2012-12-08 kinaba: mint(LL x):val(x%MODVAL) {} 155c399d58 2012-12-08 kinaba: }; 155c399d58 2012-12-08 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 155c399d58 2012-12-08 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 155c399d58 2012-12-08 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 155c399d58 2012-12-08 kinaba: mint operator+(mint x, mint y) { return x+=y; } 155c399d58 2012-12-08 kinaba: mint operator-(mint x, mint y) { return x-=y; } 155c399d58 2012-12-08 kinaba: mint operator*(mint x, mint y) { return x*=y; } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: vector<mint> FAC_(1,1); 155c399d58 2012-12-08 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: // nCk :: O(1) time, O(n^2) space. 155c399d58 2012-12-08 kinaba: vector< vector<mint> > CP_; 155c399d58 2012-12-08 kinaba: mint C(LL n, LL k) { 155c399d58 2012-12-08 kinaba: while( CP_.size() <= n ) { 155c399d58 2012-12-08 kinaba: int nn = CP_.size(); 155c399d58 2012-12-08 kinaba: CP_.push_back(vector<mint>(nn+1,1)); 155c399d58 2012-12-08 kinaba: for(int kk=1; kk<nn; ++kk) 155c399d58 2012-12-08 kinaba: CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: return k<0 || n<k ? 0 : CP_[n][k]; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: class InducedSubgraphs { public: 155c399d58 2012-12-08 kinaba: int getCount(vector <int> edge1, vector <int> edge2, int k) 155c399d58 2012-12-08 kinaba: { 155c399d58 2012-12-08 kinaba: int N = edge1.size()+1; 155c399d58 2012-12-08 kinaba: vector< vector<int> > G(N); 155c399d58 2012-12-08 kinaba: for(int i=0; i<edge1.size(); ++i) 155c399d58 2012-12-08 kinaba: { 155c399d58 2012-12-08 kinaba: int v = edge1[i], u = edge2[i]; 155c399d58 2012-12-08 kinaba: G[v].push_back(u); 155c399d58 2012-12-08 kinaba: G[u].push_back(v); 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: return solve(G, N, k).val; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: mint solve(const vector<vector<int> >& G, int N, int K) 155c399d58 2012-12-08 kinaba: { 155c399d58 2012-12-08 kinaba: mint total = 0; 155c399d58 2012-12-08 kinaba: for(int root=0; root<N; ++root) 155c399d58 2012-12-08 kinaba: total += solve_rooted(G, N, K, root); 155c399d58 2012-12-08 kinaba: return total; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: mint solve_rooted(const vector<vector<int> >& G, int N, int K, int root) 155c399d58 2012-12-08 kinaba: { 155c399d58 2012-12-08 kinaba: memo.clear(); 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: int pre = -1, cur = root; 155c399d58 2012-12-08 kinaba: for(int below=N,above=0 ;; --below,++above) 155c399d58 2012-12-08 kinaba: if(below <= K) { 155c399d58 2012-12-08 kinaba: mint b = rec(G, pre, cur).second; 155c399d58 2012-12-08 kinaba: mint a = (above>=K-1 ? mint(1) : FAC(K-above)); 155c399d58 2012-12-08 kinaba: return b*a; 155c399d58 2012-12-08 kinaba: } else { 155c399d58 2012-12-08 kinaba: if((pre==-1 && G[cur].size()>=2) || G[cur].size()>=3) { 155c399d58 2012-12-08 kinaba: // fill_branch 155c399d58 2012-12-08 kinaba: /* 155c399d58 2012-12-08 kinaba: vector< pair<int,int> > v_sz; 155c399d58 2012-12-08 kinaba: for(int k=0; k<G[cur].size(); ++k) if(G[cur][k] != pre) 155c399d58 2012-12-08 kinaba: v_sz.push_back(make_pair(G[cur][k], rec(G, cur, G[cur][k]))); 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: int total_size = 0; 155c399d58 2012-12-08 kinaba: for(int k=0; k<v_sz.size(); ++k) 155c399d58 2012-12-08 kinaba: total_size += v_sz[k].second; 155c399d58 2012-12-08 kinaba: for(int k=0; k<v_sz.size(); ++k) 155c399d58 2012-12-08 kinaba: if( v_sz[k].second+1 <= K ) 155c399d58 2012-12-08 kinaba: */ 155c399d58 2012-12-08 kinaba: return 0; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: int nex = (G[cur][0]!=pre ? G[cur][0] : G[cur][1]); 155c399d58 2012-12-08 kinaba: pre = cur; 155c399d58 2012-12-08 kinaba: cur = nex; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: // (size, number of toplofical sorting). 155c399d58 2012-12-08 kinaba: map<int, pair<int,mint> > memo; 155c399d58 2012-12-08 kinaba: pair<int,mint> rec(const vector<vector<int> >& G, int pre, int cur) 155c399d58 2012-12-08 kinaba: { 155c399d58 2012-12-08 kinaba: if(memo.count(cur)) 155c399d58 2012-12-08 kinaba: return memo[cur]; 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: vector< pair<int,mint> > children; 155c399d58 2012-12-08 kinaba: for(int i=0; i<G[cur].size(); ++i) if(G[cur][i] != pre) 155c399d58 2012-12-08 kinaba: children.push_back(rec(G, cur, G[cur][i])); 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: mint pat = 1; 155c399d58 2012-12-08 kinaba: int size = 0; 155c399d58 2012-12-08 kinaba: for(int i=0; i<children.size(); ++i) { 155c399d58 2012-12-08 kinaba: size += children[i].first; 155c399d58 2012-12-08 kinaba: pat = pat * C(size, children[i].first) * children[i].second; 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: return memo[cur] = make_pair(size+1, pat); 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: }; 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: // BEGIN CUT HERE 155c399d58 2012-12-08 kinaba: #include <ctime> 155c399d58 2012-12-08 kinaba: double start_time; string timer() 155c399d58 2012-12-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 155c399d58 2012-12-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 155c399d58 2012-12-08 kinaba: { os << "{ "; 155c399d58 2012-12-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 155c399d58 2012-12-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 155c399d58 2012-12-08 kinaba: void verify_case(const int& Expected, const int& Received) { 155c399d58 2012-12-08 kinaba: bool ok = (Expected == Received); 155c399d58 2012-12-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 155c399d58 2012-12-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 155c399d58 2012-12-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 155c399d58 2012-12-08 kinaba: #define END verify_case(_, InducedSubgraphs().getCount(edge1, edge2, k));} 155c399d58 2012-12-08 kinaba: int main(){ 155c399d58 2012-12-08 kinaba: 155c399d58 2012-12-08 kinaba: CASE(0) 155c399d58 2012-12-08 kinaba: int edge1_[] = {0, 1}; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {1, 2}; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 2; 155c399d58 2012-12-08 kinaba: int _ = 2; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(1) 155c399d58 2012-12-08 kinaba: int edge1_[] = {0, 1, 3}; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {2, 2, 2}; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 3; 155c399d58 2012-12-08 kinaba: int _ = 12; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(2) 155c399d58 2012-12-08 kinaba: int edge1_[] = {5, 0, 1, 2, 2}; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {0, 1, 2, 4, 3}; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 3; 155c399d58 2012-12-08 kinaba: int _ = 4; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(3) 155c399d58 2012-12-08 kinaba: int edge1_[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6}; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 11; 155c399d58 2012-12-08 kinaba: int _ = 481904640; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(4) 155c399d58 2012-12-08 kinaba: int edge1_[] = {5, 9, 4, 10, 10, 0, 7, 6, 2, 1, 11, 8} 155c399d58 2012-12-08 kinaba: ; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {0, 0, 10, 3, 0, 6, 1, 1, 12, 12, 7, 11} 155c399d58 2012-12-08 kinaba: ; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 6; 155c399d58 2012-12-08 kinaba: int _ = 800; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(5) 155c399d58 2012-12-08 kinaba: int edge1_[] = {0, 5, 1, 0, 2, 3, 5} 155c399d58 2012-12-08 kinaba: ; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {4, 7, 0, 6, 7, 5, 0} 155c399d58 2012-12-08 kinaba: ; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 3; 155c399d58 2012-12-08 kinaba: int _ = 0; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(6) 155c399d58 2012-12-08 kinaba: int edge1_[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = 1; 155c399d58 2012-12-08 kinaba: int _ = 890964601; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: /* 155c399d58 2012-12-08 kinaba: CASE(7) 155c399d58 2012-12-08 kinaba: int edge1_[] = ; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = ; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = ; 155c399d58 2012-12-08 kinaba: int _ = ; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: CASE(8) 155c399d58 2012-12-08 kinaba: int edge1_[] = ; 155c399d58 2012-12-08 kinaba: vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 155c399d58 2012-12-08 kinaba: int edge2_[] = ; 155c399d58 2012-12-08 kinaba: vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 155c399d58 2012-12-08 kinaba: int k = ; 155c399d58 2012-12-08 kinaba: int _ = ; 155c399d58 2012-12-08 kinaba: END 155c399d58 2012-12-08 kinaba: */ 155c399d58 2012-12-08 kinaba: } 155c399d58 2012-12-08 kinaba: // END CUT HERE