목차

Coordinates

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