-
Notifications
You must be signed in to change notification settings - Fork 0
/
2637.cpp
35 lines (35 loc) · 794 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
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int N,M;
vector<pair<int,int>> adj[101];
int in[101],cnt[101];
int main(){
scanf("%d%d",&N,&M);
for(int i=0;i<M;i++){
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
adj[x].push_back({y,k});
in[y]++;
}
vector<int> ans;
queue<int> q;
q.push(N);
cnt[N]=1;
while(!q.empty()){
int cur=q.front(); q.pop();
if(adj[cur].empty()){
ans.push_back(cur);
}
for(auto p : adj[cur]){
int nxt=p.first;
int c=p.second;
cnt[nxt]+=cnt[cur]*c;
if(--in[nxt]==0) q.push(nxt);
}
}
sort(ans.begin(),ans.end());
for(auto k : ans) printf("%d %d\n",k,cnt[k]);
}