• bzoj1016 [JSOI2008]最小生成树计数


    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,000。数据保证不会出现自回边和重边。注意:具有相同权值的边不会超过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
     
    这题好像是经典题
    先算出每种长度的边的数量,显然用这些边组成MST的方案和其他边是互不相干的
    比如当前MST中只有1、2、5,要从长度为k的所有边的集合中选两条,凑成1、2、3、4、5,那么这两条的选取和长度不为k的边是无关的
    所以直接爆搜每种长度的边,然后就可以直接乘起来
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<deque>
    #include<set>
    #include<map>
    #include<ctime>
    #define LL long long
    #define inf 0x7ffffff
    #define pa pair<int,int>
    #define mod 31011
    using namespace std;
    inline LL read()
    {
        LL x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    struct edge{int x,y,z;}e[100010];
    inline bool operator <(const edge &a,const edge &b){return a.z<b.z;}
    struct seg{int l,r,v;}a[100010];
    int n,m,cnt,tot,sum,ans=1;
    int fa[100010];
    inline int getfa(int x){return fa[x]==x?x:getfa(fa[x]);}
    inline void dfs(int x,int now,int k)
    {
    	if (now==a[x].r+1)
    	{
    		if (k==a[x].v)sum++;
    		return;
    	}
    	int fx=getfa(e[now].x),fy=getfa(e[now].y);
    	if (fx!=fy)
    	{
    		fa[fx]=fy;
    		dfs(x,now+1,k+1);
    		fa[fx]=fx;fa[fy]=fy;
    	}
    	dfs(x,now+1,k);
    }
    int main()
    {
    	n=read();m=read();
    	for (int i=1;i<=m;i++)
    	{
    		e[i].x=read();
    		e[i].y=read();
    		e[i].z=read();
    	}
    	sort(e+1,e+m+1);
    	for (int i=1;i<=n;i++)fa[i]=i;
    	for (int i=1;i<=m;i++)
    	{
    		if (e[i].z!=e[i-1].z)
    		{
    			a[++cnt].l=i;
    			a[cnt-1].r=i-1;	
    		}
    		int fx=getfa(e[i].x),fy=getfa(e[i].y);
    		if (fx!=fy)
    		{
    			a[cnt].v++;
    			fa[fx]=fy;
    			tot++;
    		}
    	}
    	a[cnt].r=m;
    	if (tot!=n-1)
    	{
    		printf("0");
    		return 0;
    	}
    	for(int i=1;i<=n;i++)fa[i]=i;
    	for(int i=1;i<=cnt;i++)
    	{
    		sum=0;
    		dfs(i,a[i].l,0);
    		ans=(ans*sum)%mod;
    		for (int k=a[i].l;k<=a[i].r;k++)
    		{
    			int fx=getfa(e[k].x),fy=getfa(e[k].y);
    			if (fx!=fy)fa[fx]=fy;
    		}
    	}
    	printf("%d
    ",ans);
    }
    
    ——by zhber,转载请注明来源
  • 相关阅读:
    OpenStack 发行版本
    刷新linux硬盘存储接口
    LVM实践
    LVM man帮助
    ansible --help 文档
    nmcli connection modify eth1 ipv4.addr "192.168.31.23" ipv4.method manual
    自己动手使用, MetaWeblog 发布博客(cnblogs)
    测试图片上传 on Markdown editor
    大批量更新数据mysql批量更新的四种方法
    PHP print_r 转换/还原为数组
  • 原文地址:https://www.cnblogs.com/zhber/p/4215788.html
Copyright © 2020-2023  润新知