회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
5일째 같은 문제로 고민중이나 안되는 이유를 모르겠습니다. 제발 알려주세요
17471번 - 게리맨더링
disis0226
2년 전
0
95%에서 오류가 나오는데 이유를 도저히 모르겠습니다
import sys sys.setrecursionlimit(10**7) # 두 선거구로 나누어져 있지 않으면 바로 종료 def check(): cnt = 0 for i in range(1, n + 1): if len(graph[i]) == 0: cnt += 1 if cnt >= 2: return False return True # 선거구의 인구 구하기 def get_popular(group: list): population = 0 for i in range(len(group)): population += popular[group[i]] return population # 선거구가 서로 연결되었는지 판단 def is_link(group: list): if not group: return False stack = [group[0]] visited_for_link = [0] * (n + 1) visited_for_link[group[0]] = 1 count = 1 # 방문한 도시의 수 while stack: cur_city = stack.pop() for next_city in graph[cur_city]: if visited_for_link[next_city] == 0 and next_city in group: visited_for_link[next_city] = 1 stack.append(next_city) count += 1 return count == len(group) def dfs(idx): global min_population_diff if len(red) > 0 and len(blue) > 0: if is_link(red) and is_link(blue): red_population = get_popular(red) blue_population = get_popular(blue) cur_population_diff = abs(red_population - blue_population) min_population_diff = min(min_population_diff, cur_population_diff) for i in range(idx, n + 1): if visited[i] == 0: visited[i] = 1 red.append(i) blue.remove(i) dfs(i + 1) red.pop() blue.append(i) visited[i] = 0 n = int(input()) popular = list(map(int, input().split())) popular.insert(0, 0) graph = [[] for _ in range(n + 1)] visited = [0] * (n + 1) # 선거구 저장 리스트 red = [] blue = [i for i in range(1, n + 1)] # 도시 연결 그래프 for i in range(1, n + 1): info = list(map(int, input().split())) for j in range(1, len(info)): graph[i].append(info[j]) min_population_diff = float('inf') checking = check() # 판별 시작 if checking == False: print(-1) else: dfs(1) if min_population_diff == float('inf'): print(-1) else: print(min_population_diff)
댓글을 작성하려면
로그인
해야 합니다.
disis0226 2년 전
95%에서 오류가 나오는데 이유를 도저히 모르겠습니다