結果
| 問題 |
No.2293 無向辺 2-SAT
|
| コンテスト | |
| ユーザー |
👑 tatyam
|
| 提出日時 | 2023-05-05 22:14:15 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 96 ms / 4,000 ms |
| コード長 | 2,302 bytes |
| コンパイル時間 | 3,656 ms |
| コンパイル使用メモリ | 254,552 KB |
| 実行使用メモリ | 7,840 KB |
| 最終ジャッジ日時 | 2024-11-23 08:19:20 |
| 合計ジャッジ時間 | 14,185 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 53 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/dsu>
#include <atcoder/modint>
using namespace std;
using mint = atcoder::modint998244353;
class potentialized_unionfind{
/*
Copyright (c) 2021 0214sh7
https://github.com/0214sh7/library/
*/
private:
std::vector<int> UF,rank,pot;
vector<int> history;
public:
void init(int N){
UF.clear();
rank.clear();
for(int i=0;i<N;i++){
UF.push_back(i);
rank.push_back(0);
pot.push_back(0);
}
}
potentialized_unionfind(int N){
init(N);
}
int root(int k){
if(UF[k]==k){
return k;
}else{
int r = root(UF[k]);
pot[k] ^= pot[UF[k]];
UF[k] = r;
return UF[k];
}
}
int potential(int k){
root(k);
return pot[k];
}
bool same(int p,int q){
return root(p)==root(q);
}
bool unite(int P,int Q,bool d){
history.push_back(P);
history.push_back(Q);
//pot(Q)-pot(P)=dを満たす
d^=potential(P);
d^=potential(Q);
int p=root(P), q=root(Q);
if(p==q)return false;
if(rank[p]<rank[q]){
std::swap(p,q);
}
UF[q]=p;
if(rank[p]==rank[q])rank[p]++;
pot[q]=d;
return true;
}
int diff(int P,int Q){
return potential(Q)^potential(P);
}
void clear() {
for(int i : history) {
UF[i]=i;
rank[i]=0;
pot[i]=0;
}
history.clear();
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, Q;
cin >> N >> Q;
potentialized_unionfind uf(N);
const mint all = mint{2}.pow(N), inv2 = 998244354 / 2;
mint ans = all;
while(Q--) {
int t;
cin >> t;
if(t == 3) {
uf.clear();
ans = all;
}
else {
int u, v;
cin >> u >> v;
u--; v--; t--;
if(uf.same(u, v)) {
if(uf.diff(u, v) != t) ans = 0;
}
else {
uf.unite(u, v, t);
ans *= inv2;
}
}
cout << ans.val() << '\n';
}
}
tatyam