zoukankan      html  css  js  c++  java
  • UOJ 176 新年的繁荣

    挺妙的解法。

    发现边权很小,我们可以考虑从大到小枚举边权来进行$kruskal$算法,这样子对于每一个边权$i$,我们只要枚举$0 leq j < m$,找到一个点使它的点权为$i | 2^j$,尝试连边即可。

    另外,如果同一个点权重复出现,一定有办法使这个边权连满,这样子直接累加到答案里就可以了。

    时间复杂度$O(m * 2^m)$,再套一个并查集的复杂度。

    Code:

    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    
    const int N = 18;
    
    int n, m, a[1 << N], ufs[1 << N];
    ll ans = 0LL;
    
    inline void read(int &X) {
        X = 0; char ch = 0; int op = 1;
        for(; ch > '9' || ch < '0'; ch = getchar())
            if(ch == '-') op = -1;
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            X = (X << 3) + (X << 1) + ch - 48;
        X *= op; 
    }
    
    inline int find(int x) {
        return ufs[x] == x ? x : ufs[x] = find(ufs[x]);
    }
    
    inline bool merge(int x, int y) {
        int fx = find(x), fy = find(y);
        if(fx == fy) return 0;
        ufs[fx] = fy;
        return 1;
    }
    
    int main() {
    //    freopen("Sample.txt", "r", stdin);
        
        read(n), read(m);
        for(int x, i = 1; i <= n; i++) {
            read(x);
            if(a[x]) ans += 1LL * x;
            else a[x] = x;
        }
        
        for(int i = 1; i < (1 << m); i++) ufs[i] = i;
        for(int i = (1 << m) - 1; i >= 0; i--) {
            for(int j = 0; j < m && (!a[i]); j++)
                a[i] = a[i | (1 << j)];
            for(int j = 0; j < m; j++)
                if(a[i | (1 << j)] && merge(a[i], a[i | (1 << j)]))
                    ans += 1LL * i;
        }
        
        printf("%lld
    ", ans);
        return 0;
    }
    View Code
  • 相关阅读:
    ACdream 1069 无耻的出题人
    ACdream 1064 完美数
    ACdream 1028 Path
    ACdream 1020 The Game about KILL
    ACdream 1015 Double Kings
    CodeForces
    Codeforces 390A( 模拟题)
    Codeforces 389B(十字模拟)
    Codeforces 389A (最大公约数)
    Codeforces 417 C
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9836606.html
Copyright © 2011-2022 走看看