結果

問題 No.2277 Honest or Dishonest ?
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2023-04-21 21:42:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 3,918 bytes
コンパイル時間 1,342 ms
コンパイル使用メモリ 125,640 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-04-25 18:52:13
合計ジャッジ時間 5,855 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 83 ms
7,680 KB
testcase_04 AC 82 ms
8,576 KB
testcase_05 AC 30 ms
6,944 KB
testcase_06 AC 70 ms
6,944 KB
testcase_07 AC 52 ms
6,940 KB
testcase_08 AC 25 ms
6,940 KB
testcase_09 AC 49 ms
7,168 KB
testcase_10 AC 42 ms
6,944 KB
testcase_11 AC 19 ms
6,944 KB
testcase_12 AC 34 ms
6,940 KB
testcase_13 AC 77 ms
7,168 KB
testcase_14 AC 41 ms
6,940 KB
testcase_15 AC 24 ms
6,940 KB
testcase_16 AC 51 ms
6,944 KB
testcase_17 AC 26 ms
6,940 KB
testcase_18 AC 71 ms
8,448 KB
testcase_19 AC 79 ms
8,448 KB
testcase_20 AC 63 ms
7,168 KB
testcase_21 AC 59 ms
6,944 KB
testcase_22 AC 51 ms
6,940 KB
testcase_23 AC 77 ms
6,940 KB
testcase_24 AC 42 ms
6,944 KB
testcase_25 AC 55 ms
7,936 KB
testcase_26 AC 15 ms
6,944 KB
testcase_27 AC 35 ms
6,940 KB
testcase_28 AC 25 ms
6,940 KB
testcase_29 AC 39 ms
7,040 KB
testcase_30 AC 33 ms
6,944 KB
testcase_31 AC 55 ms
6,944 KB
testcase_32 AC 36 ms
6,944 KB
testcase_33 AC 74 ms
8,192 KB
testcase_34 AC 80 ms
8,064 KB
testcase_35 AC 10 ms
6,940 KB
testcase_36 AC 49 ms
6,940 KB
testcase_37 AC 82 ms
7,040 KB
testcase_38 AC 19 ms
6,944 KB
testcase_39 AC 25 ms
6,944 KB
testcase_40 AC 11 ms
6,944 KB
testcase_41 AC 91 ms
8,320 KB
testcase_42 AC 56 ms
7,040 KB
testcase_43 AC 87 ms
9,728 KB
testcase_44 AC 91 ms
9,472 KB
testcase_45 AC 91 ms
9,472 KB
testcase_46 AC 94 ms
9,472 KB
testcase_47 AC 96 ms
9,472 KB
testcase_48 AC 100 ms
9,472 KB
testcase_49 AC 95 ms
9,728 KB
testcase_50 AC 95 ms
9,472 KB
testcase_51 AC 88 ms
9,472 KB
testcase_52 AC 93 ms
9,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
#include <string>
#include <cmath>
#include <complex>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

using P = pair<ll, int>;

void chmin(ll & x, ll y) { x = min(x, y); }

struct mint {
    const long long mod = 998244353;
    long long x;
    mint(long long x_ = 0) : x((x_% mod + mod) % mod) {}

    mint& operator+=(const mint& other) {
        x += other.x;
        if (x >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& other) {
        x -= other.x;
        if (x < 0) x += mod;
        return *this;
    }
    mint& operator*=(const mint& other) {
        x *= other.x;
        x %= mod;
        return *this;
    }

    mint& operator+=(const long long n) {
        return *this += mint(n);
    }
    mint& operator-=(const long long n) {
        return *this -= mint(n);
    }
    mint& operator*=(const long long n) {
        return *this *= mint(n);
    }

    mint& operator=(const mint& other) {
        x = other.x;
        return *this;
    }
    mint& operator=(const long long n) {
        x = n % mod;
        return *this;
    }

    bool operator==(const mint& other) const {
        return x == other.x;
    }
    bool operator!=(const mint& other) const {
        return x != other.x;
    }

    mint operator-() const {
        mint res(mod - x);
        return res;
    }

    mint operator+(const mint& other) const {
        mint res(x);
        return res += other;
    }
    mint operator-(const mint& other) const {
        mint res(x);
        return res -= other;
    }
    mint operator*(const mint& other) const {
        mint res(x);
        return res *= other;
    }

    mint operator+(const long long n) const {
        mint res(x);
        mint other(n);
        return res += other;
    }
    mint operator-(const long long n) const {
        mint res(x);
        mint other(n);
        return res -= other;
    }
    mint operator*(const long long n) const {
        mint res(x);
        mint other(n);
        return res *= other;
    }

    mint pow(long long n) const {
        if (n == 0) return mint(1);
        mint res = pow(n / 2);
        res *= res;
        if (n % 2) res *= *this;
        return res;
    }
    mint inv() const {
        return pow(mod - 2);
    }
    mint& operator/=(const mint& other) {
        *this *= other.inv();
        return *this;
    }
    mint operator/(const mint& other) const {
        mint res(x);
        return res /= other;
    }
};

struct status {
    int to, cost;
};

using pii = pair<int, int>;

int main()
{
    int n, q;
    cin >> n >> q;

    vii<status> to(n);
    rep(i, q) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        to[a].push_back({ b, c });
        to[b].push_back({ a, c });
    }

    vi<int> dist(n, -1);
    mint ans = 1;
    rep(i, n) {
        if (dist[i] >= 0) continue;
        deque<int> dq;
        dq.push_front(i);
        dist[i] = 0;
        while (!dq.empty()) {
            int v = dq.front();
            dq.pop_front();
            for (status nv : to[v]) {
                if (dist[nv.to] >= 0) {
                    if ((dist[nv.to] - dist[v] - nv.cost) % 2 != 0) {
                        cout << 0 << endl;
                        return 0;
                    }
                    else continue;
                }
                else {
                    dist[nv.to] = dist[v] + nv.cost;
                    if (nv.cost == 0) dq.push_front(nv.to);
                    else dq.push_back(nv.to);
                }
            }
        }
        ans *= 2;
    }
    cout << ans.x << endl;
    return 0;
}
0