baekjoon 20260226

This commit is contained in:
songyc macbook 2026-02-26 20:19:48 +09:00
parent bff62aa11b
commit cd2cda0f72
3 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,59 @@
import java.util.*;
public class _1005_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int[] ans = new int[T];
for(int t=0; t<T; t++) {
int N = sc.nextInt(), K = sc.nextInt();
int[] build_time = new int[N+1];
for(int i=1; i<=N; i++) build_time[i] = sc.nextInt();
int[] indegree = new int[N+1];
int[][] graph = new int[N+1][N+1];
for(int i=0; i<K; i++) {
int X = sc.nextInt(), Y = sc.nextInt();
indegree[Y]++;
graph[X][++graph[X][0]] = Y;
}
int W = sc.nextInt();
int[] complete_time = new int[N+1];
int[] qu = new int[N];
int front = 0, rear = 0;
for(int i=1; i<=N; i++) {
if(indegree[i] == 0) {
qu[rear++] = i;
complete_time[i] = build_time[i];
}
}
while(front <= rear) {
int now = qu[front++];
if(now == W) break;
for(int i=1; i<=graph[now][0]; i++) {
int next = graph[now][i];
indegree[next]--;
complete_time[next] = Math.max(complete_time[now] + build_time[next], complete_time[next]);
if(indegree[next] == 0) {
qu[rear++] = next;
}
}
}
ans[t] = complete_time[W];
}
sc.close();
for (int n : ans) {
System.out.println(n);
}
}
}

View File

@ -0,0 +1,55 @@
import java.util.*;
public class _1005_2 {
static int N, K, W;
static int[] build_time, complete_time;
static int[][] prev_build;
static int build(int n) {
if(complete_time[n] == -1) {
int total_time = 0;
for(int i=1; i<=prev_build[n][0]; i++) {
int prev = prev_build[n][i];
total_time = Math.max(total_time, build(prev));
}
complete_time[n] = total_time + build_time[n];
}
return complete_time[n];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int[] ans = new int[T];
for(int t=0; t<T; t++) {
N = sc.nextInt();
K = sc.nextInt();
build_time = new int[N+1];
for(int i=1; i<=N; i++) {
build_time[i] = sc.nextInt();
}
prev_build = new int[N+1][N+1];
for(int i=0; i<K; i++) {
int X = sc.nextInt(), Y = sc.nextInt();
prev_build[Y][++prev_build[Y][0]] = X;
}
complete_time = new int[N+1];
for(int i=1; i<=N; i++) complete_time[i] = -1;
W = sc.nextInt();
ans[t] = build(W);
}
sc.close();
for (int n : ans) {
System.out.println(n);
}
}
}

View File

@ -0,0 +1,24 @@
func slv() {
guard let T = Int(readLine() ?? "") else { return }
for _ in 0..<T {
guard let N = Int(readLine() ?? "") else { return }
var n = N
var m = 0
var ans: [String] = []
while n != 0 {
if n % 2 == 1 {
ans.append(String(m))
}
m += 1
n /= 2
}
print(ans.joined(separator: " "))
}
}
slv()