結果
| 問題 |
No.2293 無向辺 2-SAT
|
| コンテスト | |
| ユーザー |
SSRS
|
| 提出日時 | 2023-05-05 22:01:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,531 bytes |
| コンパイル時間 | 1,771 ms |
| コンパイル使用メモリ | 171,844 KB |
| 実行使用メモリ | 18,140 KB |
| 最終ジャッジ日時 | 2024-11-23 07:38:45 |
| 合計ジャッジ時間 | 157,554 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 TLE * 24 |
ソースコード
#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;
}
}
};
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;
}
}
SSRS