BZOJ 1016 生成树计数

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

  生成树的两个特点:

  1.可能有多个生成树,但是每种权值边出现的次数在每个树中是相同的。

  2.每一种生成树的每种权值边连接完成后形成的联通块状态相同。

  解法1:保证了权值相同的边不超过10条 所以我们可以先得出每种权值边的个数,再暴力dfs枚举每种可能性得出这种权值的所有情况,答案就是所有情况数的累乘。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 31011
using namespace std;
int n, m, cnt, tot, ans = 1, sum;
int fa[105];
struct edge {
    int x, y, v;
} e[1005];
struct data {
    int l, r, v;
} a[1005];
inline int read()
{
    int x = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x;
}
bool cmp(edge a, edge b)
{
    return a.v < b.v;
}
int find(int x)
{
    return x == fa[x] ? x : find(fa[x]);
}
void dfs(int x, int now, int k)
{
    if (now == a[x].r + 1) {
        if (k == a[x].v) {
            sum++;
        }
        return;
    }
    int p = find(e[now].x), q = find(e[now].y);
    if (p != q) {
        fa[p] = q;
        dfs(x, now + 1, k + 1);
        fa[p] = p;
        fa[q] = q;
    }
    dfs(x, now + 1, k);
}
int main()
{
    n = read();
    m = read();
    for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }
    for (int i = 1; i <= m; i++) {
        e[i].x = read(), e[i].y = read(), e[i].v = read();
    }
    sort(e + 1, e + m + 1, cmp);
    for (int i = 1; i <= m; i++) {
        if (e[i].v != e[i - 1].v) {
            a[++cnt].l = i;
            a[cnt - 1].r = i - 1;
        }
        int p = find(e[i].x), q = find(e[i].y);
        if (p != q) {
            fa[p] = q;
            a[cnt].v++;
            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 j = a[i].l; j <= a[i].r; j++) {
            int p = find(e[j].x), q = find(e[j].y);
            if (p != q && a[i].v) {
                fa[p] = q;
                a[i].v--;
            }
        }
    }
    printf("%d", ans);
    return 0;
}
View Code

  解法2:矩阵树

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
#define p 31011
#define N 1003
using namespace std;
int a[12][12],c[N][N],n,m,vis[N],fa[N],U[N];
vector<int> V[N];
struct data{
    int x,y,c;
    bool operator<(const data &a)const{
      return c<a.c;
    }
}e[N];
int gauss(int n)
{
    for (int i=1;i<=n;i++)
     for (int j=1;j<=n;j++) a[i][j]%=p;
    //for (int i=1;i<=n;i++,cout<<endl)
     // for (int j=1;j<=n;j++) cout<<a[i][j]<<" ";
    int ret=1;
    for (int i=1;i<=n;i++) {
        int num=i;
        for (int j=i+1;j<=n;j++)
         if (abs(a[j][i])) num=j;
        for (int j=1;j<=n;j++) swap(a[num][j],a[i][j]);
        if (num!=i) ret=-ret;
        for (int j=i+1;j<=n;j++)
         while (a[j][i]) {
            int t=a[j][i]/a[i][i];
            for (int k=1;k<=n;k++) 
             a[j][k]=(a[j][k]-t*a[i][k])%p;
            if (!a[j][i]) break;
            ret=-ret;
            for (int k=1;k<=n;k++) swap(a[i][k],a[j][k]);
         }
        ret=(ret*a[i][i])%p;
    }
    //cout<<ret<<endl;
    return (ret%p+p)%p;
}
int find(int x,int f[N])
{
    if (x==f[x]) return x;
    else return find(f[x],f);
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].c);
    sort(e+1,e+m+1);
    for (int i=1;i<=n;i++) vis[i]=0,fa[i]=i;
    int ans=1; int last=-1;
    for (int i=1;i<=m+1;i++) {
        if (e[i].c!=last||i==m+1) {
            for (int j=1;j<=n;j++)
             if (vis[j]) {
                int r1=find(j,U);
                V[r1].push_back(j);
                vis[j]=0;
             }
            for (int j=1;j<=n;j++)
             if (V[j].size()>1) {
                memset(a,0,sizeof(a));
                int len=V[j].size();
                for (int k=0;k<len;k++)
                 for (int l=k+1;l<len;l++) {
                    int x=V[j][k]; int y=V[j][l]; int t=c[x][y];
                    a[k+1][l+1]-=t; a[l+1][k+1]-=t;
                    a[k+1][k+1]+=t; a[l+1][l+1]+=t;
                 }
                ans=ans*gauss(len-1)%p;
                for (int k=0;k<len;k++) fa[V[j][k]]=j;
             }
            for (int j=1;j<=n;j++) {
                U[j]=fa[j]=find(j,fa);
                V[j].clear();
            }
            last=e[i].c;
            if(i==m+1) break;
        }
        int x=e[i].x; int y=e[i].y;
        int r1=find(x,fa); int r2=find(y,fa);
        if (r1==r2) continue;
        U[find(r1,U)]=find(r2,U); vis[r1]=1; vis[r2]=1;
        c[r1][r2]++; c[r2][r1]++;  
    }
    int flag=1;
    for (int i=2;i<=n;i++)
     if (find(i,U)!=find(i-1,U)) flag=0;
    ans=(ans*flag%p+p)%p;
    printf("%d\n",ans); 
}
View Code

 

posted @ 2019-02-01 22:39  Aragaki  阅读(...)  评论(... 编辑 收藏