結果

問題 No.2277 Honest or Dishonest ?
ユーザー Nzt3
提出日時 2023-04-21 22:14:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 926 bytes
コンパイル時間 2,093 ms
コンパイル使用メモリ 200,288 KB
最終ジャッジ日時 2025-02-12 11:49:49
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int rt[200000];
int getroot(int n){
  if(rt[n]==-1)return n;
  else return rt[n]=getroot(rt[n]);
}
void unite(int a,int b){
  a=getroot(a),b=getroot(b);
  if(a!=b){
    rt[a]=b;
  }
}
long long modpow(long long a,int n){
  long long ret=1,t=a;
  for(int i=0;i<30;i++){
    if(n>>i&1)ret=ret*t%mod;
    t=t*t%mod;
  }
  return ret;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  fill(rt,rt+200000,-1);
  int N,Q;
  cin>>N>>Q;
  while(Q--){
    int A,B,C;
    cin>>A>>B>>C;
    --A,--B;
    if(C==0){
      unite(A,B);
      unite(A+N,B+N);
    }else{
      unite(A,B+N);
      unite(A+N,B);
    }
  }
  for(int i=0;i<N;i++){
    if(getroot(i)==getroot(i+N)){
      cout<<"0\n";
      return 0;
    }
  }
  set<int>t;
  for(int i=0;i<N;i++){
    t.insert(getroot(i));
    t.insert(getroot(i+N));
  }
  cout<<modpow(2,t.size()/2)<<'\n';
}
0