結果

問題 No.2277 Honest or Dishonest ?
ユーザー umezoumezo
提出日時 2023-04-21 23:04:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,121 bytes
コンパイル時間 2,782 ms
コンパイル使用メモリ 210,464 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-04-25 19:05:46
合計ジャッジ時間 6,788 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 74 ms
9,088 KB
testcase_04 AC 84 ms
12,160 KB
testcase_05 AC 29 ms
10,112 KB
testcase_06 AC 65 ms
7,296 KB
testcase_07 AC 53 ms
7,680 KB
testcase_08 AC 22 ms
10,624 KB
testcase_09 AC 55 ms
10,112 KB
testcase_10 AC 41 ms
6,944 KB
testcase_11 AC 19 ms
6,940 KB
testcase_12 AC 34 ms
6,940 KB
testcase_13 AC 69 ms
8,064 KB
testcase_14 AC 41 ms
6,940 KB
testcase_15 AC 22 ms
8,960 KB
testcase_16 AC 48 ms
7,424 KB
testcase_17 AC 23 ms
6,944 KB
testcase_18 AC 72 ms
12,160 KB
testcase_19 AC 74 ms
11,776 KB
testcase_20 AC 58 ms
9,600 KB
testcase_21 AC 50 ms
6,944 KB
testcase_22 AC 50 ms
8,576 KB
testcase_23 AC 64 ms
6,940 KB
testcase_24 AC 46 ms
8,576 KB
testcase_25 AC 56 ms
11,776 KB
testcase_26 AC 13 ms
10,112 KB
testcase_27 AC 34 ms
6,940 KB
testcase_28 AC 26 ms
8,576 KB
testcase_29 AC 39 ms
10,240 KB
testcase_30 AC 32 ms
6,944 KB
testcase_31 AC 53 ms
7,936 KB
testcase_32 AC 34 ms
9,856 KB
testcase_33 AC 72 ms
11,392 KB
testcase_34 AC 73 ms
9,600 KB
testcase_35 AC 9 ms
7,296 KB
testcase_36 AC 45 ms
7,168 KB
testcase_37 AC 74 ms
7,168 KB
testcase_38 AC 17 ms
6,940 KB
testcase_39 AC 24 ms
6,944 KB
testcase_40 AC 10 ms
6,940 KB
testcase_41 AC 82 ms
9,728 KB
testcase_42 AC 57 ms
8,448 KB
testcase_43 AC 95 ms
13,312 KB
testcase_44 AC 94 ms
13,312 KB
testcase_45 AC 93 ms
13,312 KB
testcase_46 AC 91 ms
13,440 KB
testcase_47 AC 88 ms
13,440 KB
testcase_48 AC 91 ms
13,312 KB
testcase_49 AC 90 ms
13,312 KB
testcase_50 AC 89 ms
13,312 KB
testcase_51 AC 93 ms
13,312 KB
testcase_52 AC 91 ms
13,184 KB
権限があれば一括ダウンロードができます

ソースコード

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