【BZOJ】1016: [JSOI2008]最小產生樹計數(kruskal+特殊的技巧)

來源:互聯網
上載者:User

標籤:des   blog   http   io   ar   os   sp   for   資料   

http://www.lydsy.com/JudgeOnline/problem.php?id=1016

想也想不到QAQ

首先想不到的是:題目有說,具有相同權值的邊不會超過10條。

其次:老是去想組合計數怎麼搞。。。。。。。於是最sb的暴力都不會了。。

所以這題暴力搞就行了orz

依次加邊,每一種邊的方案數乘起來就是方案了。

注意並查集不能路徑壓縮,否則在計數的時候會waQAQ因為並查集的路徑壓縮是無法復原的QAQ

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=105, M=1005, MD=31011;int n, m, cnt, p[N], ans=1, sum;struct dat { int x, y, w; }e[M], a[M];inline const bool cmp(const dat &a, const dat &b) { return a.w<b.w; }inline const int ifind(const int &x) { return x==p[x]?x:ifind(p[x]); }void dfs(int now, int s, const int &x) {if(now>a[x].y) {if(s==a[x].w) ++sum;return;}dfs(now+1, s, x);int fx=ifind(e[now].x), fy=ifind(e[now].y);if(fx!=fy) { p[fx]=fy; dfs(now+1, s+1, x); p[fx]=fx; p[fy]=fy; }}int main() {read(n); read(m);for1(i, 1, m) read(e[i].x), read(e[i].y), read(e[i].w);for1(i, 1, n) p[i]=i;sort(e+1, e+1+m, cmp);int ed=0;for1(i, 1, m) {if(e[i].w!=e[i-1].w) a[++cnt].x=i, a[cnt-1].y=i-1;int fx=ifind(e[i].x), fy=ifind(e[i].y);if(fx!=fy) {p[fx]=fy;++a[cnt].w;++ed;}}if(ed!=n-1) { puts("0"); return 0; }a[cnt].y=m;for1(i, 1, n) p[i]=i;for1(i, 1, cnt) {sum=0;dfs(a[i].x, 0, i);ans=(ans*sum)%MD;for1(j, a[i].x, a[i].y) {int fx=ifind(e[j].x), fy=ifind(e[j].y);if(fx!=fy) p[fx]=fy;}}print(ans);return 0;}

  

 

 

 

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 1Sample Output8HINT Source

 

 

【BZOJ】1016: [JSOI2008]最小產生樹計數(kruskal+特殊的技巧)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.