結果
問題 | No.2293 無向辺 2-SAT |
ユーザー |
![]() |
提出日時 | 2023-05-05 22:04:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 457 ms / 4,000 ms |
コード長 | 1,547 bytes |
コンパイル時間 | 1,697 ms |
コンパイル使用メモリ | 171,924 KB |
実行使用メモリ | 9,196 KB |
最終ジャッジ日時 | 2024-11-23 07:44:00 |
合計ジャッジ時間 | 31,100 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; const long long HALF = 499122177; struct unionfind{ vector<int> p; vector<int> id; 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){ id.push_back(x); id.push_back(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; } } void reset(){ for (int v : id){ p[v] = -1; } id.clear(); } }; int main(){ int N, Q; cin >> N >> Q; long long a = 1; for (int i = 0; i < N; i++){ a *= 2; a %= MOD; } unionfind UF(N * 2); long long ans = a; for (int i = 0; i < Q; i++){ int t; cin >> t; if (t == 1){ int u, v; cin >> u >> v; u--; v--; if (UF.same(u, N + v)){ ans = 0; } else if (!UF.same(u, v)){ ans *= HALF; ans %= MOD; UF.unite(u, v); UF.unite(N + u, N + v); } } if (t == 2){ int u, v; cin >> u >> v; u--; v--; if (UF.same(u, v)){ ans = 0; } else if (!UF.same(u, N + v)){ ans *= HALF; ans %= MOD; UF.unite(u, N + v); UF.unite(N + u, v); } } if (t == 3){ ans = a; UF.reset(); } cout << ans << endl; } }