結果

問題 No.2277 Honest or Dishonest ?
ユーザー otoshigootoshigo
提出日時 2023-04-21 21:42:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 2,144 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 97,752 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-25 18:52:21
合計ジャッジ時間 5,271 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 45 ms
8,268 KB
testcase_04 AC 53 ms
9,952 KB
testcase_05 AC 23 ms
7,424 KB
testcase_06 AC 33 ms
6,940 KB
testcase_07 AC 30 ms
6,940 KB
testcase_08 AC 20 ms
7,552 KB
testcase_09 AC 36 ms
8,384 KB
testcase_10 AC 21 ms
6,940 KB
testcase_11 AC 11 ms
6,940 KB
testcase_12 AC 15 ms
6,940 KB
testcase_13 AC 39 ms
7,456 KB
testcase_14 AC 19 ms
6,940 KB
testcase_15 AC 18 ms
6,944 KB
testcase_16 AC 30 ms
6,940 KB
testcase_17 AC 14 ms
6,940 KB
testcase_18 AC 52 ms
9,900 KB
testcase_19 AC 50 ms
9,644 KB
testcase_20 AC 38 ms
8,108 KB
testcase_21 AC 27 ms
6,940 KB
testcase_22 AC 33 ms
7,400 KB
testcase_23 AC 33 ms
6,940 KB
testcase_24 AC 31 ms
7,312 KB
testcase_25 AC 41 ms
9,216 KB
testcase_26 AC 14 ms
6,944 KB
testcase_27 AC 18 ms
6,944 KB
testcase_28 AC 19 ms
6,940 KB
testcase_29 AC 30 ms
8,068 KB
testcase_30 AC 19 ms
6,944 KB
testcase_31 AC 35 ms
7,744 KB
testcase_32 AC 27 ms
7,752 KB
testcase_33 AC 51 ms
9,588 KB
testcase_34 AC 47 ms
8,716 KB
testcase_35 AC 9 ms
6,944 KB
testcase_36 AC 28 ms
6,940 KB
testcase_37 AC 38 ms
6,940 KB
testcase_38 AC 9 ms
6,940 KB
testcase_39 AC 13 ms
6,948 KB
testcase_40 AC 8 ms
6,940 KB
testcase_41 AC 51 ms
8,828 KB
testcase_42 AC 38 ms
8,068 KB
testcase_43 AC 64 ms
11,004 KB
testcase_44 AC 64 ms
11,008 KB
testcase_45 AC 65 ms
11,000 KB
testcase_46 AC 72 ms
11,004 KB
testcase_47 AC 62 ms
11,008 KB
testcase_48 AC 64 ms
11,004 KB
testcase_49 AC 67 ms
11,000 KB
testcase_50 AC 65 ms
10,876 KB
testcase_51 AC 65 ms
11,000 KB
testcase_52 AC 67 ms
11,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<tuple>
#include<queue>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) : par(n,-1), siz(n,1) {}
    int find(int x){
        if (par[x] == -1) return x;
        else{
            par[x] = find(par[x]);
            return par[x];
        }
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    int size(int x){
        return siz[find(x)];
    }
    void merge(int x, int y){
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (siz[x] < siz[y]) swap(x,y);
        siz[x] += siz[y];
        par[y] = x;
    }
};

const ll MOD=998244353;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,q;
    cin>>n>>q;
    vector<vector<pair<int,int>>>g(n);
    UnionFind uf(n);
    vector<tuple<int,int,int>>V;
    rep(i,q){
        int a,b,c;
        cin>>a>>b>>c;
        a--;
        b--;
        V.push_back(make_tuple(a,b,c));
        if(!uf.same(a,b)){
            uf.merge(a,b);
            g[a].push_back(make_pair(b,c));
            g[b].push_back(make_pair(a,c));
        }
    }
    ll ans=1;
    vector<bool>flag(n);
    vector<int>C(n,-1);
    rep(i,n){
        int l=uf.find(i);
        if(flag[l])continue;
        C[i]=1;
        queue<int>q;
        q.push(i);
        while(q.size()){
            int x=q.front();
            q.pop();
            for(auto[j,c]:g[x]){
                if(C[j]!=-1)continue;
                if(c==0)C[j]=C[x];
                else C[j]=1-C[x];
                q.push(j);
            }
        }
        flag[l]=true;
        ans=2*ans%MOD;
    }
    for(auto[a,b,c]:V){
        if(c==0&&C[a]!=C[b])ans=0;
        if(c==1&&C[a]==C[b])ans=0;
    }
    cout<<ans<<"\n";
}
0