#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 400005
#define ff first
#define ss second
#define pb push_back
#define sz(s) (int)s.size()
#define rep(c, a, b) for(c = a; c <= b; c++)
ll n, t, a[N],m, vis[N], out[N], par[N];
vector <pair<int,int>> ans;
queue <int> q;
map <pair<int,int>,int> vip;
int main () {
ios::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
for(int i = 1;i <= n; i++) {
cin >> a[i];
vis[i] = a[i];
}
for(int i = 1;i <= m; i++) {
int x, y;
cin >> x >> y;
out[x]++;
par[y] = x;
vip[{x,y}] = i;
vip[{y,x}] = i;
}
for(int i = 1; i <= n; i++) {
if(!out[i]) q.push(i);
}
while(!q.empty()) {
int b = q.front();
q.pop();
out[par[b]]--;
if(!out[par[b]]) q.push(par[b]);
ans.pb({vip[{b,par[b]}],vis[b]});
vis[par[b]] -= vis[b];
}
sort(ans.begin(),ans.end());
for(int i = 1;i<sz(ans);i++) {
cout << ans[i].ss << "\n";
}
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |