scarlyw的博客

如果世界真的不喜欢你,那世界就是我的敌人了。

NOI模拟(5.3) CQOID1T2 社交网络

社交网络

题目背景:

5.3 模拟 CQOI2018D1T2  

分析:矩阵树定理

 

求有向图的有根树形图,显然对于有根的树形图是可以直接用矩阵树定理的,那么直接建出入度矩阵和邻接表矩阵,入度矩阵 - 邻接表矩阵获得基尔霍夫矩阵。直接去掉1号点所在行列直接求就可以了。复杂度O(n3)

Source:


/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}


template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 250 + 10;
const int mod = 10007;
int n, m, x, y;
int a[MAXN][MAXN];

inline int mod_pow(int a, int b) {
	int ans = 1;
	for (; b; b >>= 1, a = (long long)a * a % mod)
		if (b & 1) ans = (long long)ans * a % mod;
	return ans;
}

inline void read_in() {
	R(n), R(m);
	for (int i = 1; i <= m; ++i) {
		R(x), R(y), x--, y--;
		if (x == y) continue ;
		a[x][x]++, a[y][x]--;
	}
}

inline void gauss() {
	int sign = 1;
	int ans = 1;
	for (int i = 1; i < n; ++i) {
		int pos = -1;
		for (int j = i; j < n; ++j)
			if (a[j][i] != 0) {
				pos = j;
				break ;
			}
		if (pos != i) {
			sign = -sign;
			for (int k = i; k < n; ++k) std::swap(a[i][k], a[pos][k]);
		}
		int inv = mod_pow(a[i][i], mod - 2); 
		for (int j = i + 1; j < n; ++j) {
			int t = (long long)a[j][i] * inv % mod;
			for (int k = i; k < n; ++k) 
				a[j][k] = (a[j][k] - t * a[i][k] % mod + mod) % mod;
		}
		ans = ans * a[i][i] % mod;
	}
	std::cout << (sign * ans + mod) % mod;
}

int main() {
	// freopen("sns.in", "r", stdin);
	// freopen("sns.out", "w", stdout);
	read_in();
	gauss();
	return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/80212802
文章标签: NOI 矩阵树定理
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

NOI模拟(5.3) CQOID1T2 社交网络

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭