티스토리 뷰
문제 링크 : https://www.acmicpc.net/problem/2637
🍊해결방법
- 하나의 부품을 만들기 위해서 이전의 부품이 필요함
- 위상정렬 활용
- 기본부품은 1 2 3 4가 아니라 이전 부품이 없는 부품들이다!!!! - 이거 때문에 애 먹었다 ㅎㅎㅎㅎ
😀풀이
import java.io.*;
import java.util.*;
public class Main {
static class Node{
int x, cnt;
Node(int x, int cnt){
this.x = x;
this.cnt = cnt;
}
}
static int N;
static int M;
static int[] in;
static int[] in2;
static int[] count;
static List<Node>[] list;
static Queue<Node> q;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 기본부품, 기타부품
// 주어진 조건에 따라 정보를 저장
// N부터 사전에 필요한 부품 개수들을 더하면서 기본 부품의 총 개수 구하기
N = Integer.parseInt(br.readLine());
in = new int[N+1];
count = new int[N+1];
list = new List[N+1];
for(int i = 1; i<=N; i++){
list[i] = new ArrayList<>();
}
M = Integer.parseInt(br.readLine());
for(int i=0; i<M; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int res = Integer.parseInt(st.nextToken());
list[a].add(new Node(b, res));
in[b] ++;
}
produce();
// 기본품은 관계가 없는 부품임
for(int i=1; i<=N; i++){
if (!list[i].isEmpty()) continue;
System.out.println(i+" "+count[i]);
}
}
static void produce(){
q = new LinkedList<>();
q.add(new Node(N, 1));
while(!q.isEmpty()){
Node pn = q.poll();
int nx = pn.x;
int pCnt = pn.cnt;
// 선 조립품들 만들자
for(int i=0; i<list[nx].size(); i++){
int next = list[nx].get(i).x;
int nCnt = list[nx].get(i).cnt;
count[next] += (pCnt * nCnt);
in[next]--;
if (in[next] == 0)
q.add(new Node(next, count[next]));
}
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 14940 쉬운 최단거리 - BFS (0) | 2024.10.24 |
---|---|
[백준] 12100 2048(easy) - 구현 (0) | 2024.10.23 |
[백준] 1450 냅색문제 - 이진탐색 (0) | 2024.10.22 |
[백준] 2295 세 수의 합 (0) | 2024.10.21 |
[백준] 2252 줄 세우기 - 위상정렬 (0) | 2024.10.15 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준 경사로 자바
- 탑다운
- 유니온파인드
- 백준 14890 자바
- 스프링부트3 자바 버전
- 위상정렬
- sql
- 누적합
- 스프링부트3
- 티스토리챌린지
- 백준
- leetcode 1552
- 백준 경사로
- 구현
- 투포인터
- 백준 14890 경사로
- 슬라이딩윈도우
- 오블완
- dp
- 탑다운dp
- 스프링부트3 자바 17 오류
- 스프링부트3 java 버전오류
- #스프링부트 자바버전 오류
- dfs
- 이진탐색
- 조합
- 백준 14890
- BFS
- 바텀업
- 1482
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함