結果
問題 | No.2293 無向辺 2-SAT |
ユーザー |
👑 ![]() |
提出日時 | 2023-05-05 23:03:01 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 216 ms / 4,000 ms |
コード長 | 2,165 bytes |
コンパイル時間 | 987 ms |
コンパイル使用メモリ | 80,536 KB |
最終ジャッジ日時 | 2025-02-12 19:22:39 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
#include <vector> #include <algorithm> namespace nachia { struct Dsu{ private: int N; std::vector<int> P; std::vector<int> lab; public: Dsu() : N(0) {} Dsu(int n) : N(n), P(n, -1) { } int leader(int u){ while(P[u] >= 0) u = P[u]; return u; } void merge(int u, int v, int newLabel){ if(newLabel < 0) newLabel = u; u = leader(u); v = leader(v); if(u == v){ return; } N--; if(-P[u] < -P[v]) std::swap(u, v); P[u] += P[v]; P[v] = u; lab.push_back(u); lab.push_back(v); } int merge(int u, int v){ merge(u, v, u); return u; } int count(){ return N; } int size(int u){ return -P[leader(u)]; } bool same(int u, int v){ return leader(u) == leader(v); } void clear(){ for(int i : lab) P[i] = -1; lab.clear(); N = P.size(); } }; } // namespace nachia #include <iostream> #include <string> #include <vector> #include <algorithm> #include <atcoder/modint> using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int main(){ int N, Q; cin >> N >> Q; auto dsu = nachia::Dsu(N*2); bool f = true; rep(q,Q){ int t; cin >> t; if(t == 1){ int u, v; cin >> u >> v; u--; v--; dsu.merge(u, v); dsu.merge(u+N, v+N); if(dsu.same(u, u+N)) f = false; } if(t == 2){ int u, v; cin >> u >> v; u--; v--; dsu.merge(u+N, v); dsu.merge(u, v+N); if(dsu.same(u, u+N)) f = false; } if(t == 3){ dsu.clear(); f = true; } int cnt = dsu.count() / 2; Modint ans = 0; if(f) ans = Modint(2).pow(cnt); cout << ans.val() << '\n'; } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;