結果

問題 No.2277 Honest or Dishonest ?
ユーザー HIcoderHIcoder
提出日時 2023-07-08 12:35:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 2,344 bytes
コンパイル時間 1,043 ms
コンパイル使用メモリ 116,800 KB
実行使用メモリ 12,244 KB
最終ジャッジ日時 2023-09-29 13:53:39
合計ジャッジ時間 6,170 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 72 ms
10,140 KB
testcase_04 AC 70 ms
10,724 KB
testcase_05 AC 22 ms
6,808 KB
testcase_06 AC 64 ms
8,592 KB
testcase_07 AC 52 ms
8,060 KB
testcase_08 AC 17 ms
6,404 KB
testcase_09 AC 43 ms
7,712 KB
testcase_10 AC 36 ms
6,488 KB
testcase_11 AC 17 ms
4,864 KB
testcase_12 AC 30 ms
5,212 KB
testcase_13 AC 69 ms
9,480 KB
testcase_14 AC 37 ms
5,692 KB
testcase_15 AC 19 ms
5,880 KB
testcase_16 AC 48 ms
7,896 KB
testcase_17 AC 25 ms
5,408 KB
testcase_18 AC 66 ms
9,896 KB
testcase_19 AC 65 ms
10,400 KB
testcase_20 AC 54 ms
8,784 KB
testcase_21 AC 49 ms
7,348 KB
testcase_22 AC 46 ms
7,780 KB
testcase_23 AC 67 ms
8,324 KB
testcase_24 AC 39 ms
7,588 KB
testcase_25 AC 47 ms
8,592 KB
testcase_26 AC 9 ms
5,932 KB
testcase_27 AC 33 ms
5,948 KB
testcase_28 AC 20 ms
5,872 KB
testcase_29 AC 31 ms
7,336 KB
testcase_30 AC 30 ms
6,180 KB
testcase_31 AC 52 ms
8,340 KB
testcase_32 AC 29 ms
6,780 KB
testcase_33 AC 65 ms
10,140 KB
testcase_34 AC 74 ms
10,620 KB
testcase_35 AC 6 ms
4,900 KB
testcase_36 AC 45 ms
7,544 KB
testcase_37 AC 72 ms
9,140 KB
testcase_38 AC 15 ms
4,756 KB
testcase_39 AC 23 ms
5,148 KB
testcase_40 AC 7 ms
4,392 KB
testcase_41 AC 78 ms
10,704 KB
testcase_42 AC 53 ms
8,924 KB
testcase_43 AC 82 ms
11,220 KB
testcase_44 AC 83 ms
12,096 KB
testcase_45 AC 83 ms
11,988 KB
testcase_46 AC 81 ms
11,988 KB
testcase_47 AC 87 ms
12,244 KB
testcase_48 AC 82 ms
12,028 KB
testcase_49 AC 84 ms
11,748 KB
testcase_50 AC 80 ms
11,032 KB
testcase_51 AC 83 ms
12,072 KB
testcase_52 AC 83 ms
11,196 KB
権限があれば一括ダウンロードができます

ソースコード

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