zoukankan      html  css  js  c++  java
  • BZOJ5297 [Cqoi2018]社交网络 【矩阵树定理】

    题目链接

    BZOJ5297

    题解

    最近这玩意这么那么火
    这题要用到有向图的矩阵树定理
    主对角线上对应入度
    剩余位置如果有边则为(-1),不然为(0)

    (M_{i,i})即为以(i)为根的有向图生成树个数

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cls(s) memset(s,0,sizeof(s))
    #define cp pair<int,int>
    #define LL long long int
    using namespace std;
    const int maxn = 255,maxm = 100005,INF = 1000000000,P = 10007;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    int qpow(int a,int b){
    	int ans = 1;
    	for (; b; b >>= 1,a = a * a % P)
    		if (b & 1) ans = ans * a % P;
    	return ans;
    }
    int inv(int x){return qpow(x,P - 2);}
    int A[maxn][maxn],n,m;
    int gause(){
    	int rev = 1;
    	for (int i = 2; i <= n; i++){
    		int j = i;
    		for (int k = i + 1; k <= n; k++)
    			if (abs(A[k][i]) > abs(A[j][i]))
    				j = k;
    		if (j != i){
    			for (int k = i; k <= n; k++) swap(A[i][k],A[j][k]);
    			rev = -rev;
    		}
    		for (j = i + 1; j <= n; j++){
    			int t = A[j][i] * inv(A[i][i]) % P;
    			for (int k = i; k <= n; k++){
    				A[j][k] = ((A[j][k] - A[i][k] * t % P) % P + P) % P;
    			}
    		}
    	}
    	int re = 1;
    	for (int i = 2; i <= n; i++)
    		re = re * A[i][i] % P;
    	re = (re * rev % P + P) % P;
    	return re;
    }
    int main(){
    	n = read(); m = read();
    	int a,b;
    	while (m--){
    		a = read(); b = read();
    		if (a == b) continue;
    		A[b][a] = -1;
    		A[a][a]++;
    	}
    	printf("%d
    ",gause());
    	return 0;
    }
    
    
  • 相关阅读:
    JAVA学习笔记 -- 包资源文件jar包裹
    Hibernate一个简短的引论
    JMS分布式应用程序异步消息解决方案EhCache 高速缓存同步问题
    一个红
    Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
    hdu 2243 考研绝望——复杂的文字(AC自己主动机+矩阵高速功率)
    我在这里3在引发众1.8万的经验分享
    关系数据库的基本概念和MySQL说明
    2015第5周日
    HTML5
  • 原文地址:https://www.cnblogs.com/Mychael/p/9039094.html
Copyright © 2011-2022 走看看