892764d484 2014-09-17 kinaba: #include <iostream> 892764d484 2014-09-17 kinaba: #include <sstream> 892764d484 2014-09-17 kinaba: #include <iomanip> 892764d484 2014-09-17 kinaba: #include <vector> 892764d484 2014-09-17 kinaba: #include <string> 892764d484 2014-09-17 kinaba: #include <map> 892764d484 2014-09-17 kinaba: #include <set> 892764d484 2014-09-17 kinaba: #include <algorithm> 892764d484 2014-09-17 kinaba: #include <numeric> 892764d484 2014-09-17 kinaba: #include <iterator> 892764d484 2014-09-17 kinaba: #include <functional> 892764d484 2014-09-17 kinaba: #include <complex> 892764d484 2014-09-17 kinaba: #include <queue> 892764d484 2014-09-17 kinaba: #include <stack> 892764d484 2014-09-17 kinaba: #include <cmath> 892764d484 2014-09-17 kinaba: #include <cassert> 892764d484 2014-09-17 kinaba: #include <tuple> 892764d484 2014-09-17 kinaba: using namespace std; 892764d484 2014-09-17 kinaba: typedef long long LL; 892764d484 2014-09-17 kinaba: typedef complex<double> CMP; 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: static const unsigned MODVAL = 1000000007; 892764d484 2014-09-17 kinaba: struct mint 892764d484 2014-09-17 kinaba: { 892764d484 2014-09-17 kinaba: unsigned val; 892764d484 2014-09-17 kinaba: mint():val(0){} 892764d484 2014-09-17 kinaba: mint(int x):val(x%MODVAL) {} 892764d484 2014-09-17 kinaba: mint(unsigned x):val(x%MODVAL) {} 892764d484 2014-09-17 kinaba: mint(LL x):val(x%MODVAL) {} 892764d484 2014-09-17 kinaba: }; 892764d484 2014-09-17 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 892764d484 2014-09-17 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 892764d484 2014-09-17 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 892764d484 2014-09-17 kinaba: mint operator+(mint x, mint y) { return x+=y; } 892764d484 2014-09-17 kinaba: mint operator-(mint x, mint y) { return x-=y; } 892764d484 2014-09-17 kinaba: mint operator*(mint x, mint y) { return x*=y; } 892764d484 2014-09-17 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: class CandyCupRunningCompetition { public: 892764d484 2014-09-17 kinaba: int findMaximum(int N, vector <int> A, vector <int> B) 892764d484 2014-09-17 kinaba: { 892764d484 2014-09-17 kinaba: const int START = 0, GOAL = N-1; 892764d484 2014-09-17 kinaba: typedef mint Flow; 892764d484 2014-09-17 kinaba: typedef int FlowFlag; 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: vector<vector<FlowFlag>> FF(N, vector<FlowFlag>(N, 0)); 892764d484 2014-09-17 kinaba: vector<vector<Flow>> F(N, vector<Flow>(N, 0)); 892764d484 2014-09-17 kinaba: vector<vector<int>> G(N); 892764d484 2014-09-17 kinaba: for(int i=0; i<A.size(); ++i) { 892764d484 2014-09-17 kinaba: int a = A[i], b = B[i]; 892764d484 2014-09-17 kinaba: FF[a][b] = FF[b][a] = i+1; 892764d484 2014-09-17 kinaba: F[a][b] = F[b][a] = POW(3, i); 892764d484 2014-09-17 kinaba: G[a].push_back(b); 892764d484 2014-09-17 kinaba: G[b].push_back(a); 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: Flow total = 0; 892764d484 2014-09-17 kinaba: for(;;) { 892764d484 2014-09-17 kinaba: // BFS 892764d484 2014-09-17 kinaba: vector<int> prev(N, -1); 892764d484 2014-09-17 kinaba: queue<int> q; 892764d484 2014-09-17 kinaba: prev[START] = START; 892764d484 2014-09-17 kinaba: q.push(START); 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: while(!q.empty()) { 892764d484 2014-09-17 kinaba: int u = q.front(); q.pop(); 892764d484 2014-09-17 kinaba: for(int v: G[u]) if(prev[v]==-1 && FF[u][v]) { 892764d484 2014-09-17 kinaba: q.push(v); 892764d484 2014-09-17 kinaba: prev[v] = u; 892764d484 2014-09-17 kinaba: if(v == GOAL) 892764d484 2014-09-17 kinaba: goto endBfs; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: endBfs: 892764d484 2014-09-17 kinaba: if(prev[GOAL] == -1) 892764d484 2014-09-17 kinaba: break; // no more flow 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: Flow f; 892764d484 2014-09-17 kinaba: FlowFlag ff = 0x7fffffff; 892764d484 2014-09-17 kinaba: for(int v=GOAL; v!=prev[v]; v=prev[v]) { 892764d484 2014-09-17 kinaba: int u = prev[v]; 892764d484 2014-09-17 kinaba: if(FF[u][v] < ff) { 892764d484 2014-09-17 kinaba: ff = FF[u][v]; 892764d484 2014-09-17 kinaba: f = F[u][v]; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: total += f; 892764d484 2014-09-17 kinaba: for(int v=GOAL; v!=prev[v]; v=prev[v]) { 892764d484 2014-09-17 kinaba: int u = prev[v]; 892764d484 2014-09-17 kinaba: F[u][v] -= f; 892764d484 2014-09-17 kinaba: F[v][u] += f; 892764d484 2014-09-17 kinaba: if(FF[u][v] == ff) { 892764d484 2014-09-17 kinaba: FF[u][v] = 0; 892764d484 2014-09-17 kinaba: FF[v][u] = ff; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: return total.val; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: }; 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: // BEGIN CUT HERE 892764d484 2014-09-17 kinaba: #include <ctime> 892764d484 2014-09-17 kinaba: double start_time; string timer() 892764d484 2014-09-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 892764d484 2014-09-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 892764d484 2014-09-17 kinaba: { os << "{ "; 892764d484 2014-09-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 892764d484 2014-09-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 892764d484 2014-09-17 kinaba: void verify_case(const int& Expected, const int& Received) { 892764d484 2014-09-17 kinaba: bool ok = (Expected == Received); 892764d484 2014-09-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 892764d484 2014-09-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 892764d484 2014-09-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 892764d484 2014-09-17 kinaba: #define END verify_case(_, CandyCupRunningCompetition().findMaximum(N, A, B));} 892764d484 2014-09-17 kinaba: int main(){ 892764d484 2014-09-17 kinaba: CASE(0) 892764d484 2014-09-17 kinaba: int N = 3; 892764d484 2014-09-17 kinaba: int A_[] = {0,1}; 892764d484 2014-09-17 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 892764d484 2014-09-17 kinaba: int B_[] = {1,2}; 892764d484 2014-09-17 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 892764d484 2014-09-17 kinaba: int _ = 1; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(1) 892764d484 2014-09-17 kinaba: int N = 4; 892764d484 2014-09-17 kinaba: int A_[] = {0,1,0,2}; 892764d484 2014-09-17 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 892764d484 2014-09-17 kinaba: int B_[] = {1,3,2,3}; 892764d484 2014-09-17 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 892764d484 2014-09-17 kinaba: int _ = 10; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(2) 892764d484 2014-09-17 kinaba: int N = 3; 892764d484 2014-09-17 kinaba: int A_[] = {0}; 892764d484 2014-09-17 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 892764d484 2014-09-17 kinaba: int B_[] = {1}; 892764d484 2014-09-17 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 892764d484 2014-09-17 kinaba: int _ = 0; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(3) 892764d484 2014-09-17 kinaba: int N = 5; 892764d484 2014-09-17 kinaba: vector <int> A; 892764d484 2014-09-17 kinaba: vector <int> B; 892764d484 2014-09-17 kinaba: int _ = 0; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(4) 892764d484 2014-09-17 kinaba: int N = 6; 892764d484 2014-09-17 kinaba: int A_[] = {1,1,2,0,4,3,0,1,4}; 892764d484 2014-09-17 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 892764d484 2014-09-17 kinaba: int B_[] = {3,2,3,1,5,5,2,4,3}; 892764d484 2014-09-17 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 892764d484 2014-09-17 kinaba: int _ = 39; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: /* 892764d484 2014-09-17 kinaba: CASE(5) 892764d484 2014-09-17 kinaba: int N = ; 892764d484 2014-09-17 kinaba: int A_[] = ; 892764d484 2014-09-17 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 892764d484 2014-09-17 kinaba: int B_[] = ; 892764d484 2014-09-17 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 892764d484 2014-09-17 kinaba: int _ = ; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(6) 892764d484 2014-09-17 kinaba: int N = ; 892764d484 2014-09-17 kinaba: int A_[] = ; 892764d484 2014-09-17 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 892764d484 2014-09-17 kinaba: int B_[] = ; 892764d484 2014-09-17 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 892764d484 2014-09-17 kinaba: int _ = ; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: */ 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: // END CUT HERE