This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 1e5 + 5;
vector<int> need, curr;
vector<pair<int, int>> adj[maxN];
vector<int> in;
vector<int> ans;
void bfs(int n) {
queue<int> q;
vector<bool> vis(n, false);
for(int i = 0; i < n; i++) {
if (in[i] == 1) {
q.push(i);
}
}
while(!q.empty()) {
int v = q.front();
q.pop();
vis[v] = true;
for(auto u : adj[v]) {
if (vis[u.first]) continue;
ans[u.second] = (need[v] - curr[v]) * 2;
curr[u.first] += ans[u.second] / 2;
curr[v] += ans[u.second] / 2;
in[u.first]--;
if (in[u.first] == 1) {
q.push(u.first);
}
}
assert(curr[v] == need[v]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
need.resize(n);
curr.assign(n, 0);
in.assign(n, 0);
ans.assign(n, 0);
for(int& i : need) {
cin >> i;
}
for(int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back({y, i});
adj[y].push_back({x, i});
in[x]++;
in[y]++;
}
bfs(n);
for(int i = 0; i < m; i++) {
cout << ans[i] << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |