結果

問題 No.1420 国勢調査 (Easy)
ユーザー rogi52rogi52
提出日時 2022-10-16 19:48:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,852 bytes
コンパイル時間 2,307 ms
コンパイル使用メモリ 203,780 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 21:27:50
合計ジャッジ時間 8,467 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 42 ms
4,380 KB
testcase_03 AC 42 ms
4,380 KB
testcase_04 AC 42 ms
4,376 KB
testcase_05 AC 41 ms
4,380 KB
testcase_06 AC 41 ms
4,380 KB
testcase_07 AC 9 ms
4,376 KB
testcase_08 AC 20 ms
4,380 KB
testcase_09 AC 11 ms
4,376 KB
testcase_10 AC 21 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 133 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 132 ms
4,380 KB
testcase_16 AC 135 ms
4,376 KB
testcase_17 AC 135 ms
4,380 KB
testcase_18 AC 133 ms
4,380 KB
testcase_19 AC 135 ms
4,380 KB
testcase_20 AC 134 ms
4,376 KB
testcase_21 AC 132 ms
4,376 KB
testcase_22 AC 166 ms
4,380 KB
testcase_23 AC 166 ms
4,376 KB
testcase_24 AC 165 ms
4,376 KB
testcase_25 AC 162 ms
4,376 KB
testcase_26 AC 164 ms
4,376 KB
testcase_27 AC 16 ms
4,376 KB
testcase_28 AC 16 ms
4,376 KB
testcase_29 AC 9 ms
4,380 KB
testcase_30 AC 17 ms
4,376 KB
testcase_31 AC 15 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

namespace algebra {

template < class T > class XOR {
  public:
    using set = T;
    static constexpr T op(const T &l, const T &r) { return l ^ r; }
    static constexpr T id = T(0);
    static constexpr T inv(const T &x) { return x; }
    static constexpr T pow(const T &x, const int n) { return n & 1 ? x : 0; }
    static constexpr bool comm = true;
};

}

template < class abel >
class pot_uf {
  public:
    using T = typename abel::set;
    vector<int> data;
    vector< T > pote;
    pot_uf(int n) : data(n, -1), pote(n, abel::id) {}
    int root(int x) {
        if(data[x] < 0) return x;
        int r = root(data[x]);
        pote[x] = abel::op(pote[x], pote[data[x]]);
        return data[x] = r;
    }
    T pot(int x) {
        root(x);
        return pote[x];
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    T diff(int x, int y) {
        return abel::op(pot(x), abel::inv(pot(y)));
    }
    bool unite(int x, int y, T p) {
        p = abel::op(p, diff(y, x));
        x = root(x), y = root(y);
        if(x == y) return p == abel::inv(p);
        if(data[x] < data[y]) swap(x, y), p = abel::inv(p);
        data[y] += data[x];
        data[x] = y;
        pote[x] = p;
        return true;
    }
    int size(int x) { return -data[root(x)]; }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    pot_uf< algebra::XOR<int> > uf(N);
    rep(_,M) {
        int a,b,y; cin >> a >> b >> y; a--; b--;
        if(uf.same(a, b)) {
            if(uf.diff(a, b) != y) {
                cout << -1 << endl;
                return 0;
            }
        } else {
            uf.unite(a, b, y);
        }
    }
    rep(i,N) cout << uf.pot(i) << endl;
}
0