結果
| 問題 | No.2277 Honest or Dishonest ? | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-04-22 06:54:48 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 91 ms / 2,000 ms | 
| コード長 | 1,088 bytes | 
| コンパイル時間 | 2,349 ms | 
| コンパイル使用メモリ | 194,624 KB | 
| 最終ジャッジ日時 | 2025-02-12 12:57:28 | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 50 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 100005;
const int mod = 998244353;
int a[maxn],b[maxn],c[maxn];
int parent[maxn],dist[maxn];
int Find(int x){
    if(parent[x] != x){
        int p = parent[x];
        parent[x] = Find(p);
        dist[x] ^= dist[p];
    }
    return parent[x];
}
int comp;
bool Union(int x, int y, int c){
    int rx = Find(x);
    int ry = Find(y);
    if(rx != ry){
        --comp;
        dist[ry] = (dist[x] ^ dist[y] ^ c);
        parent[ry] = rx;
        return true;
    }
    bool ok = (dist[y] ^ dist[x]) == c;
    return ok;
}
int main(){
    int N,Q;
    cin >> N >> Q;
    for(int i = 0;i < Q;++i){
        cin >> a[i] >> b[i] >> c[i];
    }
    for(int i = 1;i <= N;++i){
        parent[i] = i;
        dist[i] = 0;
    }
    comp = N;
    ll ans = 1;
    for(int i = 0;i < Q;++i){
        if(!Union(a[i], b[i], c[i])){
            cout << 0 << endl;
            return 0;
        }
    }
    for(int i = 1;i <= comp;++i){
        ans = ans * 2 % mod;
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        