complete 3665.py

fix makefile
This commit is contained in:
2025-05-28 16:45:26 +09:00
parent e584399efa
commit 47233029de
2 changed files with 89 additions and 0 deletions

View File

@@ -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)

View File

@@ -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])))