Artifact Content
Not logged in

Artifact 9917d60b47bafaf7e4e77fd42f7e4e43e6041932


#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>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> 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; }

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

vector< vector<mint> > CP_;
mint C(int n, int 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 TwoEntrances { public:
	int count(vector <int> a, vector <int> b, int s1, int s2)
	{
		const int N = a.size() + 1;

		// Graph
		vector<vector<int>> g(N);
		for(int i=0; i<a.size(); ++i) {
			g[a[i]].push_back(b[i]);
			g[b[i]].push_back(a[i]);
		}

		// Tree (as root=s1)
		vector<vector<int>> children(N);
		vector<int> parent(N, -1);
		vector<int> tree_size(N);
		function<int(int,int)> rec; rec = [&](int pre, int v) {
			int sub_size = 1;
			for(auto u: g[v]) if(u != pre) {
				children[v].push_back(u);
				parent[u] = v;
				sub_size += rec(v, u);
			}
			tree_size[v] = sub_size;
		};
		rec(-1, s1);

		// Mark s1--s2
		vector<bool> s2_path(N);
		for(int v=s2; v!=-1; v=parent[v])
			s2_path[v] = true;

		// Parts except s1--s2 path is easy.
		// It's dp-on-tree[s1 \setminus s2] * dp-on-tree[s1] * C(N, |s1|) * C(N-|s1|, |s2|).
		// CAUTION: ENSURE s1 and s2 NOT be included on dp-on-tree and |s|.
		// otherwise C(N, |s1|) will fix the timing s1 is filled, which reduces the freedom in
		// path mode.

		// What about the rest N-|s1|-|s2|?
	}
};

// 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(_, TwoEntrances().count(a, b, s1, s2));}
int main(){

CASE(0)
	int a_[] = {0, 1, 2};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1, 2, 3};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = 0; 
	int s2 = 1; 
	int _ = 4; 
END
CASE(1)
	int a_[] = {0, 1, 2};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1, 2, 3};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = 0; 
	int s2 = 2; 
	int _ = 9; 
END
CASE(2)
	int a_[] = {0, 1, 1, 3, 3, 3, 6, 7, 6};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = 1; 
	int s2 = 9; 
	int _ = 16000; 
END
CASE(3)
	int a_[] = {0, 0, 1, 2, 3, 1, 2, 0, 6, 5, 10, 10};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10, 11, 12};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = 3; 
	int s2 = 6; 
	int _ = 310464; 
END
CASE(4)
	int a_[] = {0};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = 1; 
	int s2 = 0; 
	int _ = 2; 
END
/*
CASE(5)
	int a_[] = ;
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = ;
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = ; 
	int s2 = ; 
	int _ = ; 
END
CASE(6)
	int a_[] = ;
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = ;
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int s1 = ; 
	int s2 = ; 
	int _ = ; 
END
*/
}
// END CUT HERE