9ce62cec1c 2015-03-26 kinaba: #include <iostream> 9ce62cec1c 2015-03-26 kinaba: #include <sstream> 9ce62cec1c 2015-03-26 kinaba: #include <iomanip> 9ce62cec1c 2015-03-26 kinaba: #include <vector> 9ce62cec1c 2015-03-26 kinaba: #include <string> 9ce62cec1c 2015-03-26 kinaba: #include <map> 9ce62cec1c 2015-03-26 kinaba: #include <set> 9ce62cec1c 2015-03-26 kinaba: #include <algorithm> 9ce62cec1c 2015-03-26 kinaba: #include <numeric> 9ce62cec1c 2015-03-26 kinaba: #include <iterator> 9ce62cec1c 2015-03-26 kinaba: #include <functional> 9ce62cec1c 2015-03-26 kinaba: #include <complex> 9ce62cec1c 2015-03-26 kinaba: #include <queue> 9ce62cec1c 2015-03-26 kinaba: #include <stack> 9ce62cec1c 2015-03-26 kinaba: #include <cmath> 9ce62cec1c 2015-03-26 kinaba: #include <cassert> 9ce62cec1c 2015-03-26 kinaba: #include <tuple> 9ce62cec1c 2015-03-26 kinaba: using namespace std; 9ce62cec1c 2015-03-26 kinaba: typedef long long LL; 9ce62cec1c 2015-03-26 kinaba: typedef complex<double> CMP; 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: static const unsigned MODVAL = 1000000007; 9ce62cec1c 2015-03-26 kinaba: struct mint 9ce62cec1c 2015-03-26 kinaba: { 9ce62cec1c 2015-03-26 kinaba: unsigned val; 9ce62cec1c 2015-03-26 kinaba: mint():val(0){} 9ce62cec1c 2015-03-26 kinaba: mint(int x):val(x%MODVAL) {} 9ce62cec1c 2015-03-26 kinaba: mint(unsigned x):val(x%MODVAL) {} 9ce62cec1c 2015-03-26 kinaba: mint(LL x):val(x%MODVAL) {} 9ce62cec1c 2015-03-26 kinaba: }; 9ce62cec1c 2015-03-26 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 9ce62cec1c 2015-03-26 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 9ce62cec1c 2015-03-26 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 9ce62cec1c 2015-03-26 kinaba: mint operator+(mint x, mint y) { return x+=y; } 9ce62cec1c 2015-03-26 kinaba: mint operator-(mint x, mint y) { return x-=y; } 9ce62cec1c 2015-03-26 kinaba: mint operator*(mint x, mint y) { return x*=y; } 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 9ce62cec1c 2015-03-26 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 9ce62cec1c 2015-03-26 kinaba: mint operator/(mint x, mint y) { return x/=y; } 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: vector<mint> FAC_(1,1); 9ce62cec1c 2015-03-26 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[size_t(n)]; } 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: vector< vector<mint> > CP_; 9ce62cec1c 2015-03-26 kinaba: mint C(int n, int k) { 9ce62cec1c 2015-03-26 kinaba: while( CP_.size() <= n ) { 9ce62cec1c 2015-03-26 kinaba: int nn = CP_.size(); 9ce62cec1c 2015-03-26 kinaba: CP_.push_back(vector<mint>(nn+1,1)); 9ce62cec1c 2015-03-26 kinaba: for(int kk=1; kk<nn; ++kk) 9ce62cec1c 2015-03-26 kinaba: CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; 9ce62cec1c 2015-03-26 kinaba: } 9ce62cec1c 2015-03-26 kinaba: return k<0 || n<k ? 0 : CP_[n][k]; 9ce62cec1c 2015-03-26 kinaba: } 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: class TwoEntrances { public: 9ce62cec1c 2015-03-26 kinaba: int count(vector <int> a, vector <int> b, int s1, int s2) 9ce62cec1c 2015-03-26 kinaba: { 9ce62cec1c 2015-03-26 kinaba: const int N = a.size() + 1; 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: // Graph 9ce62cec1c 2015-03-26 kinaba: vector<vector<int>> g(N); 9ce62cec1c 2015-03-26 kinaba: for(int i=0; i<a.size(); ++i) { 9ce62cec1c 2015-03-26 kinaba: g[a[i]].push_back(b[i]); 9ce62cec1c 2015-03-26 kinaba: g[b[i]].push_back(a[i]); 9ce62cec1c 2015-03-26 kinaba: } 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: // Tree (as root=s1) 9ce62cec1c 2015-03-26 kinaba: vector<vector<int>> children(N); 9ce62cec1c 2015-03-26 kinaba: vector<int> parent(N, -1); 9ce62cec1c 2015-03-26 kinaba: vector<int> tree_size(N); 9ce62cec1c 2015-03-26 kinaba: function<int(int,int)> rec; rec = [&](int pre, int v) { 9ce62cec1c 2015-03-26 kinaba: int sub_size = 1; 9ce62cec1c 2015-03-26 kinaba: for(auto u: g[v]) if(u != pre) { 9ce62cec1c 2015-03-26 kinaba: children[v].push_back(u); 9ce62cec1c 2015-03-26 kinaba: parent[u] = v; 9ce62cec1c 2015-03-26 kinaba: sub_size += rec(v, u); 9ce62cec1c 2015-03-26 kinaba: } 9ce62cec1c 2015-03-26 kinaba: tree_size[v] = sub_size; 9ce62cec1c 2015-03-26 kinaba: }; 9ce62cec1c 2015-03-26 kinaba: rec(-1, s1); 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: // Mark s1--s2 9ce62cec1c 2015-03-26 kinaba: vector<bool> s2_path(N); 9ce62cec1c 2015-03-26 kinaba: for(int v=s2; v!=-1; v=parent[v]) 9ce62cec1c 2015-03-26 kinaba: s2_path[v] = true; 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: // Parts except s1--s2 path is easy. 9ce62cec1c 2015-03-26 kinaba: // It's dp-on-tree[s1 \setminus s2] * dp-on-tree[s1] * C(N, |s1|) * C(N-|s1|, |s2|). 9ce62cec1c 2015-03-26 kinaba: // CAUTION: ENSURE s1 and s2 NOT be included on dp-on-tree and |s|. 9ce62cec1c 2015-03-26 kinaba: // otherwise C(N, |s1|) will fix the timing s1 is filled, which reduces the freedom in 9ce62cec1c 2015-03-26 kinaba: // path mode. 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: // What about the rest N-|s1|-|s2|? 9ce62cec1c 2015-03-26 kinaba: } 9ce62cec1c 2015-03-26 kinaba: }; 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: // BEGIN CUT HERE 9ce62cec1c 2015-03-26 kinaba: #include <ctime> 9ce62cec1c 2015-03-26 kinaba: double start_time; string timer() 9ce62cec1c 2015-03-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9ce62cec1c 2015-03-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9ce62cec1c 2015-03-26 kinaba: { os << "{ "; 9ce62cec1c 2015-03-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9ce62cec1c 2015-03-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9ce62cec1c 2015-03-26 kinaba: void verify_case(const int& Expected, const int& Received) { 9ce62cec1c 2015-03-26 kinaba: bool ok = (Expected == Received); 9ce62cec1c 2015-03-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9ce62cec1c 2015-03-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9ce62cec1c 2015-03-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9ce62cec1c 2015-03-26 kinaba: #define END verify_case(_, TwoEntrances().count(a, b, s1, s2));} 9ce62cec1c 2015-03-26 kinaba: int main(){ 9ce62cec1c 2015-03-26 kinaba: 9ce62cec1c 2015-03-26 kinaba: CASE(0) 9ce62cec1c 2015-03-26 kinaba: int a_[] = {0, 1, 2}; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = {1, 2, 3}; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = 0; 9ce62cec1c 2015-03-26 kinaba: int s2 = 1; 9ce62cec1c 2015-03-26 kinaba: int _ = 4; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: CASE(1) 9ce62cec1c 2015-03-26 kinaba: int a_[] = {0, 1, 2}; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = {1, 2, 3}; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = 0; 9ce62cec1c 2015-03-26 kinaba: int s2 = 2; 9ce62cec1c 2015-03-26 kinaba: int _ = 9; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: CASE(2) 9ce62cec1c 2015-03-26 kinaba: int a_[] = {0, 1, 1, 3, 3, 3, 6, 7, 6}; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = 1; 9ce62cec1c 2015-03-26 kinaba: int s2 = 9; 9ce62cec1c 2015-03-26 kinaba: int _ = 16000; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: CASE(3) 9ce62cec1c 2015-03-26 kinaba: int a_[] = {0, 0, 1, 2, 3, 1, 2, 0, 6, 5, 10, 10}; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = {1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10, 11, 12}; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = 3; 9ce62cec1c 2015-03-26 kinaba: int s2 = 6; 9ce62cec1c 2015-03-26 kinaba: int _ = 310464; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: CASE(4) 9ce62cec1c 2015-03-26 kinaba: int a_[] = {0}; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = {1}; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = 1; 9ce62cec1c 2015-03-26 kinaba: int s2 = 0; 9ce62cec1c 2015-03-26 kinaba: int _ = 2; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: /* 9ce62cec1c 2015-03-26 kinaba: CASE(5) 9ce62cec1c 2015-03-26 kinaba: int a_[] = ; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = ; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = ; 9ce62cec1c 2015-03-26 kinaba: int s2 = ; 9ce62cec1c 2015-03-26 kinaba: int _ = ; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: CASE(6) 9ce62cec1c 2015-03-26 kinaba: int a_[] = ; 9ce62cec1c 2015-03-26 kinaba: vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 9ce62cec1c 2015-03-26 kinaba: int b_[] = ; 9ce62cec1c 2015-03-26 kinaba: vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 9ce62cec1c 2015-03-26 kinaba: int s1 = ; 9ce62cec1c 2015-03-26 kinaba: int s2 = ; 9ce62cec1c 2015-03-26 kinaba: int _ = ; 9ce62cec1c 2015-03-26 kinaba: END 9ce62cec1c 2015-03-26 kinaba: */ 9ce62cec1c 2015-03-26 kinaba: } 9ce62cec1c 2015-03-26 kinaba: // END CUT HERE