結果

問題 No.1420 国勢調査 (Easy)
ユーザー 沙耶花沙耶花
提出日時 2021-03-05 21:56:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 3,144 bytes
コンパイル時間 4,593 ms
コンパイル使用メモリ 259,108 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-04-16 09:24:08
合計ジャッジ時間 11,015 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 109 ms
5,376 KB
testcase_03 AC 108 ms
5,376 KB
testcase_04 AC 109 ms
5,376 KB
testcase_05 AC 109 ms
5,376 KB
testcase_06 AC 109 ms
5,376 KB
testcase_07 AC 96 ms
5,376 KB
testcase_08 AC 95 ms
5,376 KB
testcase_09 AC 95 ms
5,376 KB
testcase_10 AC 95 ms
5,376 KB
testcase_11 AC 94 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 143 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 142 ms
5,376 KB
testcase_16 AC 145 ms
5,376 KB
testcase_17 AC 145 ms
5,376 KB
testcase_18 AC 140 ms
5,376 KB
testcase_19 AC 141 ms
5,376 KB
testcase_20 AC 141 ms
5,376 KB
testcase_21 AC 144 ms
5,376 KB
testcase_22 AC 241 ms
5,632 KB
testcase_23 AC 235 ms
5,632 KB
testcase_24 AC 235 ms
5,888 KB
testcase_25 AC 239 ms
5,632 KB
testcase_26 AC 237 ms
5,760 KB
testcase_27 AC 102 ms
5,376 KB
testcase_28 AC 103 ms
5,376 KB
testcase_29 AC 101 ms
5,376 KB
testcase_30 AC 102 ms
5,376 KB
testcase_31 AC 102 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001

template <class S>
struct potentialized_dsu {
  public:
    potentialized_dsu() : _n(0) {}
    potentialized_dsu(int n) : _n(n), parent_or_size(n, -1), d(n) {}

    int merge(int a, int b, S v) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
		
		if (-parent_or_size[x] < -parent_or_size[y]){
			std::swap(x, y);
			std::swap(a, b);
		}
		
		v += diff(x,a);
		v -= diff(y,b);
		
		d[y] += d[x] + v;
		
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
		
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
		int l = leader(parent_or_size[a]);
		if(parent_or_size[a] != l){
			d[a] += d[parent_or_size[a]];
			parent_or_size[a] = l;
		}
        return parent_or_size[a];
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }
	
	S diff(int a,int b){
		assert(0 <= a && a < _n);
		assert(0 <= b && b < _n);
		leader(a);
		leader(b);
		return d[b] - d[a];
	}

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
	std::vector<S> d;
};

struct xorint{
	int v = 0;
	xorint operator+(xorint another)const{
		return (xorint(*this)+=another);
	}
	
	xorint &operator+=(const xorint &another){
		(*this).v ^= another.v;
		return (*this);
	}
	
	xorint operator-(xorint another)const{
		return (xorint(*this)+=another);
	}
	
	xorint &operator-=(const xorint &another){
		(*this).v ^= another.v;
		return (*this);
	}
};

int main(){
	
	int n,m;
	cin>>n>>m;
	
	vector<int> a(m),b(m),y(m);
	rep(i,m){
		cin>>a[i]>>b[i]>>y[i];
		a[i]--;b[i]--;
	}
	
	potentialized_dsu<xorint> D(n);
	
	rep(i,m){
		xorint t;
		t.v = y[i];
		if(D.same(a[i],b[i])){
			if(D.diff(a[i],b[i]).v!=t.v){
				cout<<-1<<endl;
				return 0;
			}
		}
		else{
			D.merge(a[i],b[i],t);
		}
	}
	
	
	vector<int> ans(n);
	rep(i,n){
		ans[i] = D.diff(0,i).v;
	}
	
	rep(i,n){
		cout<<ans[i]<<endl;
	}
	
    return 0;
}
0