結果

問題 No.2251 Marking Grid
ユーザー みここみここ
提出日時 2023-03-17 22:32:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 3,000 ms
コード長 4,521 bytes
コンパイル時間 1,027 ms
コンパイル使用メモリ 84,020 KB
実行使用メモリ 11,464 KB
最終ジャッジ日時 2023-10-18 15:28:36
合計ジャッジ時間 6,177 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 324 ms
10,460 KB
testcase_23 AC 384 ms
11,464 KB
testcase_24 AC 15 ms
4,348 KB
testcase_25 AC 384 ms
11,448 KB
testcase_26 AC 88 ms
6,360 KB
testcase_27 AC 210 ms
8,408 KB
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 224 ms
8,408 KB
testcase_30 AC 73 ms
6,356 KB
testcase_31 AC 51 ms
6,356 KB
testcase_32 AC 27 ms
4,348 KB
testcase_33 AC 324 ms
10,460 KB
testcase_34 AC 35 ms
4,348 KB
testcase_35 AC 159 ms
6,360 KB
testcase_36 AC 204 ms
8,408 KB
testcase_37 AC 270 ms
8,408 KB
testcase_38 AC 16 ms
4,348 KB
testcase_39 AC 10 ms
4,348 KB
testcase_40 AC 144 ms
6,360 KB
testcase_41 AC 73 ms
6,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;

struct UnionFind
{
public:
    UnionFind(int n) : n(n), par(n), sz(n, 1), num_of_components(n)
    {
        for (int i = 0; i < n; i++)
        {
            par[i] = i;
        }
    }

    int find(int i)
    {
        assert(0 <= i && i < n);
        return (i == par[i]) ? i : par[i] = find(par[i]);
    }

    bool same(int i, int j)
    {
        assert(0 <= i && i < n);
        assert(0 <= j && j < n);
        return find(i) == find(j);
    }

    void unite(int i, int j)
    {
        assert(0 <= i && i < n);
        assert(0 <= j && j < n);
        i = find(i), j = find(j);
        if (i == j)
        {
            return;
        }
        if (sz[i] < sz[j])
        {
            std::swap(i, j);
        }
        par[j] = i;
        sz[i] += sz[j];
        num_of_components--;
    }

    int size()
    {
        return n;
    }

    int size(int i)
    {
        assert(0 <= i && i < n);
        return sz[find(i)];
    }

    int count()
    {
        return num_of_components;
    }

    bool is_root(int i)
    {
        assert(0 <= i && i < n);
        return i == find(i);
    }

    int operator[](int i)
    {
        return find(i);
    }

private:
    int n;
    std::vector<int> par;
    std::vector<int> sz;
    int num_of_components;
};

template <int MOD>
struct StaticModint
{
    using mint = StaticModint;

public:
    int val;

    StaticModint() : val(0) {}

    StaticModint(long long v)
    {
        if (std::abs(v) >= mod())
        {
            v %= mod();
        }
        if (v < 0)
        {
            v += mod();
        }
        val = v;
    }

    mint &operator++()
    {
        val++;
        if (val == mod())
        {
            val = 0;
        }
        return *this;
    }

    mint &operator--()
    {
        if (val == 0)
        {
            val = mod();
        }
        val--;
        return *this;
    }

    mint &operator+=(const mint &x)
    {
        val += x.val;
        if (val >= mod())
        {
            val -= mod();
        }
        return *this;
    }

    mint &operator-=(const mint &x)
    {
        val -= x.val;
        if (val < 0)
        {
            val += mod();
        }
        return *this;
    }

    mint &operator*=(const mint &x)
    {
        val = (int)((long long)val * x.val % mod());
        return *this;
    }

    mint &operator/=(const mint &x)
    {
        *this *= x.inv();
        return *this;
    }

    mint operator-()
    {
        return mint() - *this;
    }

    mint pow(long long n) const
    {
        mint x = 1, r = *this;
        while (n)
        {
            if (n & 1)
            {
                x *= r;
            }
            r *= r;
            n >>= 1;
        }
        return x;
    }

    mint inv() const
    {
        return pow(mod() - 2);
    }

    friend mint operator+(const mint &x, const mint &y)
    {
        return mint(x) += y;
    }

    friend mint operator-(const mint &x, const mint &y)
    {
        return mint(x) -= y;
    }

    friend mint operator*(const mint &x, const mint &y)
    {
        return mint(x) *= y;
    }

    friend mint operator/(const mint &x, const mint &y)
    {
        return mint(x) /= y;
    }

    friend bool operator==(const mint &x, const mint &y)
    {
        return x.val == y.val;
    }

    friend bool operator!=(const mint &x, const mint &y)
    {
        return x.val != y.val;
    }

    friend std::ostream &operator<<(std::ostream &os, const mint &x)
    {
        return os << x.val;
    }

    friend std::istream &operator>>(std::istream &is, mint &x)
    {
        long long v;
        is >> v;
        x = mint(v);
        return is;
    }

private:
    static constexpr int mod()
    {
        return MOD;
    }
};

using mint = StaticModint<998244353>;

using P = pair<int, int>;

P p[1000005];

int main()
{
    int h, w;
    cin >> h >> w;
    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w; j++)
        {
            int a;
            cin >> a;
            p[i * w + j] = P(a, i * w + j);
        }
    }
    sort(p, p + h * w, greater<P>());
    mint ans = 0;
    UnionFind uf(h + w);
    for(int i = 0; i < h * w; i++)
    {
        mint a = p[i].first;
        int x = p[i].second / w, y = p[i].second % w;
        if(!uf.same(x, y + h))
        {
            uf.unite(x, y + h);
            ans += a * mint(2).pow(uf.count() - 1);
        }
    }
    cout << ans << endl;
}
0