標籤: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+特殊的技巧)