結果

問題 No.2251 Marking Grid
ユーザー SSRSSSRS
提出日時 2023-03-17 21:51:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 3,000 ms
コード長 1,365 bytes
コンパイル時間 1,947 ms
コンパイル使用メモリ 184,884 KB
実行使用メモリ 20,132 KB
最終ジャッジ日時 2023-10-18 14:26:12
合計ジャッジ時間 7,650 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 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 1 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 340 ms
19,520 KB
testcase_23 AC 404 ms
20,132 KB
testcase_24 AC 16 ms
4,468 KB
testcase_25 AC 403 ms
20,124 KB
testcase_26 AC 91 ms
8,408 KB
testcase_27 AC 223 ms
18,348 KB
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 237 ms
18,492 KB
testcase_30 AC 76 ms
8,232 KB
testcase_31 AC 52 ms
5,560 KB
testcase_32 AC 27 ms
4,528 KB
testcase_33 AC 330 ms
18,676 KB
testcase_34 AC 36 ms
5,324 KB
testcase_35 AC 164 ms
12,248 KB
testcase_36 AC 206 ms
12,580 KB
testcase_37 AC 277 ms
18,264 KB
testcase_38 AC 17 ms
4,348 KB
testcase_39 AC 10 ms
4,348 KB
testcase_40 AC 148 ms
12,160 KB
testcase_41 AC 76 ms
8,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
struct unionfind{
  vector<int> p;
  unionfind(int N){
    p = vector<int>(N, -1);
  }
  int root(int x){
    if (p[x] < 0){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if (x != y){
      if (p[x] < p[y]){
        swap(x, y);
      }
      p[y] += p[x];
      p[x] = y;
    }
  }
};
int main(){
  int H, W;
  cin >> H >> W;
  vector<vector<int>> A(H, vector<int>(W));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> A[i][j];
    }
  }
  vector<tuple<int, int, int>> T;
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      T.push_back(make_tuple(A[i][j], i, j));
    }
  }
  sort(T.begin(), T.end(), greater<tuple<int, int, int>>());
  vector<long long> POW(H + W + 1);
  POW[0] = 1;
  for (int i = 0; i < H + W; i++){
    POW[i + 1] = POW[i] * 2 % MOD;
  }
  long long ans = 0;
  int cnt = H + W - 1;
  unionfind UF(H + W);
  for (int i = 0; i < H * W; i++){
    int x = get<1>(T[i]);
    int y = get<2>(T[i]);
    if (!UF.same(x, H + y)){
      ans += POW[cnt - 1] * get<0>(T[i]);
      ans %= MOD;
      UF.unite(x, H + y);
      cnt--;
    }
  }
  cout << ans << endl;
}
0