From fc86127050e4b353f7eb6ec34678320abd5f9caa Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Sun, 4 Jan 2026 20:41:26 +0900 Subject: [PATCH] baekjoon 20260104 --- code_study/Baekjoon/c/10757.c | 38 +++++++++++ code_study/Baekjoon/python/10757.py | 1 + code_study/Baekjoon/ts/1197_1.ts | 49 ++++++++++++++ code_study/Baekjoon/ts/1197_2.ts | 101 ++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 code_study/Baekjoon/c/10757.c create mode 100644 code_study/Baekjoon/python/10757.py create mode 100644 code_study/Baekjoon/ts/1197_1.ts create mode 100644 code_study/Baekjoon/ts/1197_2.ts diff --git a/code_study/Baekjoon/c/10757.c b/code_study/Baekjoon/c/10757.c new file mode 100644 index 0000000..3d6ce57 --- /dev/null +++ b/code_study/Baekjoon/c/10757.c @@ -0,0 +1,38 @@ +#include +#include +#include + +void reverse(char arr[]) { + int len = strlen(arr); + for (int i = 0; i < len / 2; i++) { + char temp = arr[i]; + arr[i] = arr[len - i - 1]; + arr[len - i - 1] = temp; + } +} + +int main(void) { + char A[10002] = { 0 }, B[10002] = { 0 }, res[10003] = { 0 }; + int carry = 0; + + scanf("%s %s", A, B); + reverse(A); + reverse(B); + + int len = strlen(A) > strlen(B) ? strlen(A) : strlen(B); + + for (int i = 0; i < len; i++) { + int sum = A[i] - '0' + B[i] - '0' + carry; + + while (sum < 0) sum += '0'; + if (sum > 9) carry = 1; + else carry = 0; + res[i] = sum % 10 + '0'; + } + + if (carry == 1) res[len] = '1'; + reverse(res); + printf("%s", res); + + return 0; +} \ No newline at end of file diff --git a/code_study/Baekjoon/python/10757.py b/code_study/Baekjoon/python/10757.py new file mode 100644 index 0000000..ccb694d --- /dev/null +++ b/code_study/Baekjoon/python/10757.py @@ -0,0 +1 @@ +print(sum(map(int, input().split()))) \ No newline at end of file diff --git a/code_study/Baekjoon/ts/1197_1.ts b/code_study/Baekjoon/ts/1197_1.ts new file mode 100644 index 0000000..16c14e7 --- /dev/null +++ b/code_study/Baekjoon/ts/1197_1.ts @@ -0,0 +1,49 @@ +export {}; +const input = require("fs").readFileSync(0).toString().trim().split("\n"); +const [V, E]: number[] = input[0].split(" ").map(Number); + +let edges: number[][] = []; +for(let edge of input.slice(1)) edges.push(edge.split(" ").map(Number)); + +let parent: number[] = Array.from({length: V+1}, (_, i) => i); +let rank: number[] = new Array(V+1).fill(0); + +const find = (x: number): number => { + if(x !== parent[x]) return parent[x] = find(parent[x]); + return parent[x]; +} + +const union = (x: number, y: number) => { + x = find(x); + y = find(y); + + if(x === y) return; + + if(rank[x] < rank[y]) [x, y] = [y, x]; + + parent[y] = x; + if(rank[x] === rank[y]) rank[x]++; +} + +const kruskal = (): number => { + edges.sort((a,b) => a[2] - b[2]); + + let total_weight = 0; + let edge_count = 0; + + for(let edge of edges) { + const [u, v, w]: number[] = edge; + + if(find(u) !== find(v)) { + union(u, v); + total_weight += w; + edge_count += 1; + + if(edge_count === V-1) break; + } + } + + return total_weight; +} + +console.log(kruskal()); \ No newline at end of file diff --git a/code_study/Baekjoon/ts/1197_2.ts b/code_study/Baekjoon/ts/1197_2.ts new file mode 100644 index 0000000..617dba3 --- /dev/null +++ b/code_study/Baekjoon/ts/1197_2.ts @@ -0,0 +1,101 @@ +export {}; + +class PriorityQueue { + private heap: [number, number][] = []; + + push(item: [number, number]): void { + this.heap.push(item); + this._bubbleUp(); + } + + pop(): [number, number] | undefined { + if (this.heap.length === 0) return undefined; + if (this.heap.length === 1) return this.heap.pop(); + const root = this.heap[0]; + this.heap[0] = this.heap.pop()!; + this._bubbleDown(); + return root; + } + + size(): number { + return this.heap.length; + } + + isEmpty(): boolean { + return this.heap.length === 0; + } + + private _bubbleUp(): void { + let index = this.heap.length - 1; + while (index > 0) { + const parentIdx = Math.floor((index - 1) / 2); + if (this.heap[index][0] >= this.heap[parentIdx][0]) break; + [this.heap[index], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[index]]; + index = parentIdx; + } + } + + private _bubbleDown(): void { + let index = 0; + const length = this.heap.length; + + while (true) { + let smallestIdx = index; + const leftIdx = 2 * index + 1; + const rightIdx = 2 * index + 2; + + if (leftIdx < length && this.heap[leftIdx][0] < this.heap[smallestIdx][0]) { + smallestIdx = leftIdx; + } + + if (rightIdx < length && this.heap[rightIdx][0] < this.heap[smallestIdx][0]) { + smallestIdx = rightIdx; + } + + if (smallestIdx === index) break; + + [this.heap[index], this.heap[smallestIdx]] = [this.heap[smallestIdx], this.heap[index]]; + index = smallestIdx; + } + } +} + +const input = require("fs").readFileSync(0).toString().trim().split("\n"); +const [V, E]: number[] = input[0].split(" ").map(Number); + +let graph: number[][][] = Array.from({length: V+1}, () => []); +for(let i=1; i<=E; i++) { + let [u,v,w]: number[] = input[i].split(" ").map(Number); + graph[u].push([w,v]); + graph[v].push([w,u]); +} + +const prim = (): number => { + let visited: boolean[] = new Array(V+1).fill(false); + let pq = new PriorityQueue(); + pq.push([0,1]); + + let total_weight: number = 0; + let count: number = 0; + + while(pq.size() !== 0) { + let [w, v]: number[] = pq.pop(); + + if(visited[v]) continue; + + visited[v] = true; + total_weight += w; + count++; + + if(count === V) break; + + for(let next of graph[v]) { + let [nw, nv] = next; + if(!visited[nv]) pq.push([nw, nv]); + } + } + + return total_weight; +} + +console.log(prim()); \ No newline at end of file