zhber 有好多做过的题没写下来，如果我还能记得就补吧

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

## Sample Output

8

#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;
{
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()
{
for (int i=1;i<=m;i++)
{
}
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\n",ans);
}


posted on 2015-01-10 22:07  zhber  阅读(103)  评论(0编辑  收藏