bzoj1016

来源:互联网 发布:过山车大亨mac版 编辑:程序博客网 时间:2024/05/16 06:11

从下午4点写到了8点多。。。。

就是因为把i打成了1.。。。。。。

现在已濒临崩溃的边缘。。。。

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define MAX 10010#define mod 31011#define f fatherusing namespace std;int n,m,father[MAX],cost,power[20];int use[MAX]={0};int cur;struct wbysr{int from,to,value;}a[MAX];int get_father(int x){if(father[x]==x)return x;father[x]=get_father(father[x]);return father[x];}bool cmp(wbysr a1,wbysr a2){return a1.value<a2.value;}void set(){int now=1;for(int i=1;i<=m;i++)if(a[i].value!=a[i+1].value)a[i].value=now++;elsea[i].value=now;}int calc(int x){int answer=0;while(x){answer+=x&1;x/=2;}//printf("calc %d %d\n",x,answer);return answer;}bool Ccheck(int x,int y){printf("%d %d  ",x,y);int cnt;cnt=n;for(int i=1;i<=n;i++)father[i]=i;for(int i=x;a[i].value==a[x].value;i++){if(y&1){father[a[i].from]=get_father(a[i].from);father[a[i].to]=get_father(a[i].to);if(father[a[i].from]!=father[a[i].to]){father[father[a[i].to]]=father[a[i].from];cnt--;}}y/=2;}int now_cost=use[a[x].value]*a[x].value;for(int i=1;i<=m&&cnt>1;i++)if(a[i].value!=a[x].value){father[a[i].from]=get_father(a[i].from);father[a[i].to]=get_father(a[i].to);if(father[a[i].from]==father[a[i].to])continue;father[father[a[i].to]]=father[a[i].from];cnt--;now_cost+=a[i].value;if(cnt==1)break;}printf("%d check\n",(cnt==1&&now_cost==cost));return (cnt==1&&now_cost==cost);}#define edge a#define find get_father#define w value#define b tobool check(int x,int sit){//printf("%d %d ",x,sit);int res;int cnt;    cnt=n;res=edge[x].w*use[edge[x].w];    for (int i=1;i<=n;i++)   f[i]=i;    for (int i=x;edge[i].w==edge[x].w;i++){        if  (sit&1){            f[edge[i].from]=find(edge[i].from);            f[edge[i].b]=find(edge[i].b);            if  (f[edge[i].from]!=f[edge[i].b]){                f[f[edge[i].b]]=f[edge[i].from];                --cnt;            }        }        sit/=2;    }    for (int i=1;i<=m && cnt>1;i++){        if  (edge[i].w==edge[x].w)  continue;        f[edge[i].from]=find(edge[i].from);        f[edge[i].b]=find(edge[i].b);        if  (f[edge[i].from]==f[edge[i].b])    continue;        f[f[edge[i].b]]=f[edge[i].from];        res+=edge[i].w;        if  (--cnt==1)  break;    }int minnum=cost;//printf("%d \n",cur==1&&res==minnum);    return  (cnt==1 && res==minnum);}int main(){scanf("%d%d",&n,&m);for(int i=1;i<=m;i++)scanf("%d%d%d",&a[i].from,&a[i].to,&a[i].value);sort(a+1,a+1+m,cmp);set();int cnt=n;cost=0;for(int i=1;i<=n;i++)father[i]=i;for(int i=1;i<=m&&cnt>1;i++){//printf("%d %d\n",i,cost);father[a[i].from]=get_father(a[i].from);father[a[i].to]=get_father(a[i].to);//printf("get %d %d\n",father[a[i].from],father[a[i].to]);if(father[a[i].from]==father[a[i].to])continue;father[father[a[i].to]]=father[a[i].from];use[a[i].value]++;cost+=a[i].value;if(--cnt==1)break;}if(cnt!=1){printf("0\n");return 0;}power[0]=1;for(int i=1;i<=10;i++)power[i]=power[i-1]*2;int ans=1;for(int i=1;i<=m;){int j=i+1;while(a[j].value==a[i].value)j++;if(use[a[i].value]){cur=0;for(int k=0;k<power[j-i];k++)if((calc(k)==use[a[i].value])&&check(i,k))cur++;ans=ans*cur%mod;//printf("%d %d %d\n",i,j,cur);}i=j;}printf("%d\n",ans);}

0 0
原创粉丝点击