最小生成樹計數 lydsy 1016

題目鏈接

證明:(摘錄)


定理一: 如果 A,B 同爲 G 的最小生成樹,且 A 的邊權從小到大爲 w(a1),w(a2),w(a3),w(an)B 的邊權從小到大爲 w(b1),w(b2),w(b3),w(bn) ,則有 w(ai)=w(bi)
證明:設 A,B 第一個不同的邊的下標爲 i ,不妨設 w(ai)w(bi) ,如果不存在這樣的 i ,無需證明。
情況一:aiB 中,爲 bj 。那麼顯然有 j>i (否則 i 不是第一個不同的邊),則有 w(ai)w(bi)w(bj)=w(ai) ,所以有 w(ai)=w(bi)=w(bj) ,所以可以調換 bi,bj 的位置,B 的權值排列不會改變, AB 這樣前 i 條邊均爲相等,可以遞歸下去證明。
情況二:ai 不在 B 中。考慮將 ai 加入 B ,則形成了一個環,環中的權值 vw(ai) (否則 B 不是最小生成樹),且一定有一條邊 bj 不在 B 中,此時仍有 j>i (否則 i 不是第一個不同的邊),所以有 w(ai)w(bi)w(bj)w(ai) ,所以仍有 w(ai)=w(bi)=w(bj) ,可以將 bj 替換爲 aiB 的權值排列不會改變且仍爲最小生成樹,仍然調換 bi,bj 的位置,B 的權值排列仍會改變,這樣 ABi 條邊均爲相等,可以遞歸下去證明。這樣換邊不會有問題,因爲 Kruskal 算法中唯一的狀態就是連通性,我們沒有改變其連通性,所以是可以遞歸證明的。

定理二:如果 A,B 同爲 G 的最小生成樹,如果 A,B 都從零開始從小到大加邊(AA 的邊,BB 的邊)的話,每種權值加完後圖的聯通性相同。
證明:歸納法證明,沒有邊時顯然成立,假設對於權值小於 v 的成立。考慮權值爲 v 的邊,如果連通性不相同,必然存在 u,v 兩點間連通性不同,假設 Au,v 聯通,根據 A,B 所有小於 v 的邊連通性相同,所以必然存在一條 uv 權值爲 v 的邊,而根據 Kruskal 算法的執行過程, B 不可能不加這條邊,所以兩棵最小生成樹 A,B 各自權值小於等於 v 的邊仍然滿足連通性相同。由歸納法可知定理二成立。

定理三:如果在最小生成樹 A 中權值爲 v 的邊有 k 條,用任意 k 條權值爲 v 的邊替換 A 中的權爲 v 的邊且不產生環的方案都是一棵合法最小生成樹。
證明:根據之前的定理,其餘的邊造成的連通性是定的,權值和也是定的,那麼選 k 條不產生環一定能形成一棵樹,而且權值與最小生成樹的權值一樣,故也是最小生成樹。

那麼算法十分明確,隨便找一個最小生成樹(圖不聯通直接輸出 0),記錄下每種權值的出現次數,然後對於每種權值的邊,分別求出方案(按照定理三的方法選),利用乘法原理即可解決。
複雜度:O(210n2α(n))


代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int ReadInt()
{
    static int n, ch;
    n = 0, ch = getchar();
    while (!isdigit(ch))
        ch = getchar();
    while (isdigit(ch)) n = (n<<3)+(n<<1)+ch-'0', ch = getchar();
    return n;
}

const int maxn = 100+10, maxm = 1000+3, mod = 31011;
struct edge
{
    int from, to, cost;
    inline bool operator < (const edge &ano)const
    {
        return cost < ano.cost;
    }
}edges[maxm];

int n, m;
namespace union_find_set
{
    int fa[maxn];
    inline int init(int n)
    {
        for (int i=0; i<n; i++)
            fa[i] = i;
    }
    inline int find(int x)
    {
        return  fa[x] == x?fa[x]:fa[x] = find(fa[x]);
    }
    inline void unite(int u, int v)
    {
        u = find(u), v = find(v);
        fa[u] = v;
    }
    inline bool same(int x, int y)
    {
        return find(x) == find(y);
    }

}

using namespace union_find_set;

vector<int>num, cost;
bool Kruskal()
{
    init(n);
    sort(edges, edges+m);
    int last = -1, cnt = 0;
    for (int i=0; i<m; i++)
    {
        const edge &e = edges[i];
        if (!same(e.to, e.from))
        {
            cnt++;
            unite(e.to, e.from);
            if (e.cost != last)
            {
                cost.push_back(e.cost);
                num.push_back(1);
                last = edges[i].cost;
            }
            else
                num[num.size()-1]++;
        }
    }
    return cnt == n-1;
}

int main()
{
    n = ReadInt(), m = ReadInt();
    init(n);
    for (int i=0; i<m; i++)
        edges[i].from = ReadInt()-1, edges[i].to = ReadInt()-1, edges[i].cost = ReadInt();
    if (!Kruskal())
        return puts("0"), 0;
    int ans = 1, r = 0;
    init(n);
    for (int i=0, j=0; i<m; i=j)
    {
        int now_ans = 0;
        j = i+1;
        while (j < m && edges[j].cost == edges[i].cost)
            ++j;

        if (edges[i].cost != cost[r])
            continue;
        static int cp[maxn], ok[maxn];
        memcpy(cp, fa, sizeof(int)*n);
        for (int s=(1<<(j-i))-1; s; --s)
        {
            if (__builtin_popcount(s) == num[r])
            {
                memcpy(fa, cp, sizeof(int)*n);
                bool flag = true;
                for (int k=i; k<j; ++k)
                {
                    if ((s>>(k-i)) & 1)
                    {
                        if (same(edges[k].from, edges[k].to))
                        {
                            flag = false;
                            break;
                        }
                        unite(edges[k].from, edges[k].to);
                    }
                }
                if (flag)
                {
                    now_ans++;
                    if (now_ans == 1)
                        memcpy(ok, fa, sizeof(int)*n);
                }
            }
        }
        memcpy(fa, ok, sizeof(int)*n);
        ans = ans*now_ans%mod;
        r++;
    }
    printf("%d\n", ans);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章