bzoj 1016 JSOI2008 最小產生樹計數

來源:互聯網
上載者:User

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

    我都不忍心吐槽了。

    這麼水的暴力我一開始竟然想寫鏈剖!!!

    對於某個權值,相同的邊不會超過10條。於是,暴力,然後乘起來。

    注意特判!特判!圖不連通的時候輸出0。

    我的程式在不聯通的時候會輸出奇怪的數字……要崩潰了……

    上代碼:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#define N 111#define M 1011#define yu 31011#define inf 0x7f7f7f7fusing namespace std;struct sss{    int x, y;    int val;}bian[M];int n, m;int fa[N];int use[M] = {0}, same[15], samenum = 0;long long ans = 1, minval;bool cmp(sss x, sss y) { return x.val < y.val; }int find(int x){    if (fa[x] == x) return x;    fa[x] = find(fa[x]);    return fa[x];}long long kru(){    long long nowval = 0; int nowc = n;    for (int i = 1; i <= n; ++i) fa[i] = i;    for (int i = 1; i <= m; ++i)    {        int x = find(bian[i].x);        int y = find(bian[i].y);        if (use[i] == 1 && x == y) return 0; // use[i] == 1 用 2 不用         if (use[i] == 2) continue;        if (x != y)        {            fa[x] = y;            nowval += bian[i].val;            nowc --;        }    }    if (nowc != 1) return 0;    else return nowval;}int main(){    scanf("%d%d", &n, &m);    for (int i = 1; i <= m; ++i)        scanf("%d%d%d", &bian[i].x, &bian[i].y, &bian[i].val);    sort(bian+1, bian+1+m, cmp);    bian[0].val = -inf; bian[m+1].val = -inf;    minval = kru();    if (minval == 0)    {        printf("0\n");        return 0;    }    for (int w = 1; w <= m; ++w)    {        samenum = 0;        if (bian[w].val != bian[w+1].val) continue;        same[++samenum] = w++;        while (bian[w].val == bian[w-1].val) {same[++samenum] = w++;}        w--;        int zans = 0;        int cou = (1 << samenum)-1;        for (int i = 0; i <= cou; ++i)        {            int j = i, thisb = 1;            for (int k = 1; k  <= samenum; ++k) use[same[k]] = 2;            while (j){if (j & 1) use[same[thisb]] = 1;j >>= 1; thisb++;}            if (kru() == minval) zans++;        }        for (int i = 1; i <= samenum; ++i) use[same[i]] = 0;        ans *= (zans%yu);        ans %= yu;    }    printf("%lld\n", ans%yu);    return 0;}

 

bzoj 1016 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.