Artifact Content
Not logged in

Artifact 46b17adf49a4bfa24c9430fbb48b0499b0bc47f3


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

template<typename T>
class IdGen
{
	map<T, int> v2id_;
	vector<T>   id2v_;
public:
	int v2id(const T& v) {
		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
		return v2id_[v];
	}
	const T& id2v(int i) const { return id2v_[i]; }
	int size() const { return id2v_.size(); }
};

template<typename Vert, typename Flow, int NV=2502>
class MaxFlow
{
	IdGen<Vert> idgen;
	vector<int> G[NV];
	Flow F[NV][NV];

public:
	void addEdge( Vert s_, Vert t_, Flow f )
	{
		const int s = idgen.v2id(s_), t = idgen.v2id(t_);
		G[s].push_back(t);
		G[t].push_back(s);
		F[s][t] = f;
		F[t][s] = 0;
	}

	Flow calc( Vert s_, Vert t_ )
	{
		const int S = idgen.v2id(s_), D = idgen.v2id(t_);
		for( Flow total=0 ;; ) {
			// Do BFS and compute the level for each node.
			int LV[NV] = {0};
			vector<int> Q(1, S);
			for(int lv=1; !Q.empty(); ++lv) {
				vector<int> Q2;
				for(size_t i=0; i!=Q.size(); ++i) {
					const vector<int>& ne = G[Q[i]];
					for(size_t j=0; j!=ne.size(); ++j)
						if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
							LV[ne[j]]=lv, Q2.push_back(ne[j]);
				}
				Q.swap(Q2);
			}

			// Destination is now unreachable. Done.
			if( !LV[D] )
				return total;

			// Iterating DFS.
			bool blocked[NV] = {};
			total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
		}
	}

private:
	Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
	{
		Flow flow_out = 0;
		for(size_t i=0; i!=G[v].size(); ++i) {
			int u = G[v][i];
			if( LV[v]+1==LV[u] && F[v][u] ) {
				Flow f = min(flow_in-flow_out, F[v][u]);
				if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
					F[v][u]  -= f;
					F[u][v]  += f;
					flow_out += f;
					if( flow_in == flow_out ) return flow_out;
				}
			}
		}
		blocked[v] = (flow_out==0);
		return flow_out;
	}
};

class OldBridges { public:
	string isPossible(vector <string> bridges, int a1, int a2, int an, int b1, int b2, int bn)
	{
		typedef pair<int,int> Vert;
		enum {SRC, SINK, VERT, E1, E2};
		static const int INF = 99999999;
		MaxFlow<Vert, int>* mf = new MaxFlow<Vert,int>;

		mf->addEdge(Vert(SRC,0), Vert(VERT,a1), 2*an);
		mf->addEdge(Vert(VERT,a2), Vert(SINK,0), 2*an);
		mf->addEdge(Vert(SRC,0), Vert(VERT,b1), 2*bn);
		mf->addEdge(Vert(VERT,b2), Vert(SINK,0), 2*bn);

		int N = bridges.size();
		for(int a=0; a<N; ++a)
		for(int b=a+1; b<N; ++b)
			if(bridges[a][b] == 'N') {
				mf->addEdge(Vert(VERT,a), Vert(VERT,b), INF);
				mf->addEdge(Vert(VERT,b), Vert(VERT,a), INF);
			} else if(bridges[a][b] == 'O') {
				Vert e1 = Vert(E1, a*N+b);
				Vert e2 = Vert(E2, a*N+b);
				mf->addEdge(Vert(VERT,a), e1, 2);
				mf->addEdge(Vert(VERT,b), e1, 2);
				mf->addEdge(e1, e2, 2);
				mf->addEdge(e2, Vert(VERT,a), 2);
				mf->addEdge(e2, Vert(VERT,b), 2);
			}

		int flow = mf->calc(Vert(SRC,0), Vert(SINK,0));
		delete mf;
		return flow==(an+an+bn+bn) ? "Yes" : "No";
	}
};

// 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 string& Expected, const string& 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(_, OldBridges().isPossible(bridges, a1, a2, an, b1, b2, bn));}
int main(){

CASE(0)
	string bridges_[] = {"XOXX","OXOX","XOXO","XXOX"};
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = 0; 
	int a2 = 1; 
	int an = 1; 
	int b1 = 2; 
	int b2 = 3; 
	int bn = 1; 
	string _ = "Yes"; 
END
CASE(1)
	string bridges_[] = {"XOXX","OXOX","XOXO","XXOX"};
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = 0; 
	int a2 = 2; 
	int an = 1; 
	int b1 = 1; 
	int b2 = 3; 
	int bn = 1; 
	string _ = "No"; 
END
CASE(2)
	string bridges_[] = {"XOXO","OXOX","XOXO","OXOX"};
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = 0; 
	int a2 = 2; 
	int an = 1; 
	int b1 = 1; 
	int b2 = 3; 
	int bn = 1; 
	string _ = "Yes"; 
END
CASE(3)
	string bridges_[] = {"XNXO","NXOX","XOXO","OXOX"};
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = 0; 
	int a2 = 2; 
	int an = 1; 
	int b1 = 1; 
	int b2 = 3; 
	int bn = 2; 
	string _ = "No"; 
END
CASE(4)
	string bridges_[] = {"XOXOO","OXOXO","XOXOO","OXOXO","OOOOX"};
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = 0; 
	int a2 = 2; 
	int an = 2; 
	int b1 = 1; 
	int b2 = 3; 
	int bn = 2; 
	string _ = "Yes"; 
END
CASE(5)
	string bridges_[] = {"XOOOX","OXOOX","OOXOX","OOOXN","XXXNX"};
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = 0; 
	int a2 = 4; 
	int an = 3; 
	int b1 = 1; 
	int b2 = 2; 
	int bn = 2; 
	string _ = "No"; 
END
/*
CASE(6)
	string bridges_[] = ;
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = ; 
	int a2 = ; 
	int an = ; 
	int b1 = ; 
	int b2 = ; 
	int bn = ; 
	string _ = ; 
END
CASE(7)
	string bridges_[] = ;
	  vector <string> bridges(bridges_, bridges_+sizeof(bridges_)/sizeof(*bridges_)); 
	int a1 = ; 
	int a2 = ; 
	int an = ; 
	int b1 = ; 
	int b2 = ; 
	int bn = ; 
	string _ = ; 
END
*/
}
// END CUT HERE