結果

問題 No.2277 Honest or Dishonest ?
ユーザー nagisa5101nagisa5101
提出日時 2023-04-21 21:56:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,848 bytes
コンパイル時間 4,171 ms
コンパイル使用メモリ 263,760 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 18:56:43
合計ジャッジ時間 6,718 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 26 ms
6,940 KB
testcase_04 AC 26 ms
6,944 KB
testcase_05 AC 9 ms
6,944 KB
testcase_06 AC 21 ms
6,944 KB
testcase_07 AC 18 ms
6,944 KB
testcase_08 AC 7 ms
6,944 KB
testcase_09 AC 18 ms
6,944 KB
testcase_10 AC 13 ms
6,944 KB
testcase_11 AC 7 ms
6,940 KB
testcase_12 AC 14 ms
6,944 KB
testcase_13 AC 24 ms
6,944 KB
testcase_14 AC 15 ms
6,944 KB
testcase_15 AC 9 ms
6,944 KB
testcase_16 AC 18 ms
6,940 KB
testcase_17 AC 10 ms
6,944 KB
testcase_18 AC 25 ms
6,944 KB
testcase_19 AC 25 ms
6,944 KB
testcase_20 AC 21 ms
6,944 KB
testcase_21 AC 19 ms
6,940 KB
testcase_22 AC 18 ms
6,940 KB
testcase_23 AC 25 ms
6,944 KB
testcase_24 AC 16 ms
6,944 KB
testcase_25 AC 19 ms
6,944 KB
testcase_26 AC 5 ms
6,944 KB
testcase_27 AC 13 ms
6,944 KB
testcase_28 AC 9 ms
6,940 KB
testcase_29 AC 14 ms
6,940 KB
testcase_30 AC 13 ms
6,944 KB
testcase_31 AC 20 ms
6,940 KB
testcase_32 AC 13 ms
6,940 KB
testcase_33 AC 27 ms
6,940 KB
testcase_34 AC 27 ms
6,944 KB
testcase_35 AC 4 ms
6,944 KB
testcase_36 AC 17 ms
6,940 KB
testcase_37 AC 25 ms
6,940 KB
testcase_38 AC 7 ms
6,940 KB
testcase_39 AC 10 ms
6,940 KB
testcase_40 AC 4 ms
6,940 KB
testcase_41 AC 28 ms
6,944 KB
testcase_42 AC 19 ms
6,944 KB
testcase_43 AC 31 ms
6,940 KB
testcase_44 AC 32 ms
6,944 KB
testcase_45 AC 32 ms
6,944 KB
testcase_46 AC 31 ms
6,944 KB
testcase_47 AC 32 ms
6,944 KB
testcase_48 AC 31 ms
6,940 KB
testcase_49 AC 31 ms
6,944 KB
testcase_50 AC 32 ms
6,940 KB
testcase_51 AC 31 ms
6,940 KB
testcase_52 AC 31 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repll(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep2(i, n, m) for (int i = n; i < (int)(m); i++)
#define repll2(i, n, m) for (long long i = n; i < (long long)(m); i++)
#define all(v) v.begin(),v.end()
using ll=long long;
using ld=long double;
using vi=vector<int>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vvvl=vector<vvl>;
using vld=vector<ld>;
using vvld=vector<vld>;

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,1,-1};

const double PI = acos(-1);
//const ll MOD=1e9+7;
//const ll MOD=998244353;
const ll INF=(1LL<<60);
const int INF2=(1<<30);
//using mint=modint1000000007;
//using mint=modint998244353;

/* UnionFind:素集合系管理の構造体(union by size)
    isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
    unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
    treeSize(x): x を含む集合の要素数。
*/
struct UnionFind {
    vector<int> size, parents;
    UnionFind() {}
    UnionFind(int n) {  // make n trees.
        size.resize(n, 0);
        parents.resize(n, 0);
        for (int i = 0; i < n; i++) {
            makeTree(i);
        }
    }
    void makeTree(int x) {
        parents[x] = x;  // the parent of x is x
        size[x] = 1;
    }
    bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
    bool unite(int x, int y) {
        x = findRoot(x);
        y = findRoot(y);
        if (x == y) return false;
        if (size[x] > size[y]) {
            parents[y] = x;
            size[x] += size[y];
        } else {
            parents[x] = y;
            size[y] += size[x];
        }
        return true;
    }
    int findRoot(int x) {
        if (x != parents[x]) {
            parents[x] = findRoot(parents[x]);
        }
        return parents[x];
    }
    int treeSize(int x) { return size[findRoot(x)]; }
};

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,q;cin>>n>>q;
    UnionFind uf(2*n);
    int cnt=2*n;
    rep(i,q){
        int a,b,c;cin>>a>>b>>c;
        a--,b--;
        if(c==0){
            if(uf.isSame(a,b))continue;
            uf.unite(a,b);
            uf.unite(a+n,b+n);
            cnt-=2;
        }
        else{
            if(uf.isSame(a,b+n))continue;
            uf.unite(a+n,b);
            uf.unite(a,b+n);
            cnt-=2;
        }
    }
    rep(i,n){
        if(uf.isSame(i,i+n)){
            cout<<0<<endl;
            return 0;
        }
    }
    cnt/=2;
    cout<<pow_mod(2,cnt,998244353)<<endl;
    return 0;
}
0