結果

問題 No.2293 無向辺 2-SAT
コンテスト
ユーザー C
提出日時 2026-10-21 15:04:20
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 58 ms / 4,000 ms
+ 610µs
コード長 2,261 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 10,211 ms
コンパイル使用メモリ 390,888 KB
実行使用メモリ 9,920 KB
最終ジャッジ日時 2026-10-21 16:21:55
合計ジャッジ時間 23,564 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
#include <boost/polygon/point_data.hpp>
#include <boost/polygon/voronoi.hpp>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(x) begin(x),end(x)
#define sz(x) (int)(x).size()
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vi>;
using pll=pair<ll,ll>;
using pi=pair<int,int>;

using mint=modint998244353;
using mint2=static_modint<2>;

// using BoostPoint=boost::polygon::point_data<int>;
// using Voronoi=boost::polygon::voronoi_diagram<double>;

#line 2 "data-structure/union-find-with-potential.hpp"

template <class T>
struct UnionFindWithPotential {
  vector<int> dat;
  vector<T> pot;

  UnionFindWithPotential(int N) : dat(N, -1), pot(N, T()) {}

  int root(int x) {
    if (dat[x] < 0) return x;
    int r = root(dat[x]);
    pot[x] += pot[dat[x]];
    return dat[x] = r;
  }

  // return P(x) - P(root(x))
  T potential(int x) {
    root(x);
    return pot[x];
  }

  bool same(int x, int y) { return root(x) == root(y); }

  // return P(x) - P(y)
  T diff(int x, int y) {
    return potential(x) - potential(y);
  }

  // s.t. P(x) = P(y) + p
  // return Satisfiablility
  bool merge(int x, int y, T p) {
    p += potential(y) - potential(x);
    x = root(x), y = root(y);
    if (x == y) return p == T();
    if (dat[x] < dat[y]) swap(x, y), p = -p;
    dat[y] += dat[x];
    dat[x] = y;
    pot[x] = p;
    return true;
  }

  // return size of CC including x
  int size(int x) { return -dat[root(x)]; }
};

int main()
{
  	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,Q;
	cin>>N>>Q;
	vi seen(N+1,0);
	int time=0,comp=N;
	bool ok=true;
	UnionFindWithPotential<mint2>uf(N+1);
	auto touch=[&](int x){
		if(seen[x]==time)return;
		seen[x]=time;
		uf.dat[x]=-1;
		uf.pot[x]=0;
	};
	while(Q--){
		int t;
		cin>>t;
		if(t==3){
			time++;
			comp=N;
			ok=true;
		}else{
			int u,v;
			cin>>u>>v;
			touch(u);
			touch(v);
			if(uf.same(u,v)){
				if(uf.diff(u,v)!=mint2(t-1))ok=false;
			}else{
				uf.merge(u,v,mint2(t-1));
				comp--;
			}
		}
		if(ok)cout<<mint(2).pow(comp).val()<<'\n';
		else cout<<0<<'\n';
	}

}
0