結果

問題 No.2277 Honest or Dishonest ?
ユーザー tsugutsugutsugutsugu
提出日時 2023-04-21 21:49:02
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 3,371 bytes
コンパイル時間 3,841 ms
コンパイル使用メモリ 282,924 KB
実行使用メモリ 22,924 KB
最終ジャッジ日時 2024-04-25 18:54:13
合計ジャッジ時間 6,918 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
15,148 KB
testcase_01 AC 5 ms
15,248 KB
testcase_02 AC 6 ms
15,372 KB
testcase_03 AC 35 ms
15,632 KB
testcase_04 AC 44 ms
17,164 KB
testcase_05 AC 46 ms
20,752 KB
testcase_06 AC 29 ms
15,292 KB
testcase_07 AC 28 ms
15,500 KB
testcase_08 AC 50 ms
22,668 KB
testcase_09 AC 40 ms
17,672 KB
testcase_10 AC 20 ms
15,248 KB
testcase_11 AC 12 ms
15,376 KB
testcase_12 AC 17 ms
15,240 KB
testcase_13 AC 32 ms
15,240 KB
testcase_14 AC 21 ms
15,376 KB
testcase_15 AC 38 ms
19,596 KB
testcase_16 AC 27 ms
15,444 KB
testcase_17 AC 15 ms
15,500 KB
testcase_18 AC 48 ms
17,548 KB
testcase_19 AC 44 ms
17,224 KB
testcase_20 AC 33 ms
16,268 KB
testcase_21 AC 25 ms
15,372 KB
testcase_22 AC 30 ms
16,140 KB
testcase_23 AC 29 ms
15,244 KB
testcase_24 AC 29 ms
16,396 KB
testcase_25 AC 48 ms
18,828 KB
testcase_26 AC 41 ms
22,924 KB
testcase_27 AC 16 ms
15,372 KB
testcase_28 AC 35 ms
19,084 KB
testcase_29 AC 45 ms
19,468 KB
testcase_30 AC 17 ms
15,372 KB
testcase_31 AC 24 ms
15,496 KB
testcase_32 AC 43 ms
19,340 KB
testcase_33 AC 41 ms
16,784 KB
testcase_34 AC 31 ms
15,372 KB
testcase_35 AC 27 ms
19,592 KB
testcase_36 AC 21 ms
15,400 KB
testcase_37 AC 30 ms
15,400 KB
testcase_38 AC 10 ms
15,376 KB
testcase_39 AC 13 ms
15,244 KB
testcase_40 AC 22 ms
18,392 KB
testcase_41 AC 31 ms
15,376 KB
testcase_42 AC 23 ms
15,500 KB
testcase_43 AC 34 ms
15,752 KB
testcase_44 AC 49 ms
17,192 KB
testcase_45 AC 50 ms
17,168 KB
testcase_46 AC 51 ms
17,308 KB
testcase_47 AC 48 ms
17,336 KB
testcase_48 AC 48 ms
17,296 KB
testcase_49 AC 33 ms
15,756 KB
testcase_50 AC 33 ms
15,632 KB
testcase_51 AC 35 ms
15,888 KB
testcase_52 AC 34 ms
15,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};
const int mod = 998244353;
// const int mod = 1000000007;
struct modint {
    long long a = 0;
    modint(long long x = 0) {while (x < 0) x += mod; a = x % mod;}
    modint(const modint &r) {a = r.a;}
    modint operator -(){return modint(-a);}
    modint operator +(const modint &r) {return modint(*this) += r;}
    modint operator -(const modint &r) {return modint(*this) -= r;}
    modint operator *(const modint &r) {return modint(*this) *= r;}
    modint &operator +=(const modint &r) {
      a += r.a;
      if(a >= mod) a -= mod;
      return *this;
    }
    modint &operator -=(const modint &r) {
        a -= r.a;
        if(a < 0) a += mod;
        return *this;
    }
    modint &operator *=(const modint &r) {
        a = a * r.a % mod;
        return *this;
    }
    modint pow(long long r) const {
        if (!r) return 1;
        modint x = pow(r >> 1);
        x *= x;
        if (r & 1) x *= *this;
        return x;
    }
    modint inverse() const {
        return pow(mod - 2);
    }
    modint& operator/=(const modint r) {
        return (*this) *= r.inverse();
    }
    modint operator/(const modint r) const {
        modint res(*this);
        return res /= r;
    }
};
 
istream &operator>>(istream &is, modint &x) {
    long long t;
    is >> t;
    x = t;
    return is;
}
ostream &operator<<(ostream &os, const modint& x) {
    return os << x.a;
}
const int MAX = 500044;
modint fac[MAX], inv[MAX], finv[MAX];
void cominit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++) {
        fac[i] = fac[i - 1] * modint(i);
        inv[i] = - inv[mod % i] * modint(mod / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}
modint Com(long long n, long long k) {
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}
modint Per(long long n, long long k) {
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac[n] * finv[n - k];
}
 
using Mint = modint;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    UnionFind uf(2 * n);
    for (int i = 0; i < q; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        if (c == 0) {
            uf.merge(a, b);
            uf.merge(a + n, b + n);
        } else {
            uf.merge(a + n, b);
            uf.merge(a, b + n);
        }
    }
    for (int i = 0; i < n; i++) {
        if (uf.issame(i, i + n)) {
            cout << 0 << '\n';
            return 0;
        }
    }
    set<int> st;
    for (int i = 0; i < 2 * n; i++) {
        st.insert(uf.root(i));
    }
    int cnt = st.size() / 2;
    cout << Mint(2).pow(cnt) << '\n';
}
0