Skip to content

Commit

Permalink
Merge pull request #218 from wellFoundedDevelopers/byeonghee/53week
Browse files Browse the repository at this point in the history
[소병희] - 작업, 키 순서, 구간 합 구하기 5, 도넛과 막대 그래프
  • Loading branch information
bngsh authored Mar 12, 2024
2 parents ec7e1cf + 6849a1c commit ce368b3
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/kotlin/byeonghee/week53/구간 합 구하기 5.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package byeonghee.week53

class 소병희_구간합구하기5 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val table = Array(n + 1) { IntArray(n + 1) }
val sb = StringBuilder()

repeat(n) { i ->
readLine().split(" ").forEachIndexed { j, v ->
table[i+1][j+1] = table[i+1][j] + v.toInt()
}
}

repeat(n) { i ->
repeat(n) { j ->
table[i+1][j+1] += table[i][j+1]
}
}

repeat(m) {
val (r1, c1, r2, c2) = readLine().split(" ").map { it.toInt() }
sb.appendLine(table[r2][c2] + table[r1-1][c1-1] - table[r1-1][c2] - table[r2][c1-1])
}

println(sb)
}
}
}

fun main() {
소병희_구간합구하기5.solve()
}
27 changes: 27 additions & 0 deletions src/main/kotlin/byeonghee/week53/도넛과 막대 그래프.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package byeonghee.week53

class 소병희_도넛과막대그래프 {
fun solution(edges: Array<IntArray>): IntArray {
val answer = IntArray(4)
val inDegree = IntArray(1_000_001)
val outDegree = IntArray(1_000_001)

for((from, to) in edges) {
outDegree[from]++
inDegree[to]++
}

var v = 1
while(inDegree[v] > 0 || outDegree[v] > 0) {
if (inDegree[v] == 0 && outDegree[v] >= 2) answer[0] = v
else if (outDegree[v] == 0) answer[2]++
else if (inDegree[v] >= 2 && outDegree[v] == 2) answer[3]++

v++
}

answer[1] = outDegree[answer[0]] - answer[2] - answer[3]

return answer
}
}
39 changes: 39 additions & 0 deletions src/main/kotlin/byeonghee/week53/작업.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package byeonghee.week53

class 소병희_작업 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val preJob = Array(n + 1) { ArrayDeque<Int>() }

repeat(m) {
val (pre, cur) = readLine().split(" ").map { it.toInt() }
preJob[cur].add(pre)
}

val x = readLine().toInt()
var answer = 0
val visited = BooleanArray(n+1)
val q = ArrayDeque<Int>(x)
q.add(x)
visited[x] = true

while(q.isNotEmpty()) {
val cur = q.removeFirst()
preJob[cur].forEach {
if (visited[it].not()) {
q.add(it)
visited[it] = true
answer++
}
}
}

println(answer)
}
}
}

fun main() {
소병희_작업.solve()
}
46 changes: 46 additions & 0 deletions src/main/kotlin/byeonghee/week53/키 순서.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package byeonghee.week53

class 소병희_키순서 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val adj = Array(n) { IntArray(n) }

repeat(m) {
val (low, high) = readLine().split(" ").map { it.toInt() - 1 }
adj[low][high] = 1
adj[high][low] = -1
}

for(mid in 0 until n) for(a in 0 until n) for(b in 0 until n) {
val compare = adj[a][mid] + adj[mid][b]
when (compare) {
2 -> {
adj[a][b] = 1
adj[b][a] = -1
}

-2 -> {
adj[a][b] = -1
adj[b][a] = 1
}
}
}

var answer = 0
for(a in 0 until n) {
var cnt = 1
for(b in 0 until n) {
if (adj[a][b] != 0) cnt++
}
if (cnt == n) answer++
}

println(answer)
}
}
}

fun main() {
소병희_키순서.solve()
}

0 comments on commit ce368b3

Please sign in to comment.