結果

問題 No.2291 Union Find Estimate
ユーザー tnakao0123tnakao0123
提出日時 2023-06-03 23:43:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,083 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 54,656 KB
実行使用メモリ 5,748 KB
最終ジャッジ日時 2023-08-28 08:04:37
合計ジャッジ時間 2,066 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 28 ms
4,380 KB
testcase_03 AC 7 ms
5,748 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2291.cc:  No.2291 Union Find Estimate - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_W = 200000;
const int MOD = 998244353;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

struct UFT {
  vector<int> links, ranks, sizes;
  UFT() {}

  void init(int n) {
    links.resize(n);
    for (int i = 0; i < n; i++) links[i] = i;
    ranks.assign(n, 1);
    sizes.assign(n, 1);
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

char s[MAX_W + 4];
UFT uft;
int ds[MAX_W], es[26];

/* subroutines */

/* main */

int main() {
  int w, h;
  scanf("%d%d", &w, &h);

  mi v = mi(10).pow(w), inv10 = mi(1) / 10;
  uft.init(w);
  fill(ds, ds + w, -1);

  while (h--) {
    scanf("%s", s);
    fill(es, es + 26, -1);

    for (int i = 0; i < w; i++) {
      int ri = uft.root(i);
      if (s[i] >= '0' && s[i] <= '9') {
	int si = s[i] - '0';
	if (ds[ri] < 0)
	  ds[ri] = si, v *= inv10;
	else if (ds[ri] != si)
	  v = 0;
      }
      else if (s[i] >= 'a' && s[i] <= 'z') {
	int si = s[i] - 'a';
	if (es[si] < 0)
	  es[si] = ri;
	else if (es[si] != ri) {
	  int d0 = ds[es[si]], d1 = ds[ri];
	  es[si] = uft.merge(es[si], ri);
	  if (d0 < 0 && d1 < 0)
	    v *= inv10;
	  else if (d0 < 0)
	    ds[es[si]] = d1, v *= inv10;
	  else if (d1 < 0)
	    ds[es[si]] = d0, v *= inv10;
	  else if (d0 != d1)
	    v = 0;
	}
      }
    }

    printf("%d\n", v.v);
  }
  return 0;
}
0