結果

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

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;
using Graph=vector<vector<int>>;

vector<int> color;
bool dfs(const Graph &G,int v,int cur=0){
  color[v]=cur;
  for(auto next_v:G[v]){
    if(color[next_v]!=-1){
      if(color[next_v]==cur) return false;
      continue;
    }
    if(!dfs(G,next_v,1-cur)) return false;
  }
  return true;
}

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int findSet(int x){
    if(x != p[x]) p[x]=findSet(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[findSet(x)];
  }
};

const int MOD=998244353;

ll modpow(ll x,ll n){
  x%=MOD;
  ll ans=1;
  while(n){
    if(n&1) ans=ans*x%MOD;
    x=x*x%MOD;
    n/=2;
  }
  return ans;
}

int main(){
  int n,q;
  cin>>n>>q;
  
  Dis ds=Dis(n),ds1=Dis(n);
  Graph G(n);
  
  vector<int> A(q),B(q),C(q);
  rep(i,q){
    int a,b,c;
    cin>>a>>b>>c;
    a--,b--;
    A[i]=a,B[i]=b,C[i]=c;
  }
  
  rep(i,q){
    if(C[i]==1) continue;
    ds.unite(A[i],B[i]);
  }
  
  rep(i,q){
    if(C[i]==0) continue;
    if(ds.same(A[i],B[i])){
      cout<<0<<endl;
      return 0;
    }
    int aa=ds.findSet(A[i]);
    int bb=ds.findSet(B[i]);
    G[aa].push_back(bb);
    G[bb].push_back(aa);
    ds1.unite(aa,bb);
  }
  
  color.assign(n,-1);
  bool b=true;
  rep(v,n){
    if(color[v]!=-1) continue;
    if(!dfs(G,v)) b=false;
  }
  if(b==false){
    cout<<0<<endl;
    return 0;
  }
  
  ll cnt=0;
  rep(i,n){
    if(ds.findSet(i)==i && ds1.findSet(i)==i) cnt++;
  }
  cout<<modpow(2,cnt)<<endl;
 
  return 0;
}
0