Artifact Content
Not logged in

Artifact f4634643344316b41e26ffad9c26c6eb822ae28a


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

static const unsigned MODVAL = 1000000009;
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; }

vector<mint> FAC_(1,1);
mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; }

// nCk :: O(1) time, O(n^2) space.
vector< vector<mint> > CP_;
mint C(LL n, LL k) {
	while( CP_.size() <= n ) {
		int nn = CP_.size();
		CP_.push_back(vector<mint>(nn+1,1));
		for(int kk=1; kk<nn; ++kk)
			CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk];
	}
	return k<0 || n<k ? 0 : CP_[n][k];
}

class InducedSubgraphs { public:
	int getCount(vector <int> edge1, vector <int> edge2, int k)
	{
		int N = edge1.size()+1;
		vector< vector<int> > G(N);
		for(int i=0; i<edge1.size(); ++i)
		{
			int v = edge1[i], u = edge2[i];
			G[v].push_back(u);
			G[u].push_back(v);
		}
		return solve(G, N, k).val;
	}

	mint solve(const vector<vector<int> >& G, int N, int K)
	{
		mint total = 0;
		for(int root=0; root<N; ++root)
			total += solve_rooted(G, N, K, root);
		return total;
	}

	mint solve_rooted(const vector<vector<int> >& G, int N, int K, int root)
	{
		memo.clear();

		int pre = -1, cur = root;
		for(int below=N,above=0 ;; --below,++above)
			if(below <= K) {
				mint b = rec(G, pre, cur).second;
				mint a = (above>=K-1 ? mint(1) : FAC(K-above));
				return b*a;
			} else {
				if((pre==-1 && G[cur].size()>=2) || G[cur].size()>=3) {
					// fill_branch
/*
					vector< pair<int,int> > v_sz;
					for(int k=0; k<G[cur].size(); ++k) if(G[cur][k] != pre)
						v_sz.push_back(make_pair(G[cur][k], rec(G, cur, G[cur][k])));

					int total_size = 0;
					for(int k=0; k<v_sz.size(); ++k)
						total_size += v_sz[k].second;
					for(int k=0; k<v_sz.size(); ++k)
						if( v_sz[k].second+1 <= K )
*/
					return 0;
				}
				
				int nex = (G[cur][0]!=pre ? G[cur][0] : G[cur][1]);
				pre = cur;
				cur = nex;
			}
	}

	// (size, number of toplofical sorting).
	map<int, pair<int,mint> > memo;
	pair<int,mint> rec(const vector<vector<int> >& G, int pre, int cur)
	{
		if(memo.count(cur))
			return memo[cur];

		vector< pair<int,mint> > children;
		for(int i=0; i<G[cur].size(); ++i) if(G[cur][i] != pre)
			children.push_back(rec(G, cur, G[cur][i]));

		mint pat  = 1;
		int  size = 0;
		for(int i=0; i<children.size(); ++i) {
			size += children[i].first;
			pat   = pat * C(size, children[i].first) * children[i].second;
		}
		return memo[cur] = make_pair(size+1, pat);
	}
};

// 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(_, InducedSubgraphs().getCount(edge1, edge2, k));}
int main(){

CASE(0)
	int edge1_[] = {0, 1};
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {1, 2};
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 2; 
	int _ = 2; 
END
CASE(1)
	int edge1_[] = {0, 1, 3};
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {2, 2, 2};
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 3; 
	int _ = 12; 
END
CASE(2)
	int edge1_[] = {5, 0, 1, 2, 2};
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {0, 1, 2, 4, 3};
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 3; 
	int _ = 4; 
END
CASE(3)
	int edge1_[] = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 11; 
	int _ = 481904640; 
END
CASE(4)
	int edge1_[] = {5, 9, 4, 10, 10, 0, 7, 6, 2, 1, 11, 8}
;
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {0, 0, 10, 3, 0, 6, 1, 1, 12, 12, 7, 11}
;
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 6; 
	int _ = 800; 
END
CASE(5)
	int edge1_[] = {0, 5, 1, 0, 2, 3, 5}
;
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {4, 7, 0, 6, 7, 5, 0}
;
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 3; 
	int _ = 0; 
END
CASE(6)
	int edge1_[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = 1; 
	int _ = 890964601; 
END
/*
CASE(7)
	int edge1_[] = ;
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = ;
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = ; 
	int _ = ; 
END
CASE(8)
	int edge1_[] = ;
	  vector <int> edge1(edge1_, edge1_+sizeof(edge1_)/sizeof(*edge1_)); 
	int edge2_[] = ;
	  vector <int> edge2(edge2_, edge2_+sizeof(edge2_)/sizeof(*edge2_)); 
	int k = ; 
	int _ = ; 
END
*/
}
// END CUT HERE