Artifact Content
Not logged in

Artifact 2e1145b0d110a2f1334d252beea09a38518032cc


#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; }

class CandyCupRunningCompetition { public:
	int findMaximum(int N, vector <int> A, vector <int> B)
	{
		const int START = 0, GOAL = N-1;
		typedef mint Flow;
		typedef int FlowFlag;

		vector<vector<FlowFlag>> FF(N, vector<FlowFlag>(N, 0));
		vector<vector<Flow>> F(N, vector<Flow>(N, 0));
		vector<vector<int>> G(N);
		for(int i=0; i<A.size(); ++i) {
			int a = A[i], b = B[i];
			FF[a][b] = FF[b][a] = i+1;
			F[a][b] = F[b][a] = POW(3, i);
			G[a].push_back(b);
			G[b].push_back(a);
		}

		Flow total = 0;
		for(;;) {
			// BFS
			vector<int> prev(N, -1);
			queue<int> q;
			prev[START] = START;
			q.push(START);

			while(!q.empty()) {
				int u = q.front(); q.pop();
				for(int v: G[u]) if(prev[v]==-1 && FF[u][v]) {
					q.push(v);
					prev[v] = u;
					if(v == GOAL)
						goto endBfs;
				}
			}
		endBfs:
			if(prev[GOAL] == -1)
				break; // no more flow

			Flow f;
			FlowFlag ff = 0x7fffffff;
			for(int v=GOAL; v!=prev[v]; v=prev[v]) {
				int u = prev[v];
				if(FF[u][v] < ff) {
					ff = FF[u][v];
					f = F[u][v];
				}
			}

			total += f;
			for(int v=GOAL; v!=prev[v]; v=prev[v]) {
				int u = prev[v];
				F[u][v] -= f;
				F[v][u] += f;
				if(FF[u][v] == ff) {
					FF[u][v] = 0;
					FF[v][u] = ff;
				}
			}
		}
		return total.val;
	}
};

// 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(_, CandyCupRunningCompetition().findMaximum(N, A, B));}
int main(){
CASE(0)
	int N = 3; 
	int A_[] = {0,1};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1,2};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int _ = 1; 
END
CASE(1)
	int N = 4; 
	int A_[] = {0,1,0,2};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {1,3,2,3};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int _ = 10; 
END
CASE(2)
	int N = 3; 
	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 _ = 0; 
END
CASE(3)
	int N = 5; 
	vector <int> A; 
	vector <int> B; 
	int _ = 0; 
END
CASE(4)
	int N = 6; 
	int A_[] = {1,1,2,0,4,3,0,1,4};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {3,2,3,1,5,5,2,4,3};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int _ = 39; 
END
/*
CASE(5)
	int N = ; 
	int A_[] = ;
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int _ = ; 
END
CASE(6)
	int N = ; 
	int A_[] = ;
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	int _ = ; 
END
*/
}
// END CUT HERE