zoukankan      html  css  js  c++  java
  • HDU 4408 Minimum Spanning Tree 第37届ACM/ICPC 金华赛区网络赛1009 题 (最小生成树计数)

    Minimum Spanning Tree

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 205    Accepted Submission(s): 65


    Problem Description
    XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertexes and m (0<=m<=1000) edges, he wants to know the number of minimum spanning trees in the graph.
     
    Input
    There are no more than 15 cases. The input ends by 0 0 0.
    For each case, the first line begins with three integers --- the above mentioned n, m, and p. The meaning of p will be explained later. Each the following m lines contains three integers u, v, w (1<=w<=10), which describes that there is an edge weighted w between vertex u and vertex v( all vertex are numbered for 1 to n) . It is guaranteed that there are no multiple edges and no loops in the graph.
     
    Output
    For each test case, output a single integer in one line representing the number of different minimum spanning trees in the graph.
    The answer may be quite large. You just need to calculate the remainder of the answer when divided by p (1<=p<=1000000000). p is above mentioned, appears in the first line of each test case.
     
    Sample Input
    5 10 12 2 5 3 2 4 2 3 1 3 3 4 2 1 2 3 5 4 3 5 1 3 4 1 1 5 3 3 3 2 3 0 0 0
     
    Sample Output
    4
     
    Source
     
    Recommend
    zhoujiaqi2010
     
     
     
    网上找的代码,修改了下AC了。。。
    好坑啊。。。。杯具了
    #include <map>
    #include <stack>
    #include <queue>
    #include <math.h>
    #include <vector>
    #include <string>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    #include <algorithm>
    #define N 405
    #define M 4005
    #define E
    #define inf 0x3f3f3f3f
    #define dinf 1e10
    #define linf (LL)1<<60
    #define LL long long
    #define clr(a,b) memset(a,b,sizeof(a))
    using namespace std;
    
    LL mod;
    struct Edge
    {
        int a,b,c;
        bool operator<(const Edge & t)const
        {
            return c<t.c;
        }
    }edge[M];
    int n,m;
    LL ans;
    int fa[N],ka[N],vis[N];
    LL gk[N][N],tmp[N][N];
    vector<int>gra[N];
    
    int findfa(int a,int b[]){return a==b[a]?a:b[a]=findfa(b[a],b);}
    
    LL det(LL a[][N],int n)
    {
        for(int i=0;i<n;i++)for(int j=0;j<n;j++)a[i][j]%=mod;
        long long ret=1;
        for(int i=1;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
                while(a[j][i])
                {
                    LL t=a[i][i]/a[j][i];
                    for(int k=i;k<n;k++)
                        a[i][k]=(a[i][k]-a[j][k]*t)%mod;
                    for(int k=i;k<n;k++)
                        swap(a[i][k],a[j][k]);
                    ret=-ret;
                }
            if(a[i][i]==0)return 0;
            ret=ret*a[i][i]%mod;
            //ret%=mod;
        }
        return (ret+mod)%mod;
    }
    
    int main()
    {
        while(scanf("%d%d%I64d",&n,&m,&mod)==3)
        {
    
            if(n==0 && m==0 && mod==0)break;
    
            memset(gk,0,sizeof(gk));
            memset(tmp,0,sizeof(tmp));
            memset(fa,0,sizeof(fa));
            memset(ka,0,sizeof(ka));
            memset(tmp,0,sizeof(tmp));
    
            for(int i=0;i<N;i++)gra[i].clear();
            for(int i=0;i<m;i++)
                scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
            sort(edge,edge+m);
            for(int i=1;i<=n;i++)fa[i]=i,vis[i]=0;
            int pre=-1;
            ans=1;
            for(int h=0;h<=m;h++)
            {
                if(edge[h].c!=pre||h==m)
                {
                    for(int i=1;i<=n;i++)
                        if(vis[i])
                        {
                            int u=findfa(i,ka);
                            gra[u].push_back(i);
                            vis[i]=0;
                        }
                    for(int i=1;i<=n;i++)
                        if(gra[i].size()>1)
                        {
                            for(int a=1;a<=n;a++)
                                for(int b=1;b<=n;b++)
                                    tmp[a][b]=0;
                            int len=gra[i].size();
                            for(int a=0;a<len;a++)
                                for(int b=a+1;b<len;b++)
                                {
                                    int la=gra[i][a],lb=gra[i][b];
                                    tmp[a][b]=(tmp[b][a]-=gk[la][lb]);
                                    tmp[a][a]+=gk[la][lb];tmp[b][b]+=gk[la][lb];
                                }
                            long long ret=(long long)det(tmp,len);
                            ret%=mod;
                            ans=(ans*ret%mod)%mod;
                            for(int a=0;a<len;a++)fa[gra[i][a]]=i;
                        }
                    for(int i=1;i<=n;i++)
                    {
                        ka[i]=fa[i]=findfa(i,fa);
                        gra[i].clear();
                    }
                    if(h==m)break;
                    pre=edge[h].c;
                }
                int a=edge[h].a,b=edge[h].b;
                int pa=findfa(a,fa),pb=findfa(b,fa);
                if(pa==pb)continue;
                vis[pa]=vis[pb]=1;
                ka[findfa(pa,ka)]=findfa(pb,ka);
                gk[pa][pb]++;gk[pb][pa]++;
            }
            int flag=0;
            for(int i=2;i<=n&&!flag;i++)if(ka[i]!=ka[i-1])flag=1;
            ans%=mod;
            printf("%I64d\n",flag?0:ans);
        }
        return 0;
    }
  • 相关阅读:
    OO先导课——第二次上课
    OO先导课——第一次上课
    OO先导课——JAVA初见懵的知识合集
    OO先导课——作业(1)
    在驱动和应用程序间共享内存
    【求助】NdisSend,自定义数据包发送失败?
    HTTP协议详解(真的很经典)
    原始数据包的分析
    IP数据包的校验和算法
    基于IMD的包过滤防火墙原理与实现
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2698446.html
Copyright © 2011-2022 走看看