|
| 1 | +from collections import defaultdict |
| 2 | +n, m = map(int, input().split()) |
| 3 | +abc = [] |
| 4 | + |
| 5 | +class UnionFind(): |
| 6 | + def __init__(self, n): |
| 7 | + self.n = n + 1 |
| 8 | + self.parents = [-1] * (n + 1) |
| 9 | + |
| 10 | + def find(self, x): |
| 11 | + if self.parents[x] < 0: |
| 12 | + return x |
| 13 | + else: |
| 14 | + self.parents[x] = self.find(self.parents[x]) |
| 15 | + return self.parents[x] |
| 16 | + |
| 17 | + def union(self, x, y): |
| 18 | + x = self.find(x) |
| 19 | + y = self.find(y) |
| 20 | + |
| 21 | + if x == y: |
| 22 | + return |
| 23 | + |
| 24 | + if self.parents[x] > self.parents[y]: |
| 25 | + x, y = y, x |
| 26 | + |
| 27 | + self.parents[x] += self.parents[y] |
| 28 | + self.parents[y] = x |
| 29 | + |
| 30 | + def size(self, x): |
| 31 | + return -self.parents[self.find(x)] |
| 32 | + |
| 33 | + def same(self, x, y): |
| 34 | + return self.find(x) == self.find(y) |
| 35 | + |
| 36 | + def members(self, x): |
| 37 | + root = self.find(x) |
| 38 | + return [i for i in range(self.n) if self.find(i) == root] |
| 39 | + |
| 40 | + def roots(self): |
| 41 | + return [i for i, x in enumerate(self.parents) if x < 0] |
| 42 | + |
| 43 | + def group_count(self): |
| 44 | + return len(self.roots()) |
| 45 | + |
| 46 | + def all_group_members(self): |
| 47 | + return {r: self.members(r) for r in self.roots()} |
| 48 | + |
| 49 | + def __str__(self): |
| 50 | + return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) |
| 51 | + |
| 52 | +groups = UnionFind(n + 1) |
| 53 | +num = defaultdict(lambda: 0) |
| 54 | + |
| 55 | +for _ in range(m): |
| 56 | + a, b, c = map(int, input().split()) |
| 57 | + abc.append((a, b, c)) |
| 58 | + groups.union(a, b) |
| 59 | + |
| 60 | +group_nums = defaultdict(lambda: defaultdict(lambda: 0)) |
| 61 | +for a, b, c in abc: |
| 62 | + group_id = groups.find(a) |
| 63 | + if group_nums[group_id][a] != 0: |
| 64 | + group_nums[group_id][b] = group_nums[group_id][a] - c |
| 65 | + else: |
| 66 | + group_nums[group_id][a] = group_nums[group_id][b] + c |
| 67 | + |
| 68 | +candidates = defaultdict(lambda: []) |
| 69 | + |
| 70 | +group_orders = defaultdict(lambda: []) |
| 71 | +for group_id, nums in group_nums.items(): |
| 72 | + min_value = min(nums.values()) |
| 73 | + sorted_nums = list(sorted([[k, v - min_value] for k, v in nums.items()], key=lambda x: x[1])) |
| 74 | + max_num = sorted_nums[-1][1] |
| 75 | + for base_num in range(1, n + 1 - max_num): |
| 76 | + pattern = [] |
| 77 | + for pos, value in sorted_nums: |
| 78 | + pattern.append((pos, value + base_num)) |
| 79 | + candidates[group_id].append(pattern) |
| 80 | + |
| 81 | +sets = [set() for _ in range(n + 1)] |
| 82 | + |
| 83 | +for cand in candidates.values(): |
| 84 | + for c in cand: |
| 85 | + for pos, value in c: |
| 86 | + sets[pos].add(value) |
| 87 | +print(sets) |
| 88 | + |
| 89 | +ans = [] |
| 90 | +for ss in sets[1:]: |
| 91 | + if len(ss) == 1: |
| 92 | + ans.append(list(ss)[0]) |
| 93 | + else: |
| 94 | + ans.append(-1) |
| 95 | +print(*ans, sep=" ") |
0 commit comments