結果

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

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;



ll mod_pow(ll x,ll y,ll mod){
    ll res=1;
    while(y>0){
        if(y&1){
            res*=x;
            res%=mod;
        }
        x*=x;
        x%=mod;
        y/=2;
    }
    return res;
}


int main(){
    int N,Q;
    cin>>N>>Q;

    vector<int> A(Q),B(Q),C(Q);

    vector<vector<P>> G(N);

    for(int i=0;i<Q;i++){
        cin>>A[i]>>B[i]>>C[i];
        A[i]--;B[i]--;
       
        G[A[i]].emplace_back(B[i],C[i]);
        G[B[i]].emplace_back(A[i],C[i]);

        
    }

   
    vector<int> color(N,0);//まだ塗られていない


    auto dfs=[&](auto f,int now,int c)->bool{
        color[now]=c;

        for(auto [to,flag]:G[now]){
            if(color[to]!=0){
                if(flag==1){
                    //toとnowが同じ色ならNG
                    if(color[to]==color[now]) return false;
                }else{
                    //flag==0
                    if(color[to]!=color[now]) return false;
                }

            }else{
                
                if(flag==1){
                    //nowとtoは異なる色でなければならない
                    
                    if(!f(f,to,-c)){
                        return false;
                    }
                    
                   
                }else{
                    //nowとtoは同じ色でなければならない
                    
                    if(!f(f,to,c)){
                        return false;
                    }
                   
                }

            }
        }
        return true;

    };


    ll ans=1;

    for(int i=0;i<N;i++){
        if(color[i]==0){
            
            

            if(dfs(dfs,i,1)){
                ans*=2; //色の反転で塗り分け方が2通り
                ans%=MOD;
            }else{
                

                ans*=0;
            }

            // for(int i=0;i<N;i++){
            //     cout<<"color["<<i<<"]="<<color[i]<<endl;
            // }

        }
    }

    
    cout<<ans<<endl;


}
0