ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 13899 |
문제명 | Coordinates |
레벨 | 골드 5 |
분류 |
그래프 |
시간복잡도 | O(N+M) |
인풋사이즈 | n<=100,000, m<=1,000,000 |
사용한 언어 | Python 3.11 |
제출기록 | 118116KB / 2616ms |
최고기록 | 2616ms |
해결날짜 | 2023/08/04 |
"""Solution code for "BOJ 13899. Coordinates".
- Problem link: https://www.acmicpc.net/problem/13899
- Solution link: http://www.teferi.net/ps/problems/boj/13899
Tags: [graph]
"""
import sys
from teflib import tree as ttree
from teflib import disjointset
def main():
N, M = [int(x) for x in sys.stdin.readline().split()]
graph = [[] for _ in range(N)]
coord_diff = [{} for _ in range(N)]
dsu = disjointset.DisjointSet(N)
for _ in range(M):
a, b, dx, dy = [int(x) for x in sys.stdin.readline().split()]
if dsu.union_if_disjoint(a - 1, b - 1):
graph[a - 1].append(b - 1)
graph[b - 1].append(a - 1)
coord_diff[a - 1][b - 1] = (dx, dy)
coord_diff[b - 1][a - 1] = (-dx, -dy)
source = 0
coords = [None] * N
coords[source] = (0, 0)
for u, p in ttree.dfs(graph, source):
if u != source:
px, py = coords[p]
dx, dy = coord_diff[p][u]
coords[u] = px + dx, py + dy
for x, y in coords:
print(x, y)
if __name__ == '__main__':
main()