結果
| 問題 | No.2277 Honest or Dishonest ? | 
| コンテスト | |
| ユーザー |  NAMIDAIRO | 
| 提出日時 | 2023-04-21 21:42:31 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 102 ms / 2,000 ms | 
| コード長 | 3,918 bytes | 
| コンパイル時間 | 1,094 ms | 
| コンパイル使用メモリ | 122,184 KB | 
| 最終ジャッジ日時 | 2025-02-12 11:17:50 | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 50 | 
ソースコード
#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;
}
            
            
            
        