bzoj1016(矩阵树定理+MST)

版权声明:如果看得起就随便拿去用吧QWQ https://blog.csdn.net/qkoqhh/article/details/84190743

这个可以边做MST边求生成树的方案数,就是在加入相同边权的时候统计各个联通块的生成树方案数并缩成一点(因为不同MST方案数来自于取相同边权的边),然后把他们都乘起来就是了。。

然后写起来比较恶心就是了。。

/*
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 105
#define nm 10005 
#define pi 3.1415926535897931
using namespace std;
const int inf=31011;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
 
 
 



struct tmp{
    int x,y,v;
    bool operator<(const tmp&o)const{return v<o.v;}
}a[nm];
int g[NM][NM][NM],ans;
int n,m,f[NM],_f[NM],tot,d[NM],c[NM],cnt[NM];
int _find(int x){return _f[x]==x?x:_f[x]=_find(_f[x]);}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

int det(int a[][NM]){
    int ans=1;int n=a[0][0]-1;
    //inc(i,1,n){inc(j,1,n)printf("%d ",a[i][j]);putchar('\n');}
    inc(i,1,n)inc(j,i+1,n){
	for(int x=i,y=j;a[y][i];swap(x,y)){
	    int t=a[x][i]/a[y][i];
	    inc(k,i,n)a[x][k]+=inf-t*a[y][k]%inf,a[x][k]%=inf;
	}
	if(!a[i][i])ans=-ans;
	if(!a[i][i])inc(k,i,n)swap(a[i][k],a[j][k]);
    }
    inc(i,1,n)ans*=a[i][i],ans%=inf;ans+=inf;ans%=inf;
    return ans;
}

int main(){
    //freopen("data.in","r",stdin);
    n=read();m=read();ans=1;
    inc(i,1,m)a[i].x=read(),a[i].y=read(),a[i].v=read();
    sort(a+1,a+1+m);
    inc(i,1,n)f[i]=i;
    inc(i,1,m){
	int k=i;
	while(a[i].v==a[k+1].v&&k<m)k++;
	inc(j,1,n)_f[j]=j;mem(c);mem(cnt);mem(d);
	inc(j,i,k)if(find(a[j].x)!=find(a[j].y)){
	    int x=_find(find(a[j].x)),y=_find(find(a[j].y));
	    if(x!=y)_f[x]=y;
	}
	tot=0;
	inc(j,i,k)if(find(a[j].x)!=find(a[j].y)){
	    int x=find(a[j].x),y=find(a[j].y);int t=_find(x);
	    if(!c[t])c[t]=++tot,d[t]=1,cnt[c[t]]=1;
	    if(!d[x])d[x]=++cnt[c[t]];
	    if(!d[y])d[y]=++cnt[c[t]];
	}
	inc(j,1,tot)mem(g[j]),g[j][0][0]=cnt[j];
	inc(j,i,k)if(find(a[j].x)!=find(a[j].y)){
	    int x=find(a[j].x),y=find(a[j].y);int t=c[_find(x)];
	    g[t][d[x]][d[y]]--;g[t][d[y]][d[x]]--;
	    g[t][d[x]][d[x]]++;g[t][d[y]][d[y]]++;
	}
	inc(j,1,tot)ans*=det(g[j]),ans%=inf;
	inc(j,i,k){
	    int x=find(a[j].x),y=find(a[j].y);
	    if(x!=y)f[x]=y;
	}
	i=k;
    }
    inc(i,1,n)if(find(i)!=find(1))return 0*printf("0\n");
    return 0*printf("%d\n",ans);
}

1016: [JSOI2008]最小生成树计数

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 7601  Solved: 3201
[Submit][Status][Discuss]

Description

  现在给出了一个简单无向加权图。你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的
最小生成树。(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的)。由于不同的最小生
成树可能很多,所以你只需要输出方案数对31011的模就可以了。

Input

  第一行包含两个数,n和m,其中1<=n<=100; 1<=m<=1000; 表示该无向图的节点数和边数。每个节点用1~n的整
数编号。接下来的m行,每行包含两个整数:a, b, c,表示节点a, b之间的边的权值为c,其中1<=c<=1,000,000,0
00。数据保证不会出现自回边和重边。注意:具有相同权值的边不会超过10条。

Output

  输出不同的最小生成树有多少个。你只需要输出数量对31011的模就可以了。

Sample Input

4 6
1 2 1
1 3 1
1 4 1
2 3 2
2 4 1
3 4 1

Sample Output

8

HINT

Source

[Submit][Status][Discuss]



猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/84190743
今日推荐