Artifact Content
Not logged in

Artifact 96cf85c87151dbba0c3f9c0786a8c57b6eed679e


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

typedef int vert;
typedef int cost;
typedef pair<cost,vert> cedge;

bool byVert( const cedge& e1, const cedge& e2 )
{
	if( e1.second != e2.second )
		return e1.second < e2.second;
	return e1.first < e2.first;
}

class TreesCount { public:
	int count(vector <string> graph) 
	{
		LL answer = 1;

		priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
		set<vert> V;
		Q.push( cedge(0, 0) );
		while( !Q.empty() )
		{
			const cost c0 = Q.top().first;

			vector<cedge> cand;
			while( !Q.empty() && Q.top().first==c0 ) {
				if( !V.count(Q.top().second) )
					cand.push_back( Q.top() );
				Q.pop();
			}

			sort( cand.begin(), cand.end(), byVert );
			for(int i=0; i<cand.size();) {
				cost c = cand[i].first;
				vert v = cand[i].second;
				int j = i;
				while( j<cand.size() && cand[j].second==v )
					++j;

				answer = answer * (j-i) % 1000000007;
				V.insert(v);
				for(int u=0; u<graph.size(); ++u)
					if( graph[v][u]>'0' && !V.count(u) )
						Q.push( cedge(c+graph[v][u]-'0', u) );

				i = j;
			}
		}

		return (int)answer;
	}
};

// 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(_, TreesCount().count(graph));}
int main(){

CASE(0)
	string graph_[] = {"01",
 "10"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 1; 
END
CASE(1)
	string graph_[] = {"011",
 "101",
 "110"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 1; 
END
CASE(2)
	string graph_[] = {"021",
 "201",
 "110"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 2; 
END
CASE(3)
	string graph_[] = {"0123",
 "1012",
 "2101",
 "3210"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 6; 
END
CASE(4)
	string graph_[] = {"073542",
 "705141",
 "350721",
 "517031",
 "442304",
 "211140"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 2; 
END
	/*
CASE(5)
	string graph_[] = ;
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = ; 
END
CASE(6)
	string graph_[] = ;
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = ; 
END
*/
}
// END CUT HERE