-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2637.cpp
52 lines (52 loc) · 1002 Bytes
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<utility>
#include<math.h>
#include<memory.h>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef priority_queue<int> pqi;
typedef priority_queue<ll> pqll;
int inDegree[101];
int arr[101][101];
int ans[101];
int chk[101];
int main() {
cin.tie(NULL); ios_base::sync_with_stdio(false);
int n,m;
cin >> n>>m;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
arr[a][b] = c;
inDegree[b]++;
chk[a]++;
}
queue<int> q;
for (int i = 1; i <= n; i++) {
if (inDegree[i] == 0) q.push(i);
}
for (int i = 1; i <= n; i++) {
int x = q.front();
q.pop();
for (int j = 1; j <= n; j++) {
if (arr[x][j]) {
if (ans[x] == 0) ans[x] = 1;
ans[j] += arr[x][j] * ans[x];
if (--inDegree[j]==0) {
q.push(j);
}
}
}
}
for (int i = 1; i <= n; i++) {
if (!chk[i]) {
cout << i << ' ' << ans[i]<<'\n';
}
}
}