| ps | |
|---|---|
| 링크 | acmicpc.net/… |
| 출처 | BOJ |
| 문제 번호 | 4230 |
| 문제명 | 사랑과 전쟁 |
| 레벨 | 플래티넘 3 |
| 분류 |
2-sat |
| 시간복잡도 | O(T*(N+M)) |
| 인풋사이즈 | T<=?, N<=30, M<=50 |
| 사용한 언어 | Python |
| 제출기록 | 32720KB / 92ms |
| 최고기록 | 68ms |
| 해결날짜 | 2022/11/03 |
"""Solution code for "BOJ 4230. 사랑과 전쟁".
- Problem link: https://www.acmicpc.net/problem/4230
- Solution link: http://www.teferi.net/ps/problems/boj/4230
Tags: [2-sat]
"""
import sys
from teflib import twosat
def main():
while (line := sys.stdin.readline().rstrip()) != '0 0':
N, M = [int(x) for x in line.split()]
two_sat = twosat.TwoSat(N)
two_sat.x_is_true(~0)
for _ in range(M):
x, y = sys.stdin.readline().split()
x = int(x[:-1]) if x[-1] == 'h' else ~int(x[:-1])
y = int(y[:-1]) if y[-1] == 'h' else ~int(y[:-1])
two_sat.x_or_y(x, y)
try:
assignment = two_sat.find_truth_assignment()
except ValueError:
print('bad luck')
else:
answer = ' '.join(
f'{i}{"h" if x else "w"}'
for i, x in enumerate(assignment[1:], start=1)
)
print(answer)
if __name__ == '__main__':
main()