結果

問題 No.2291 Union Find Estimate
ユーザー Carpenters-CatCarpenters-Cat
提出日時 2023-05-05 22:12:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,830 bytes
コンパイル時間 3,191 ms
コンパイル使用メモリ 220,900 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-05-02 16:35:33
合計ジャッジ時間 6,836 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = std::vector<int> ;
using Vl = vector<ll>;
using VVi = vector<Vi>;

ll m = 998244353;

class UnionFind {
	Vi par;
	Vi siz;
	Vl val;
	// VVi iv;
	
	public :
	ll ans;
	UnionFind(int N = 0) {
		par.resize(N + 10);
		siz.resize(N + 10);
		val.assign(N + 10, -1);
		iota(par.begin(), par.end(), 0);
		fill(siz.begin(), siz.end(), 0);
		for (int i = 0; i < N; i ++) {
			ans *= 10;
			ans %= m;
		}
		for (int i = N; i < N + 10; i ++) {
			val[i] = i - N;
		}
	}
	void init(int N) {
		par.resize(N);
		siz.resize(N);
		iota(par.begin(), par.end(), 0);
		fill(siz.begin(), siz.end(), 0);
	}
	int root(int v) {
		if (v == par[v]) {
			return v;
		} else {
			int p = root(par[v]);
			if (val[p] != val[v] && val[v] != -1 && val[p] != -1) {
				ans = 0;
			} else if (val[v] != -1) {
				val[p] = val[v];
			} else {
				val[v] = val[p];
			}
			return par[v] = root(par[v]);
		}
	}
	bool unit(int a, int b) {
		a = root(a);
		b = root(b);
		if (a == b) {
			return false;
		}
		if (siz[a] < siz[b]) {
			swap(a, b);
		}
		par[b] = a;
		siz[a] += siz[b];
		if (val[a] != val[b] && val[a] != -1 && val[b] != -1) {
			ans = 0;
		} else if ((val[a] != -1) != (val[b] != -1)) {
			int p = (val[a] == -1 ? b : a);
			int q = a ^ b ^ p;
			val[q] = val[p];
			ans *= ((m * 3 + 1) / 10);
			ans %= m;
		}
		return true;
	}
};
int main () {
	int Q, N;
	cin >> Q >> N;
	UnionFind uu(N);
	while (Q--) {
		string s;
		cin >> s;
		Vi A[26];
		for (int i = 0; i < N; i ++) {
			char c = s[i];
			if ('0' <= c && c <= '9') {
				uu.unit(i, c - '0' + N);
			} else if (c != '?') {
				A[c - 'a'].push_back(i);
			}
		}
		for (auto a : A) {
			if (a.size() > 1) {
				for (int i = 0; i < a.size(); i ++) {
					uu.unit(a[i], a[0]);
				}
			}
		}
		cout << uu.ans << endl;
	}
}
0