목차

완벽한 선거!

ps
링크acmicpc.net/…
출처BOJ
문제 번호3747
문제명완벽한 선거!
레벨플래티넘 4
분류

2-sat

시간복잡도O(T*(N+M))
인풋사이즈T<=?, N<=1000, M<=1,000,000
사용한 언어Python
제출기록494408KB / 4308ms
최고기록3748ms
해결날짜2022/11/07
태그

[라이]2-SAT 문제

풀이

코드

"""Solution code for "BOJ 3747. 완벽한 선거!".

- Problem link: https://www.acmicpc.net/problem/3747
- Solution link: http://www.teferi.net/ps/problems/boj/3747

Tags: [2-sat]
"""

import sys
from teflib import twosat


def main():
    inp = iter(sys.stdin.read().split())
    while True:
        try:
            N = int(next(inp))
        except StopIteration:
            break
        two_sat = twosat.TwoSat(N)
        M = int(next(inp))
        for _ in range(M):
            x, y = (int(next(inp)), int(next(inp)))
            x = x - 1 if x > 0 else x
            y = y - 1 if y > 0 else y
            two_sat.x_or_y(x, y)
        print('1' if two_sat.is_satisfiable() else '0')


if __name__ == '__main__':
    main()