회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
그래프 DP 시간초과
4013번 - ATM
x1111101101
1년 전
0
O(V+E)로 풀릴 것 같은데 시간 초과가 나네요
어디서 시간이 터지는걸까요?
import java.util.* import kotlin.math.max import kotlin.math.min /** * 4013번: ATM * 1. SCC를 하나의 노드로 병합해 그래프 재구성 * 2. DP */ class Node(val id: Int) { val outEdges = LinkedList<Node>() // to val inEdges = LinkedList<Node>() // from val mergedNodeIds = LinkedList<Int>() var money = 0 var restaurant = false override fun hashCode(): Int = id } class Graph(val nodeSize: Int) { val nodes = Array(nodeSize) { Node(it) } fun mergeSCC(): Graph { val sccList = calcSCC() var id = 0 val newNodes = ArrayList<Node>() val sccNodes = Array<Node?>(nodeSize) { null } for (group in sccList) { val node = Node(id++) group.forEach { sccNodes[it.id] = node node.money += it.money node.restaurant = it.restaurant || node.restaurant node.mergedNodeIds += it.id } newNodes += node } for (i in nodes.indices) { val node = sccNodes[i]!! val prevNode = nodes[i] prevNode.outEdges.forEach { val tnode = sccNodes[it.id]!! if (tnode == node) return@forEach node.outEdges += tnode tnode.inEdges += node } } val newGraph = Graph(newNodes.size) for (i in newNodes.indices) { newGraph.nodes[i] = newNodes[i] } return newGraph } fun calcSCC(): List<List<Node>> { val sccList = LinkedList<List<Node>>() val stack = ArrayList<Node>(nodeSize) val parent = Array(nodeSize) { 0 } val finish = Array(nodeSize) { false } var id = 0 fun dfs(i: Int): Int { parent[i] = ++id stack += nodes[i] var curParent = id for (out in nodes[i].outEdges) { if (parent[out.id] == 0) { curParent = min(curParent, dfs(out.id)) } else if (!finish[out.id]) { curParent = min(curParent, parent[out.id]) } } if (parent[i] == curParent) { val currentScc = LinkedList<Node>() while (true) { val last = stack.removeLast() currentScc += last finish[last.id] = true if (last.id == i) { break } } sccList += currentScc } return curParent } for (node in nodes) { if (parent[node.id] == 0) dfs(node.id) } return sccList } } fun List<String>.f() = this.filter { it.isNotEmpty() && it != " " } fun main(): Unit = with(System.`in`.bufferedReader()) { val (n, m) = readLine().split(" ").f().map { it.toInt() } val graph = Graph(n) repeat(m) { val (a, b) = readLine().split(" ").f().map { it.toInt() - 1} graph.nodes[a].outEdges += graph.nodes[b] graph.nodes[b].inEdges += graph.nodes[a] } repeat(n) { graph.nodes[it].money = readLine().toInt() } val (s, p) = readLine().split(" ").f().map { it.toInt() - 1 } readLine().split(" ").f().map { it.toInt() - 1 }.forEach { graph.nodes[it].restaurant = true } val newGraph = graph.mergeSCC() // O(V+E) val startNode = newGraph.nodes.first { it.mergedNodeIds.contains(s) } // O(V) val dp = Array(newGraph.nodeSize) { -1 } fun dp(node: Node): Int { // O(V+E) if(dp[node.id] != -1) return dp[node.id] if(node == startNode) { dp[node.id] = startNode.money } else if(node.inEdges.isEmpty()) { dp[node.id] = Int.MIN_VALUE } else { val inMax = (node.inEdges.maxOfOrNull { dp(it) } ?: -1) dp[node.id] = if(inMax < 0) Int.MIN_VALUE else node.money + inMax } return dp[node.id] } var ans = 0 newGraph.nodes.forEach { if(it.restaurant) ans = max(ans, dp(it)) } println(max(0, ans)) }
댓글을 작성하려면
로그인
해야 합니다.
x1111101101 1년 전
O(V+E)로 풀릴 것 같은데 시간 초과가 나네요
어디서 시간이 터지는걸까요?