bzoj1016[JSOI2008]最小產生樹計數

來源:互聯網
上載者:User

標籤:find   合數   排序   zoj   fat   const   使用   esc   code   

Description

  現在給出了一個簡單無向加權圖。你不滿足於求出這個圖的最小產生樹,而希望知道這個圖中有多少個不同的最小產生樹。(如果兩顆最小產生樹中至少有一條邊不同,則這兩個最小產生樹就是不同的)。由於不同的最小產生樹可能很多,所以你只需要輸出方案數對31011的模就可以了。

Input

  第一行包含兩個數,n和m,其中1<=n<=100; 1<=m<=1000; 表示該無向圖的節點數和邊數。每個節點用1~n的整數編號。接下來的m行,每行包含兩個整數:a, b, c,表示節點a, b之間的邊的權值為c,其中1<=c<=1,000,000,000。資料保證不會出現自回邊和重邊。注意:具有相同權值的邊不會超過10條。

Output

  輸出不同的最小產生樹有多少個。你只需要輸出數量對31011的模就可以了。

Sample Input4 6
1 2 1
1 3 1
1 4 1
2 3 2
2 4 1
3 4 1
Sample Output8   誒這題不大好理解。直接把題解複製上來啦: 

發現最小產生樹的形態是固定的。

即如果有一顆最小產生樹,則所有最小產生樹所使用的相同權值的邊的數量是一樣的,並且每次構出的點都是一樣的。

就是說每次都是相同的點被相同權值的邊給串連起來。這個簡單的證明就是如果有不同權值的邊連上的相同的點,那麼一定會有更優的產生樹。

 

於是知道了這兩個性質這題就很好搞了,排序之後先做一遍最小產生樹,得出了每種權值所用的邊的數量,然後打一個大暴力,枚舉相同權值的邊,然後並查集暴力判環,得出某種邊權值的邊的選擇方案,然後乘法原理統計答案,組合數效率,因為相同權值的邊最多有10條,所以最壞效率是20個C(10,5),發現效率是很OK的,如果不用並查集判環,複雜度就是滿的,判環相當於大量剪枝,於是發現實際上暴力根本達不到滿複雜度的效率,大量資料直接0.015s秒出。

 

P.S. 請注意判沒有產生樹的情況。

 

 1 program award(input,output); 2 const 3   cs=31011; 4 var 5   father:array[0..110]of longint; 6   a,b,w,l,r,num:array[0..1010]of longint; 7   n,m,i,j,cnt,x,y,ans:longint; 8 procedure sort(q,h:longint); 9 var10   i,j,x,t:longint;11 begin12    i:=q;j:=h;x:=w[(i+j)>>1];13   repeat14     while w[i]<x do inc(i);15     while x<w[j] do dec(j);16     if i<=j then17        begin18          t:=a[i];a[i]:=a[j];a[j]:=t;19          t:=b[i];b[i]:=b[j];b[j]:=t;20          t:=w[i];w[i]:=w[j];w[j]:=t;21          inc(i);dec(j);22        end;23    until i>j;24    if j>q then sort(q,j);25    if i<h then sort(i,h);26 end;27 function find(k:longint):longint;28 begin29    if father[k]=k then exit(k) else exit(find(father[k]));30 end;31 procedure dfs(k,s:longint);32 var33   x,y:longint;34 begin35    if k=r[i]+1 then begin if s=num[i] then inc(j);exit; end;36    x:=find(a[k]);y:=find(b[k]);37    if x<>y then38       begin39          father[x]:=y;40          dfs(k+1,s+1);41          father[x]:=x;42       end;43    dfs(k+1,s);44 end;45 begin46    assign(input,‘award.in‘);assign(output,‘award.out‘);reset(input);rewrite(output);47    readln(n,m);48    for i:=1 to m do readln(a[i],b[i],w[i]);49    sort(1,m);50    for i:=1 to n do father[i]:=i;51    w[0]:=0;j:=0;cnt:=0;52    fillchar(num,sizeof(num),0);53    for i:=1 to m do54       begin55          if w[i]<>w[i-1] then begin r[cnt]:=i-1;inc(cnt);l[cnt]:=i; end;56          x:=find(a[i]);y:=find(b[i]);57          if x<>y then begin father[x]:=y;inc(num[cnt]);inc(j); end;58       end;59    r[cnt]:=m;60    if j<n-1 then begin write(0);close(input);close(output);halt; end;61    ans:=1;62    for i:=1 to n do father[i]:=i;63    for i:=1 to cnt do64       begin65          j:=0;66          dfs(l[i],0);67          ans:=ans*j mod cs;68          for j:=l[i] to r[i] do69             begin70                x:=find(a[j]);y:=find(b[j]);71                if x<>y then father[x]:=y;72             end;73       end;74    write(ans);75    close(input);close(output);76 end.

 

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.