【kruscal】【最小產生樹】【搜尋】bzoj1016 [JSOI2008]最小產生樹計數

來源:互聯網
上載者:User

標籤:style   blog   color   io   for   sp   div   on   log   

不用Matrix-tree定理什麼的,一邊kruscal一邊 對權值相同的邊 暴搜即可。將所有方案乘起來。

 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int n,m; 5 struct Disjoint_Set 6 { 7     int fa[101],rank[101]; 8     void init(){for(int i=1;i<=n;i++) fa[i]=i;} 9     int findroot(int x) 10       {11         if(fa[x]==x) return x;12         int rt=findroot(fa[x]);13         fa[x]=rt;14         return rt;15       }16     void Union(int U,int V)17       {18         if(rank[U]<rank[V]) fa[U]=V;19         else20           {21             fa[V]=U;22             if(rank[U]==rank[V]) rank[U]++;23           }24       }25 };26 Disjoint_Set S,used;27 struct Edge{int u,v,w;};28 bool cmp(const Edge &a,const Edge &b){return a.w<b.w;}29 Edge edges[1001];30 int res,ans=1,tot,cnt,sta,end;31 void dfs(int cur,int sum,Disjoint_Set now)32 {33     if(cur>end)34       {35           if(sum==cnt) res++;36           return;37       }38     dfs(cur+1,sum,now);39     int f1=now.findroot(edges[cur].u),f2=now.findroot(edges[cur].v);40     if(f1!=f2) {now.Union(f1,f2); dfs(cur+1,sum+1,now);}41 }42 int main()43 {44     scanf("%d%d",&n,&m);45     for(int i=1;i<=m;i++) scanf("%d%d%d",&edges[i].u,&edges[i].v,&edges[i].w);46     sort(edges+1,edges+m+1,cmp);47     S.init();used.init();48     for(int i=1;i<=m;i++)49       {50           if(edges[i].w!=edges[i-1].w) {used=S; cnt=0; sta=i;}51           int f1=S.findroot(edges[i].u),f2=S.findroot(edges[i].v);52           if(f1!=f2) {S.Union(f1,f2); tot++; cnt++;}53           if(edges[i].w!=edges[i+1].w)54             {55                 res=0; end=i;56                 dfs(sta,0,used);57                 ans=((ans%31011)*(res%31011))%31011;58             }59           else if(tot==n-1)60             {61                 res=0;62                 for(int j=i+1;j<=m;j++)63                   if(edges[j].w!=edges[i].w)64                     {65                       end=j-1;66                       goto OUT;67                     }68                 end=m;69                 OUT:dfs(sta,0,used);70                 ans=((ans%31011)*(res%31011))%31011;71                 break;72             }73       }74     printf("%d\n",tot==n-1 ? ans : 0);75     return 0;76 }

 

【kruscal】【最小產生樹】【搜尋】bzoj1016 [JSOI2008]最小產生樹計數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.