結果

問題 No.2277 Honest or Dishonest ?
ユーザー nu50218
提出日時 2023-04-21 22:04:36
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 2,000 bytes
コンパイル時間 11,894 ms
コンパイル使用メモリ 286,252 KB
最終ジャッジ日時 2025-02-12 11:42:19
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOCAL
#include <local.hpp>
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2")
#include <bits/stdc++.h>
#define debug(...) ((void)0)
#define postprocess(...) ((void)0)
#endif

#include "atcoder/modint.hpp"
using mint = atcoder::modint998244353;

using namespace std;
using ll = long long;
using ld = long double;

struct WeightedUnionFind {
    vector<int> d;
    vector<int> p;
    WeightedUnionFind(int n = 0) : d(n, -1), p(n, 0) {}
    int find(int x) {
        if (d[x] < 0) return x;
        int root = find(d[x]);
        p[x] ^= p[d[x]];
        return d[x] = root;
    }
    // w = weight(y) ^ weight(x)
    bool unite(int x, int y, int w) {
        w ^= potential(x) ^ potential(y);
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;
        p[y] = w;
        return true;
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return -d[find(x)]; }
    // potential(x) := weight(x) ^ weight(root(x))
    int potential(int x) {
        find(x);
        return p[x];
    }
};

void solve([[maybe_unused]] int test) {
    int N, Q;
    cin >> N >> Q;

    WeightedUnionFind uf(N);

    int A[Q], B[Q], C[Q];

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

    for (int i = 0; i < Q; i++) {
        if ((uf.potential(A[i]) ^ uf.potential(B[i])) != C[i]) {
            cout << 0 << endl;
            return;
        }
    }

    mint ans = 1;

    set<int> roots;
    for (int i = 0; i < N; i++) {
        roots.insert(uf.find(i));
    }

    for (auto&& x : roots) {
        ans *= 2;
    }

    cout << ans.val() << endl;
}

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

    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        solve(i);
    }

    postprocess();
}
0