結果
| 問題 |
No.2293 無向辺 2-SAT
|
| コンテスト | |
| ユーザー |
hiro71687k
|
| 提出日時 | 2023-05-10 18:01:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,109 bytes |
| コンパイル時間 | 4,170 ms |
| コンパイル使用メモリ | 254,456 KB |
| 最終ジャッジ日時 | 2025-02-12 21:16:28 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 TLE * 19 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=14449999999999999;
ll mod=998244353;
struct unionfind
{
vector<ll>par,siz;
unionfind(ll n): par(n,-1),siz(n,1){}
ll root(ll x){
if (par[x]==-1)
{
return x;
}else{
return par[x]=root(par[x]);
}
}
bool issame(ll x,ll y){
return root(x)==root(y);
}
bool unite(ll x,ll y){
x=root(x);
y=root(y);
if (x==y)
{
return false;
}
if (siz[x]<siz[y])
{
swap(x,y);
}
par[y]=x;
siz[x]+=siz[y];
return true;
}
ll size(ll x){
return siz[root(x)];
}
};
int main(){
ll n,q;
cin >> n >> q;
unionfind uf(n*2);
ll ans=1;
ll half=inv_mod(2,mod);
ll x=1;
for (ll i = 0; i < n; i++)
{
x*=2;
x%=mod;
}
ans=x;
unionfind uf2(n*2);
vector<ll>a;
for (ll i = 0; i < q; i++)
{
ll t;
cin >> t;
if (t==1)
{
ll u,v;
cin >> u >> v;
u--,v--;
if (!uf.issame(u,v)||!uf.issame(u+n,v+n))
{
ans*=half;
ans%=mod;
}
uf.unite(u,v);
uf.unite(u+n,v+n);
if (uf.issame(u,u+n)||uf.issame(v,v+n))
{
ans=0;
}
}else if (t==2)
{
ll u,v;
cin >> u >> v;
u--,v--;
if (!uf.issame(u,v+n)||!uf.issame(u+n,v))
{
ans*=half;
ans%=mod;
}
uf.unite(u,v+n);
uf.unite(u+n,v);
if (uf.issame(u,u+n)||uf.issame(v,v+n))
{
ans=0;
}
}else{
uf=uf2;
ans=x;
}
a.push_back(ans);
}
for (ll i = 0; i < a.size(); i++)
{
cout << a[i] << endl;
}
}
hiro71687k