ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 3665 |
문제명 | 최종 순위 |
레벨 | 골드 1 |
분류 |
정렬 |
시간복잡도 | O(nlogn + m) |
인풋사이즈 | n<=500, m<=25000 |
사용한 언어 | Python |
제출기록 | 29452KB / 168ms |
최고기록 | 144ms |
해결날짜 | 2021/09/29 |
"""Solution code for "BOJ 3665. 최종 순위".
- Problem link: https://www.acmicpc.net/problem/3665
- Solution link: http://www.teferi.net/ps/problems/boj/3665
Tags: [TopologicalSort]
"""
import graphlib
import sys
def main():
test_case_count = int(sys.stdin.readline())
for _ in range(test_case_count):
n = int(sys.stdin.readline()) # pylint: disable=unused-variable
t = [int(x) for x in sys.stdin.readline().split()]
pred_graph = {}
preds = set()
for t_i in t:
pred_graph[t_i - 1] = preds
preds = preds | {t_i - 1}
m = int(sys.stdin.readline())
for _ in range(m):
a, b = [int(x) for x in sys.stdin.readline().split()]
if b - 1 in pred_graph[a - 1]:
pred_graph[a - 1].remove(b - 1)
pred_graph[b - 1].add(a - 1)
else:
pred_graph[b - 1].remove(a - 1)
pred_graph[a - 1].add(b - 1)
try:
order = graphlib.TopologicalSorter(pred_graph).static_order()
print(*(u + 1 for u in order))
except graphlib.CycleError:
print('IMPOSSIBLE')
if __name__ == '__main__':
main()
"""Solution code for "BOJ 3665. 최종 순위".
- Problem link: https://www.acmicpc.net/problem/3665
- Solution link: http://www.teferi.net/ps/problems/boj/3665
Tags: [Sort]
"""
import sys
def main():
test_case_count = int(sys.stdin.readline())
for _ in range(test_case_count):
n = int(sys.stdin.readline())
t = [int(x) for x in sys.stdin.readline().split()]
orig_rank =[None] * (n + 1)
for i, t_i in enumerate(t):
orig_rank[t_i] = i
m = int(sys.stdin.readline())
new_rank = orig_rank[:]
for _ in range(m):
a, b = [int(x) for x in sys.stdin.readline().split()]
if orig_rank[a] < orig_rank[b]:
new_rank[a] += 1
new_rank[b] -= 1
else:
new_rank[a] -= 1
new_rank[b] += 1
if len(set(new_rank)) != n + 1:
print('IMPOSSIBLE')
else:
print(*sorted(range(1, n + 1), key=new_rank.__getitem__))
if __name__ == '__main__':
main()