-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d61296
commit 5fd649b
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// 1325_효율적인 해킹.swift | ||
// Programmers1 | ||
// | ||
// Created by hwangJi on 2023/04/01. | ||
// | ||
|
||
import Foundation | ||
|
||
func solution1325() { | ||
let MN = readLine()!.split(separator: " ").map({ Int($0)! }) | ||
var graph: [Int: [Int]] = [:] | ||
|
||
for _ in 1...MN[1] { | ||
let nodes = readLine()!.split(separator: " ").map({ Int($0)! }) | ||
|
||
if graph[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] { | ||
var visited = visited | ||
|
||
if let nodes = graph[start] { | ||
for i in nodes { | ||
if !visited.contains(i) { | ||
visited.append(i) | ||
visited = dfs(graph: graph, start: i, visited: visited) | ||
} | ||
} | ||
} | ||
|
||
return visited | ||
} | ||
|
||
var hackingCountArr: [Int] = [Int](repeating: 0, count: MN[0] + 1) | ||
|
||
for i in graph.keys { | ||
let res = dfs(graph: graph, start: i, visited: []).count | ||
hackingCountArr[i] = res | ||
} | ||
|
||
let maxCnt = hackingCountArr.max() | ||
|
||
for (idx, i) in hackingCountArr.enumerated() { | ||
if i == maxCnt { | ||
print(idx) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters