[BOJ] 2637 장난감조립

Time Lapse :15min 2sec

2637.cpp

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
32
33
34
35
#include <iostream>
#include <vector>
using namespace std;
int N, M;
vector<vector<pair<int, int>>> vec(101);
int memo[101][101];
bool tool[101];
bool visited[101];
void f(int idx, int mul) {
if(visited[idx]) return;
visited[idx] = true;
if(!tool[idx]){
memo[idx][idx] = 1;
return;
}
for(pair<int,int> p : vec[idx]) {
f(p.first, mul * p.second);
for(int i = 1; i <= N; ++i)
memo[idx][i] += memo[p.first][i] * p.second;
}
return;
}
int main() {
scanf("%d %d",&N, &M);
for(int i = 0; i < M; ++i){
int x, y, z;
scanf("%d %d %d",&x, &y, &z);
vec[x].emplace_back(make_pair(y,z));
tool[x] = true;
}
f(N, 1);
for(int i = 1; i <= N; ++i)
if(memo[N][i])
printf("%d %d\n",i,memo[N][i]);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2637/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.