# 【BZOJ1016】【JSOI2008】最小生成树计数

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

8

## Code

#include <stdio.h>
#include <algorithm>
#define MN 105
#define MM 1005
#define R register
#define mod 31011
R int x; R bool f; R char c;
for (f=0; (c=getchar())<'0'||c>'9'; f=c=='-');
for (x=c-'0'; (c=getchar())>='0'&&c<='9'; x=(x<<3)+(x<<1)+c-'0');
return f?-x:x;
}
int V,E,cnt,val[MM],x[MM],y[MM],rk[MM],l[MM],r[MM],v[MM],fa[MN],sz[MN],sum,ans=1,k;
inline bool cmp(int x,int y){return val[x]<val[y];}
inline int find(int x){return fa[x]==x?x:find(fa[x]);}
inline void swap(int &x,int &y){x^=y,y^=x,x^=y;}
inline void ins(int x,int y){
if (sz[x]<sz[y]) swap(x,y);
fa[y]=x;sz[x]+=sz[y];
}
inline void del(int x,int y){
if (fa[x]==y) swap(x,y);
fa[y]=y; sz[x]-=sz[y];
}
inline void dfs(int t,int no,int k){
if (no>r[t]){
sum+=(k==v[t]);
return;
}R int p=find(x[rk[no]]),q=find(y[rk[no]]);
if (p!=q){
ins(p,q);dfs(t,no+1,k+1);del(p,q);
}dfs(t,no+1,k);
}
int main(){
std::sort(rk+1,rk+E+1,cmp);for (R int i=1; i<=V; ++i) fa[i]=i;
for (R int i=1; i<=E; ++i){
if (val[rk[i]]!=val[rk[i-1]]) {r[cnt]=i-1; if (k==V-1) break;l[++cnt]=i;}
R int p=find(x[rk[i]]),q=find(y[rk[i]]);
if (p!=q){ins(p,q);++v[cnt],++k;}
}if (!r[cnt]) r[cnt]=E;if (k!=V-1){puts("0");return 0;}for (R int i=1; i<=V; ++i) fa[i]=i;
for (R int i=1; i<=cnt; ++i){
sum=0;dfs(i,l[i],0);ans=ans*sum%mod;
for (R int j=l[i]; j<=r[i]; ++j){
R int p=find(x[rk[j]]),q=find(y[rk[j]]);
if (p!=q) ins(p,q);
}
}printf("%d\n",ans);
return 0;
}
posted @ 2017-09-19 14:46  Melacau  阅读(...)  评论(... 编辑 收藏