Bzoj 1016 Jsoi 2008 minimum Spanning tree count kruskal+ search

Source: Internet
Author: User

Topic: Give some sides, find out how many of the minimum spanning tree can be formed.


Idea: The minimum spanning tree has very many theorems ah, I am not very clear. Here is just a simple way to talk about practice. For the various theorems, see here: http://blog.csdn.net/wyfcyx_forever/article/details/40182739

Let's do the minimum spanning tree first. Then record how many edges of each length are in the smallest spanning tree, and then search from small to large to see how many different ways to put each edge right. And then all of them are counted out, and the result is finally.


CODE:

#include <map> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm > #define MAX 2010#define MO 31011using namespace std;map<int,int> g;struct complex{int x,y,len;bool operator &lt ;(Const Complex &a) Const {return Len < A.len;} void Read () {scanf ("%d%d%d", &x,&y,&len);}} Edge[max];int Points,edges;int ones[max];int father[max];int ans = 1;void pretreatment () {for (int i = 1;i <= 1025; ++i) o Nes[i] = ones[i >> 1] + (i&1);} int Find (int x) {if (!father[x] | | father[x] = = x) return father[x] = X;return Father[x] = find (Father[x]);} BOOL Kruskal () {int cnt = 0;for (int i = 1;i <= edges; ++i) {int fx = find (edge[i].x); int fy = find (EDGE[I].Y); if (FX! = f Y) {father[fy] = FX; g[edge[i].len]++;cnt++;}} if (CNT < points-1) return False;return true;} void DFS (int pos) {if (pos > Edges) return; int st = pos,ed = Pos,re = 0;int cnt = g[edge[st].len];if (!cnt) {DFS (ed + 1); r Eturn;} while (edge[ed + 1].len = = Edge[st].len) ++ed;for (int i = 0;i < (1 << (ed-st + 1)), ++i) if (ones[i] = = cnt) {memset (father,0,sizeof (father)); int temp = I;for ( int j = St; Temp Temp >>= 1,++j) if (temp&1) {int fx = find (edge[j].x); int fy = find (EDGE[J].Y); if (fx = = FY) break;father[fy] = FX;} if (!temp) ++re;} Ans = (ans * Re)% Mo;memset (father,0,sizeof (father)); for (int i = st; I <= ed; ++i) {int fx = Find (edge[i].x); int fy = Find (EDGE[I].Y); if (fx = = FY) continue;father[fy] = FX;} for (int i = ed + 1;i <= edges; ++i) edge[i].x = Find (edge[i].x), edge[i].y = Find (EDGE[I].Y);D FS (ed + 1);} int main () {pretreatment (); Cin >> points >> edges;for (int i = 1;i <= edges; ++i) edge[i]. Read (); Sort (edge + 1,edge + edges + 1); Memset (father,0,sizeof (father)); Kruskal ()) {cout << 0 << endl;return 0;} DFS (1); cout << ans << endl;return 0;}


Bzoj 1016 Jsoi 2008 minimum Spanning tree count kruskal+ search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.