跳到正文

题解

题解 P2143 【[JSOI2010]巨额奖金】

水库中的水库约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年7月29日
更新
2026年7月29日
篇幅
约 6 分钟
获赞
1
题目
P2143

版本

  1. v1当前

博客链接

定理

首先引入几个定理

  • 对于两个不同的最小生成树A,BA,B(这里的不同指严格意义下的边不同),当且仅当对于长度为ww的边,A,BA,B所拥有的个数相等
  • 只连接长度≤w\leq w的边,A,BA,B连通性相同

证明略

思路

根据这几个定理,可以得到一个简单的思路

  • 将边长wiw_i离散化,因为对于最小生成树来说只需考虑大小关系
  • 求出最小生成树AA,记录长度wiw_i的边使用的次数cnt[wi]cnt[w_i]
  • 考虑Kruscal算法的过程,我们每次加边来联通两个联通块,所以假设当前考虑到长度为ww的边,我们直接用暴搜考虑加边的方案数
  • 乘法原理计数
C++
// luogu-judger-enable-o2
/***************************************************************
    File name: P2143.cpp
    Author: ljfcnyali
    Create time: 2019年05月24日 星期五 08时38分49秒
***************************************************************/
#include<bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for ( int i = (a), _end_ = (b); i <= _end_; ++ i ) 
#define mem(a) memset ( (a), 0, sizeof ( a ) ) 
#define str(a) strlen ( a ) 

const int maxn = 2010;
const int INF = 0x3f3f3f3f;
const int Mod = 31011;

struct node
{
    int u, v, w;
} a[maxn];

int n, m, f[maxn], cnt[maxn], sum, ans, W;

inline int cha(int x)
{
    return f[x] == x ? x : cha(f[x]);
}

inline int cmp(node x, node y)
{
    return x.w < y.w;
}

inline void Kruscal()
{
    ans = sum = 0;
    REP(i, 1, m)
    {
        int fx = cha(a[i].u), fy = cha(a[i].v);
        if ( fx != fy ) 
        {
            f[fx] = fy; cnt[a[i].w] ++; ++ sum;
//			printf("%d %d %d\n", a[i].u, a[i].v, a[i].w);
        }
        if ( sum == n - 1 ) { ans = 1; return ; }
    }
}

inline int dfs(int x, int w, int s)
{
    if ( s == cnt[w] ) return 1;	
    if ( a[x].w != w ) return 0;
    int fx = cha(a[x].u), fy = cha(a[x].v), sum = 0;
    if ( fx != fy ) { f[fx] = fy; sum = dfs(x + 1, w, s + 1); f[fx] = fx; }
    return sum + dfs(x + 1, w, s);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
        mem(a); mem(cnt); W = 1; REP(i, 1, n) f[i] = i;
        REP(i, 1, m) scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
        sort ( a + 1, a + m + 1, cmp ); a[m + 1].w = INF;
        REP(i, 1, m)
        {
            if ( a[i].w != a[i + 1].w ) a[i].w = W ++;
            else a[i].w = W;
        }
        Kruscal();
        if ( ans == 0 ) { printf("0\n"); continue ; }
        REP(i, 1, n) f[i] = i;
        REP(i, 1, m)
        {
            if ( !cnt[a[i].w] || a[i - 1].w == a[i].w ) continue ;
            sum = dfs(i, a[i].w, 0);
            ans = (ans * sum) % Mod;
            for ( int j = i; j; ++ j )
            {
                if ( a[j].w != a[i].w ) break ;
                int fx = cha(a[j].u), fy = cha(a[j].v);
                if ( fx != fy ) f[fx] = fy;
            }
        }
        printf("%d\n", ans);
    return 0;
}
原文
存档评论1 条抓取于