-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
43 lines (41 loc) · 867 Bytes
/
Copy pathJ.cpp
File metadata and controls
43 lines (41 loc) · 867 Bytes
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
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
vector<int> w,parent;
int n,m;
vector<vector<int>> v;
int solve(){
stack<int> S;
vector<int> dist(n,0);
for(int i=0;i<n;i++)
if(parent[i]==0){
S.push(i);
dist[i]=w[i];
}
while(!S.empty()){
int s = S.top();S.pop();
for(auto i:v[s]){
dist[i]=max(dist[i],dist[s]+w[i]);
parent[i]--;
if(parent[i]==0)S.push(i);
}
}
return *max_element(dist.begin(),dist.end());
}
int main(){
int tc;cin>>tc;
while(tc--){
cin>>m>>n;
v.clear();
v.resize(n);parent.resize(n,0);
while(m--){
int a,b;cin>>a>>b;
v[b].pb(a);
parent[a]++;
}
w.resize(n);
for(int i=0;i<n;i++)cin>>w[i];
int ans = solve();
cout<<ans<<endl;
}
}