4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef int vert; 4fd800b3a8 2011-02-23 kinaba: typedef int cost; 4fd800b3a8 2011-02-23 kinaba: typedef pair<cost,vert> cedge; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool byVert( const cedge& e1, const cedge& e2 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( e1.second != e2.second ) 4fd800b3a8 2011-02-23 kinaba: return e1.second < e2.second; 4fd800b3a8 2011-02-23 kinaba: return e1.first < e2.first; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class TreesCount { public: 4fd800b3a8 2011-02-23 kinaba: int count(vector <string> graph) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL answer = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: priority_queue< cedge, vector<cedge>, greater<cedge> > Q; 4fd800b3a8 2011-02-23 kinaba: set<vert> V; 4fd800b3a8 2011-02-23 kinaba: Q.push( cedge(0, 0) ); 4fd800b3a8 2011-02-23 kinaba: while( !Q.empty() ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const cost c0 = Q.top().first; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<cedge> cand; 4fd800b3a8 2011-02-23 kinaba: while( !Q.empty() && Q.top().first==c0 ) { 4fd800b3a8 2011-02-23 kinaba: if( !V.count(Q.top().second) ) 4fd800b3a8 2011-02-23 kinaba: cand.push_back( Q.top() ); 4fd800b3a8 2011-02-23 kinaba: Q.pop(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: sort( cand.begin(), cand.end(), byVert ); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<cand.size();) { 4fd800b3a8 2011-02-23 kinaba: cost c = cand[i].first; 4fd800b3a8 2011-02-23 kinaba: vert v = cand[i].second; 4fd800b3a8 2011-02-23 kinaba: int j = i; 4fd800b3a8 2011-02-23 kinaba: while( j<cand.size() && cand[j].second==v ) 4fd800b3a8 2011-02-23 kinaba: ++j; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: answer = answer * (j-i) % 1000000007; 4fd800b3a8 2011-02-23 kinaba: V.insert(v); 4fd800b3a8 2011-02-23 kinaba: for(int u=0; u<graph.size(); ++u) 4fd800b3a8 2011-02-23 kinaba: if( graph[v][u]>'0' && !V.count(u) ) 4fd800b3a8 2011-02-23 kinaba: Q.push( cedge(c+graph[v][u]-'0', u) ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: i = j; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return (int)answer; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, TreesCount().count(graph));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = {"01", 4fd800b3a8 2011-02-23 kinaba: "10"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = {"011", 4fd800b3a8 2011-02-23 kinaba: "101", 4fd800b3a8 2011-02-23 kinaba: "110"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = {"021", 4fd800b3a8 2011-02-23 kinaba: "201", 4fd800b3a8 2011-02-23 kinaba: "110"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = {"0123", 4fd800b3a8 2011-02-23 kinaba: "1012", 4fd800b3a8 2011-02-23 kinaba: "2101", 4fd800b3a8 2011-02-23 kinaba: "3210"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 6; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = {"073542", 4fd800b3a8 2011-02-23 kinaba: "705141", 4fd800b3a8 2011-02-23 kinaba: "350721", 4fd800b3a8 2011-02-23 kinaba: "517031", 4fd800b3a8 2011-02-23 kinaba: "442304", 4fd800b3a8 2011-02-23 kinaba: "211140"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string graph_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE