From 47233029de5b53578f20770b96a5dcbc3f3d1151 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Wed, 28 May 2025 16:45:26 +0900 Subject: [PATCH] complete 3665.py fix makefile --- space/src-c/Makefile | 2 + storage/zeta/py/completed/3665.py | 87 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 storage/zeta/py/completed/3665.py diff --git a/space/src-c/Makefile b/space/src-c/Makefile index 9ef913e..cb52af6 100644 --- a/space/src-c/Makefile +++ b/space/src-c/Makefile @@ -8,6 +8,8 @@ BUILD_DIR = ../../build OUTPUT = c.out +all: build + build: $(CC) -o $(BUILD_DIR)/$(OUTPUT) -O2 -Wall -lm --std=c11 -fsanitize=address,leak,undefined $(SRC_DIR)/$(TARGET) diff --git a/storage/zeta/py/completed/3665.py b/storage/zeta/py/completed/3665.py new file mode 100644 index 0000000..ab3215b --- /dev/null +++ b/storage/zeta/py/completed/3665.py @@ -0,0 +1,87 @@ +from collections import deque + +import sys + +input = sys.stdin.readline + + +class FinalRankSolver: + def __init__(self, n: int, last_rank: list[int], m: int, changes: list[list[int]]): + self.n = n + self.last_rank = last_rank + self.m = m + self.changes = changes + + def solve(self) -> int | list[int]: + + flag = 1 + + adj = [[0 for _ in range(self.n)] for _ in range(self.n)] + + indeg = [0] * self.n + + last_rank_index = [0] * self.n + + for i, r in enumerate(self.last_rank): + last_rank_index[r - 1] = i + + for i in range(1, self.n): + for j in range(i): + adj[self.last_rank[i] - 1][self.last_rank[j] - 1] = 1 + indeg[self.last_rank[j] - 1] += 1 + + for first, second in self.changes: + if last_rank_index[first - 1] > last_rank_index[second - 1]: + adj[first - 1][second - 1] = 0 + adj[second - 1][first - 1] = 1 + indeg[second - 1] -= 1 + indeg[first - 1] += 1 + else: + first, second = second, first + adj[first - 1][second - 1] = 0 + adj[second - 1][first - 1] = 1 + indeg[second - 1] -= 1 + indeg[first - 1] += 1 + + new_rank = [] + q = deque() + for i, v in enumerate(indeg): + if v == 0: + q.append(i) + + while q: + index = q.popleft() + indeg[index] = -1 + new_rank.append(index + 1) + for ix, vi in enumerate(adj[index]): + if vi == 1: + indeg[ix] -= 1 + for i, v in enumerate(indeg): + if v == 0: + q.append(i) + + if any(deg != -1 for deg in indeg): + flag = 0 + + if flag: + return new_rank + else: + return flag + + +if __name__ == "__main__": + tc = int(input()) + + for _ in range(tc): + n = int(input()) + last_rank = list(map(int, input().split())) + m = int(input()) + changes = [list(map(int, input().split())) for i in range(m)] + + res = FinalRankSolver(n, last_rank, m, changes).solve() + if res == -1: + print("?") + elif res == 0: + print("IMPOSSIBLE") + else: + print(" ".join(map(str, res[::-1])))