일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 램프 파이썬
- 백준 1167 트리의 지름 파이썬
- 가장 긴 바이토닉 부분 수열 파이썬
- 백준 2352 반도체 설계 파이썬
- 백준 1516 게임 개발
- 백준 1238 파티 파이썬
- 가장 긴 팰린드롬 파이썬
- 백준 1613 역사
- SQL SERVER 장비교체
- 프로그래머스 가장 긴 팰린드롬
- SQL SERVER MIGRATION
- 프로그래머스 순위
- 다중 컬럼 NOT IN
- 등굣길 파이썬
- 백준 1043 거짓말 파이썬
- 프로그래머스 순위 파이썬
- 백준 1034 램프 파이썬
- 반도체 설계 파이썬
- SWEA
- 다리 만들기 파이썬
- 게임 개발 파이썬
- 프로그래머스 베스트앨범
- 프로그래머스 여행경로
- 백준 11054.가장 긴 바이토닉 부분 수열
- 역사 파이썬
- 백준 2146 다리 만들기
- 트리의 지름 파이썬
- 순위 파이썬
- 프로그래머스 등굣길
- 베스트앨범 파이썬
Archives
- Today
- Total
공부, 기록
백준 1613. 역사 파이썬(PYTHON) 본문
문제링크 : www.acmicpc.net/problem/1613
1613번: 역사
첫째 줄에 첫 줄에 사건의 개수 n(400 이하의 자연수)과 알고 있는 사건의 전후 관계의 개수 k(50,000 이하의 자연수)가 주어진다. 다음 k줄에는 전후 관계를 알고 있는 두 사건의 번호가 주어진다.
www.acmicpc.net
import sys
from collections import deque
input = sys.stdin.readline
"""def solution(graph,questions):
answer = list()
notanswer = list()
answers=[0]*len(questions)
for idx, question in enumerate(questions):
a, b = question
queue = deque()
visitied = set()
queue.append(a)
visitied.add(a)
while queue:
node = queue.popleft()
if node == b:
answer.append(idx)
break
else:
for i in graph[node]:
if i not in visitied:
queue.append(i)
visitied.add(i)
for idx, question in enumerate(questions):
b, a = question
queue = deque()
visitied = set()
queue.append(a)
visitied.add(a)
while queue:
node = queue.popleft()
if node == b:
notanswer.append(idx)
break
else:
for i in graph[node]:
if i not in visitied:
queue.append(i)
visitied.add(i)
for i in range(len(questions)):
if i in answer:
answers[i] = -1
elif i in notanswer:
answers[i] = 1
else:
answers[i] = 0
return answers"""
############dfs, bfs로 풀면 시간초과 플로이드마샬로 풀어야함#############
def main():
N, M = map(int,input().split())
#graph = dict()
maps = [[0 for _ in range(N)]for _ in range(N)]
#for i in range(N):
#graph.setdefault(i,list())
for i in range(M):
first, late = map(int,input().split())
maps[first-1][late-1]=1
#graph[first-1].append(late-1)
for k in range(N):
for i in range(N):
for j in range(N):
if maps[i][k] and maps[k][j]:
maps[i][j] = 1
questions = list()
for _ in range(int(input())):
a, b= map(int,input().split())
questions.append((a-1,b-1))
#answer = solution(graph,questions)
for i in questions:
a,b = i[0],i[1]
if maps[a][b] == 1:
print(-1)
elif maps[b][a] == 1:
print(1)
else:
print(0)
main()
'코딩' 카테고리의 다른 글
백준 2352. 반도체 설계 파이썬(PYTHON) (0) | 2020.11.14 |
---|---|
백준 2146. 다리 만들기 파이썬(PYTHON) (0) | 2020.11.14 |
백준 1516. 게임 개발 파이썬(PYTHON) (0) | 2020.11.14 |
백준 1238 파티 파이썬(PYTHON) (0) | 2020.11.14 |
백준 1167. 트리의 지름 파이썬 (0) | 2020.11.14 |