Minimum Spanning Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1408 Accepted Submission(s): 450
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.
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.
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 | We have carefully selected several similar problems for you:
5379
5378
5377
5376
5375
种类数可以使负的吗?一直wa,去掉det函数里边小于0时,ret-=ret就过了
ac代码
Problem : 4408 ( Minimum Spanning Tree ) Judge Status : Accepted
RunId : 14484663 Language : C++ Author : lwj1994
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stdlib.h>
using namespace std;
struct s
{
int u,v,w;
}edge[5010];
int cmp(const void *a,const void *b)
{
return (*(struct s *)a).w-(*(struct s *)b).w;
}
int n,m;
long long mod;
int pre[1010],f[1010],vis[1010];
long long G[1010][1010],C[1010][1010];
int find(int x,int *pre)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x],pre);
}
vector<int>vt[1010];
void init()
{
int i;
for(i=0;i<=n;i++)
{
f[i]=i;
vis[i]=0;
// vt[i].clear();
}
for(i=0;i<=500;i++)
vt[i].clear();
}
long long det(long long a[][1010],int n)
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]%=mod;
}
}
long long ret=1;
for(i=1;i<n;i++)
{
for(j=i+1;j<n;j++)
{
while(a[j][i])
{
int t=a[i][i]/a[j][i];
for(k=i;k<n;k++)
{
a[i][k]=(a[i][k]-a[j][k]*t)%mod;
}
for(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;
}
// if(ret<0)
// ret=-ret;
return (ret+mod)%mod;
}
int main()
{
while(scanf("%d%d%lld",&n,&m,&mod)!=EOF)
{
if(n==0&&m==0&&mod==0)
break;
int i,j,k;
memset(G,0,sizeof(G));
memset(vis,0,sizeof(vis));
memset(C,0,sizeof(C));
memset(pre,0,sizeof(pre));
memset(f,0,sizeof(f));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
}
if(m==0)
{
printf("0\n");
continue;
}
qsort(edge,m,sizeof(edge[0]),cmp);
init();
int w=-1,a,b;
long long ans=1;
for(k=0;k<=m;k++)
{
if(edge[k].w!=w||k==m)
{
for(i=1;i<=n;i++)
{
if(vis[i])
{
int u=find(i,pre);
vt[u].push_back(i);
vis[i]=0;
}
}
for(i=1;i<=n;i++)
{
if(vt[i].size()>1)
{
int len=vt[i].size();
// memset(C,0,sizeof(C));
for(a=1;a<=n;a++)
for(b=1;b<=n;b++)
C[a][b]=0;
for(a=0;a<len;a++)
{
for(b=a+1;b<len;b++)
{
int a1=vt[i][a];
int b1=vt[i][b];
C[b][a]-=G[a1][b1];
C[a][b]=C[b][a];
C[a][a]+=G[a1][b1];
C[b][b]+=G[a1][b1];
}
}
int ret=det(C,len);
// printf("+++++%d\n",ret);
ans=(ans*ret)%mod;
for(a=0;a<len;a++)
{
f[vt[i][a]]=i;
}
}
}
for(i=1;i<=n;i++)
{
pre[i]=f[i]=find(i,f);
vt[i].clear();
}
if(k==m)
break;
w=edge[k].w;
}
int a=edge[k].u;
int b=edge[k].v;
int fa=find(a,f);
int fb=find(b,f);
if(fa!=fb)
{
int fx=find(fa,pre),fy=find(fb,pre);
vis[fa]=vis[fb]=1;
pre[fx]=fy;
G[fa][fb]++;
G[fb][fa]++;
}
}
// printf("%d\n",ans);
int flag=0;
for(i=2;i<=n;i++)
{
if(flag)
break;
if(pre[i]!=pre[i-1])
flag=1;
}
if(!flag)
printf("%lld\n",ans%mod);
else
printf("0\n");
}
}