結果

問題 No.3695 同室と別室
コンテスト
ユーザー GOTKAKO
提出日時 2026-09-10 00:14:01
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 26 ms / 2,000 ms
+ 466µs
コード長 2,241 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,828 ms
コンパイル使用メモリ 215,868 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-09-10 00:15:47
合計ジャッジ時間 2,920 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

using Wuf = int;
Wuf ng = -1;
class WeightedUnionFind{
    public:
    vector<int> par,siz;
    vector<Wuf> rootweight;
 
    Wuf op(Wuf a,Wuf b){return a^b;} //多分plusかxorだけ.
    Wuf inv(Wuf a){return a;} //逆元?.
 
    WeightedUnionFind(int N){
        par.resize(N,-1);
        siz.resize(N,1);
        rootweight.resize(N);
    }
 
    int root(int x){
        if(par.at(x) == -1) return x;
        else{
            int r = root(par.at(x));
            rootweight.at(x) = op(rootweight.at(x),rootweight.at(par.at(x)));
            return par.at(x) = r;
        }
    }
 
    Wuf weight(int x){
        root(x); //par等を更新する.
        return rootweight.at(x);
    }
 
    Wuf diffweight(int u, int v){ //return Av-Au
        if(!issame(u,v)) return ng;
        return op(weight(v),inv(weight(u)));
    }
 
    bool unite(int u,int v,Wuf w){ //Av-Au=w.
        w = op(w,weight(u)); w = op(w,inv(weight(v)));
        u = root(u); v = root(v);
        if(u == v) return false;
 
        if(siz.at(u) < siz.at(v)) swap(u,v),w = inv(w);
        par.at(v) = u;
        siz.at(u) += siz.at(v);
        rootweight.at(v) = w; 
        return true;
    }
    bool unite2(int u,int v,Wuf w){ //本来の辺と矛盾するならfalseを返す.
        if(root(u) == root(v)) return diffweight(u,v) == w;
        w = op(w,weight(u)); w = op(w,inv(weight(v)));
        u = root(u); v = root(v);
 
        if(siz.at(u) < siz.at(v)) swap(u,v),w = inv(w);
        par.at(v) = u;
        siz.at(u) += siz.at(v);
        rootweight.at(v) = w; 
        return true;
    }
    bool issame(int u, int v){
        if(root(u) == root(v)) return true;
        else return false;
    }
};

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

    int N,M; cin >> N >> M;
    int mul = N;
    WeightedUnionFind Z(N);
    while(M--){
        int t,a,b; cin >> t >> a >> b,a--,b--;
        if(Z.issame(a,b)){
            if(Z.diffweight(a,b) != t){cout << "0\n"; return 0;}
        }
        else Z.unite(a,b,t),mul--;
    }

    int answer = 1;
    while(mul--){
        answer += answer;
        if(answer >= 998244353) answer -= 998244353;
    }
    cout << answer << endl;
}
0