|
| 1 | +class Database: |
| 2 | + def __init__(self, row_counts): |
| 3 | + self.row_counts = row_counts |
| 4 | + self.max_row_count = max(row_counts) |
| 5 | + n_tables = len(row_counts) |
| 6 | + self.ranks = [1] * n_tables |
| 7 | + self.parents = list(range(n_tables)) |
| 8 | + |
| 9 | + def merge(self, src, dst): |
| 10 | + src_parent = self.get_parent(src) |
| 11 | + dst_parent = self.get_parent(dst) |
| 12 | + |
| 13 | + if src_parent == dst_parent: |
| 14 | + return False |
| 15 | + |
| 16 | + # merge two components |
| 17 | + # use union by rank heuristic |
| 18 | + # update max_row_count with the new maximum table size |
| 19 | + return True |
| 20 | + |
| 21 | + def get_parent(self, table): |
| 22 | + # find parent and compress path |
| 23 | + return self.parents[table] |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + n_tables, n_queries = map(int, input().split()) |
| 28 | + counts = list(map(int, input().split())) |
| 29 | + assert len(counts) == n_tables |
| 30 | + db = Database(counts) |
| 31 | + for i in range(n_queries): |
| 32 | + dst, src = map(int, input().split()) |
| 33 | + db.merge(dst - 1, src - 1) |
| 34 | + print(db.max_row_count) |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + main() |
0 commit comments