Skip to content

[Algorithm] 효율적인 해킹 #172

Description

@hwangJi-dev

💬 문제

https://www.acmicpc.net/problem/1325


💬 Idea

  • B를 해킹하면 A를 해킹할 수 있으므로, B만 그래프에 정점으로 저장해준 후 B를 돌며 최대 해킹 수를 구한다.
  • 시간초과 줄여보겠다구,, 노력했는데!!! 이 문제는 Swift 언어로 풀이하면 어느 풀이든 시간초과가 나는 문제라고 한다. (readLine 입력 때문인 것 같다네용)

💬 풀이

import Foundation
func solution1325(){letMN=readLine()!.split(separator:"").map({Int($0)! })vargraph:[Int:[Int]]=[:]for_in1...MN[1]{letnodes=readLine()!.split(separator:"").map({Int($0)! })ifgraph[nodes[1]]==nil{graph[nodes[1]]=[nodes[0]]}else{graph[nodes[1]]?.append(nodes[0])}}func dfs(graph:[Int:[Int]], start:Int, visited:[Int])->[Int]{varvisited= visited
iflet nodes =graph[start]{foriin nodes {if !visited.contains(i){
visited.append(i)
visited =dfs(graph: graph, start: i, visited: visited)}}}return visited
}varhackingCountArr:[Int]=[Int](repeating:0, count:MN[0]+1)foriin graph.keys {letres=dfs(graph: graph, start: i, visited:[]).count
hackingCountArr[i]= res
}letmaxCnt= hackingCountArr.max()for(idx, i)in hackingCountArr.enumerated(){if i == maxCnt {print(idx)}}}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions