提出 #58292961


ソースコード 拡げる

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


template<class Abel> struct WeightedUnionFind {
     vector<int> par;
     vector<int> rank;
     vector<Abel> diff_weight;
 
     WeightedUnionFind(int n = 1, Abel SUM_UNITY = 0) {
         init(n, SUM_UNITY);
     }
 
     void init(int n = 1, Abel SUM_UNITY = 0) {
         par.resize(n); rank.resize(n); diff_weight.resize(n);
         for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
     }
 
     int root(int x) {
         if (par[x] == x) {
             return x;
         }
         else {
             int r = root(par[x]);
             diff_weight[x] += diff_weight[par[x]];
             return par[x] = r;
         }
     }
 
     Abel weight(int x) {
         root(x);
         return diff_weight[x];
     }
 
     bool issame(int x, int y) {
         return root(x) == root(y);
     }
 
     bool merge(int x, int y, Abel w) {
         w += weight(x); w -= weight(y);
         x = root(x); y = root(y);
         if (x == y) return false;
         if (rank[x] < rank[y]) swap(x, y), w = -w;
         if (rank[x] == rank[y]) ++rank[x];
         par[y] = x;
         diff_weight[y] = w;
         return true;
     }
 
     Abel diff(int x, int y) {
         return weight(y) - weight(x);
     }
 };

int main(){
	int n,m;cin >> n >> m;
	WeightedUnionFind<long long>uf(n);
	for(int i = 0;i<m;i++){
		int u,v;
		long long w;
		cin >> u >> v >> w;
		u--,v--;
		uf.merge(u,v,w);
	}
	for(int i = 0;i<n;i++){
		cout << uf.weight(i) << " ";
	}
	cout << endl;
	return 0;
}

提出情報

提出日時
問題 D - Hidden Weights
ユーザ blueberry1001
言語 C++ 20 (gcc 12.2)
得点 400
コード長 1623 Byte
結果 AC
実行時間 146 ms
メモリ 6676 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 29
セット名 テストケース
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 1 ms 3476 KiB
00_sample_02.txt AC 1 ms 3580 KiB
00_sample_03.txt AC 1 ms 3472 KiB
01_random_01.txt AC 57 ms 4696 KiB
01_random_02.txt AC 128 ms 6280 KiB
01_random_03.txt AC 134 ms 5160 KiB
01_random_04.txt AC 144 ms 6296 KiB
01_random_05.txt AC 13 ms 4956 KiB
01_random_06.txt AC 78 ms 6332 KiB
01_random_07.txt AC 140 ms 5792 KiB
01_random_08.txt AC 143 ms 6208 KiB
01_random_09.txt AC 17 ms 5228 KiB
01_random_10.txt AC 120 ms 6316 KiB
01_random_11.txt AC 131 ms 5012 KiB
01_random_12.txt AC 144 ms 6316 KiB
01_random_13.txt AC 32 ms 4752 KiB
01_random_14.txt AC 142 ms 6292 KiB
01_random_15.txt AC 130 ms 5032 KiB
01_random_16.txt AC 143 ms 6292 KiB
01_random_17.txt AC 79 ms 3848 KiB
01_random_18.txt AC 87 ms 6296 KiB
01_random_19.txt AC 118 ms 3968 KiB
01_random_20.txt AC 143 ms 6440 KiB
02_handmade_01.txt AC 145 ms 6248 KiB
02_handmade_02.txt AC 146 ms 6300 KiB
02_handmade_03.txt AC 146 ms 6328 KiB
02_handmade_04.txt AC 125 ms 6300 KiB
02_handmade_05.txt AC 51 ms 3436 KiB
02_handmade_06.txt AC 138 ms 6676 KiB