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;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<array<int, 2>>> g(n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
--x; --y;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
if (m >= n) {
cout << 0 << '\n';
return 0;
}
if (n == 1) {
cout << 2 * a[0] << '\n';
return 0;
}
if (m < n) {
vector<long long> res(m);
function<void(int, int)> Dfs = [&](int v, int pv) {
for (auto& e : g[v]) {
int u = e[0];
if (u == pv) {
continue;
}
Dfs(u, v);
if (a[u] != 0) {
res[e[1]] = 2 * a[u];
a[v] -= a[u];
}
}
};
Dfs(0, 0);
if (a[0] != 0) {
cout << 0 << '\n';
return 0;
}
for (int i = 0; i < m; i++) {
cout << res[i] << '\n';
}
return 0;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |