結果

問題 No.2293 無向辺 2-SAT
ユーザー 👑 kmjpkmjp
提出日時 2023-05-05 22:14:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 431 ms / 4,000 ms
コード長 2,337 bytes
コンパイル時間 2,479 ms
コンパイル使用メモリ 208,648 KB
実行使用メモリ 28,296 KB
最終ジャッジ日時 2023-08-15 04:33:56
合計ジャッジ時間 30,135 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
7,700 KB
testcase_01 AC 8 ms
7,640 KB
testcase_02 AC 7 ms
7,756 KB
testcase_03 AC 420 ms
28,296 KB
testcase_04 AC 382 ms
7,988 KB
testcase_05 AC 369 ms
8,016 KB
testcase_06 AC 359 ms
7,720 KB
testcase_07 AC 278 ms
7,832 KB
testcase_08 AC 381 ms
7,832 KB
testcase_09 AC 351 ms
7,780 KB
testcase_10 AC 351 ms
7,780 KB
testcase_11 AC 382 ms
8,096 KB
testcase_12 AC 380 ms
7,736 KB
testcase_13 AC 342 ms
7,776 KB
testcase_14 AC 339 ms
7,696 KB
testcase_15 AC 338 ms
7,696 KB
testcase_16 AC 377 ms
7,996 KB
testcase_17 AC 404 ms
9,196 KB
testcase_18 AC 382 ms
7,932 KB
testcase_19 AC 187 ms
7,696 KB
testcase_20 AC 378 ms
8,064 KB
testcase_21 AC 371 ms
7,776 KB
testcase_22 AC 403 ms
8,300 KB
testcase_23 AC 405 ms
8,360 KB
testcase_24 AC 415 ms
8,352 KB
testcase_25 AC 410 ms
8,420 KB
testcase_26 AC 406 ms
8,280 KB
testcase_27 AC 414 ms
8,492 KB
testcase_28 AC 410 ms
8,280 KB
testcase_29 AC 417 ms
8,356 KB
testcase_30 AC 408 ms
8,304 KB
testcase_31 AC 412 ms
8,424 KB
testcase_32 AC 358 ms
7,724 KB
testcase_33 AC 364 ms
7,708 KB
testcase_34 AC 355 ms
7,768 KB
testcase_35 AC 361 ms
7,716 KB
testcase_36 AC 358 ms
7,736 KB
testcase_37 AC 361 ms
7,916 KB
testcase_38 AC 364 ms
7,720 KB
testcase_39 AC 364 ms
7,644 KB
testcase_40 AC 360 ms
7,716 KB
testcase_41 AC 333 ms
7,776 KB
testcase_42 AC 349 ms
7,696 KB
testcase_43 AC 358 ms
7,828 KB
testcase_44 AC 365 ms
7,832 KB
testcase_45 AC 377 ms
7,920 KB
testcase_46 AC 373 ms
7,668 KB
testcase_47 AC 376 ms
7,832 KB
testcase_48 AC 373 ms
7,712 KB
testcase_49 AC 376 ms
7,772 KB
testcase_50 AC 380 ms
7,936 KB
testcase_51 AC 382 ms
8,020 KB
testcase_52 AC 384 ms
8,124 KB
testcase_53 AC 398 ms
8,280 KB
testcase_54 AC 414 ms
9,136 KB
testcase_55 AC 431 ms
10,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------


template<int um> class UF_pop {
	public:
	vector<int> par,rank; vector<vector<int>> hist;
	UF_pop() {par=rank=vector<int>(um,0); for(int i=0;i<um;i++) par[i]=i;}
	void reinit() {int i; hist.clear(); FOR(i,um) rank[i]=0,par[i]=i;}
	int operator[](int x) {return (par[x]==x)?(x):operator[](par[x]);}
	void pop() {
		if(hist.size()) {
			auto a=hist.back();
			hist.pop_back();
			par[a[0]]=a[1]; rank[a[0]]=a[2];
			par[a[3]]=a[4]; rank[a[3]]=a[5];
		}
	}
	void operator()(int x,int y) {
		x=operator[](x); y=operator[](y);
		hist.push_back({x,par[x],rank[x],y,par[y],rank[y]});
		if(x==y) return;
		if(rank[x]<rank[y]) par[x]=y;
		else rank[x]+=(rank[x]==rank[y]), par[y]=x;
	}
};

UF_pop<404040> uf;
const ll mo=998244353;
int p2[404040];
int N,Q;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	p2[0]=1;
	FOR(i,404010) p2[i+1]=p2[i]*2%mo;
	
	int num=2*N;
	int ng=0;
	while(Q--) {
		cin>>i;
		int U,V;
		if(i==1) {
			cin>>U>>V;
			U--,V--;
			if(uf[2*U]!=uf[2*V]) {
				num--;
				uf(2*U,2*V);
			}
			if(uf[2*U+1]!=uf[2*V+1]) {
				num--;
				uf(2*U+1,2*V+1);
			}
			if(uf[2*U]==uf[2*U+1]) ng=1;
			if(uf[2*V]==uf[2*V+1]) ng=1;
		}
		else if(i==2) {
			cin>>U>>V;
			U--,V--;
			if(uf[2*U]!=uf[2*V+1]) {
				num--;
				uf(2*U,2*V+1);
			}
			if(uf[2*U+1]!=uf[2*V]) {
				num--;
				uf(2*U+1,2*V);
			}
			if(uf[2*U]==uf[2*U+1]) ng=1;
			if(uf[2*V]==uf[2*V+1]) ng=1;
		}
		else {
			while(uf.hist.size()) uf.pop();
			num=2*N;
			ng=0;
		}
		
		if(ng) {
			cout<<0<<endl;
		}
		else {
			cout<<p2[num/2]<<endl;
		}
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0