Artifact Content
Not logged in

Artifact 39d81aae764f5374645ce2da2e9219f34e7cecc4


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

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N) : uf(N), sz(N, 1), nc(N)
	{
		for (int i = 0; i<N; ++i) uf[i] = i;
	}
	int size()
	{
		return nc;
	}
	int size(int a)
	{
		return sz[Find(a)];
	}
	int Find(int a)
	{
		return uf[a] == a ? a : uf[a] = Find(uf[a]);
	}
	// Semi-compression w.o. recursion.
	//{ while(uf[a] != a) a=uf[a]=uf[uf[a]]; return a; }
	bool Union(int a, int b)
	{
		a = Find(a);
		b = Find(b);
		if (a != b)
		{
			if (sz[a] >= sz[b]) swap(a, b);
			uf[a] = b;
			sz[b] += sz[a];
			--nc;
		}
		return (a != b);
	}
};

static const unsigned MODVAL = 987654323;
struct mint
{
	unsigned val;
	mint() :val(0) {}
	mint(int      x) :val((x+MODVAL)%MODVAL) {}
	mint(unsigned x) :val(x%MODVAL) {}
	mint(LL       x) :val((x+MODVAL)%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; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL - 2); }
mint operator/(mint x, mint y) { return x /= y; }

mint det(vector<vector<mint>> M)
{
	const int n = M.size();
	mint ans = 1;

	for (int i=0; i<n; ++i)
	{
		// pivot
		if (M[i][i].val == 0)
			for (int j = i + 1; j<n; ++j)
				if (M[j][i].val != 0)
				{
					swap(M[i], M[j]);
					break;
				}
		if (M[i][i].val == 0)
			return 0;

		ans *= M[i][i];

		// M[i][i] <-- 1
		mint rp = POW(M[i][i], MODVAL-2);
		for (int j = i; j<n; ++j)
			M[i][j] *= rp;

		// M[*][i] <-- 0
		for (int j = i+1; j<n; ++j)
		{
			mint r = M[j][i];
			for (int k = i + 1; k < n; ++k)
				M[j][k] -= M[i][k] * r;
		}
	}
	return ans;
}

class BuildingSpanningTreesDiv1 { public:
	int getNumberOfSpanningTrees(int n, vector <int> x, vector <int> y)
	{
		for (auto& xi : x) --xi;
		for (auto& yi : y) --yi;

		UnionFind uf(n);
		for (int i = 0; i < x.size(); ++i)
			if (!uf.Union(x[i], y[i]))
				return 0;

		vector<int> w;
		for (int v = 0; v < n; ++v)
			if (uf.Find(v) == v)
				w.push_back(uf.size(v));

		// complete graph with |w| vertices, w[i]--w[j] has w[i]*w[j] multiple edges.

		const int N = w.size();
		vector<vector<mint>> lap(N-1, vector<mint>(N-1));
		for (int v = 0; v < N - 1; ++v)
			for (int u = 0; u < N - 1; ++u)
				lap[v][u] = (v == u ? w[v] * (n - w[v]) : - w[v] * w[u]);
		return det(lap).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(_, BuildingSpanningTreesDiv1().getNumberOfSpanningTrees(n, x, y));}
int main(){

CASE(0)
	int n = 3; 
	int x_[] = {1,2};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {2,3};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = 1; 
END
CASE(1)
	int n = 5; 
	int x_[] = {1,3,4};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {2,4,5};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = 6; 
END
CASE(2)
	int n = 4; 
	int x_[] = {1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {2};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = 8; 
END
CASE(3)
	int n = 4; 
	int x_[] = {1,2,1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {2,3,3};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = 0; 
END
CASE(4)
	int n = 8; 
	int x_[] = {1,4,2,3,5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {4,7,6,5,8};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = 144; 
END
CASE(5)
	int n = 1000; 
	int x_[] = {1};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = {2};
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = 529013784; 
END
/*
CASE(6)
	int n = ; 
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = ;
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = ; 
END
CASE(7)
	int n = ; 
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int y_[] = ;
	  vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 
	int _ = ; 
END
*/
}
// END CUT HERE