This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define all(v) (v).begin(),(v).end()
using ll = long long;
using namespace std;
const int maxn = 1<<19;
int n, m;
vector<array<ll, 2>> g[maxn];
ll val[maxn];
ll vis[maxn], C[maxn], h[maxn], back = -1, bb;
ll s = 0;
ll dfs(int v, int p) {
ll cur = C[v];
vis[v] = 1;
if(h[v]&1) s -= C[v];
else s += C[v];
for(auto &[i, id] : g[v]) if(i != p) {
if(!vis[i]) {
h[i] = h[v]+1;
ll t = dfs(i, v);
cur += t;
val[id] = t;
} else if(h[i] < h[v]) {
if(back != -1 || (h[i]&1) != (h[v]&1)) {cout << "0\n", exit(0);}
back = id, bb = h[v]&1;
}
}
return -cur;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> C[i];
for(int f, t, i = 0; i < m; i++) {
cin >> f >> t;
g[f].push_back({t, i});
g[t].push_back({f, i});
}
dfs(1, 1);
if(back != -1) val[back] = bb?-s:s;
for(int i = 0; i < m; i++) cout << -2ll*val[i] << "\n"; cout << endl;
}
//?VK
//??V
//K?
Compilation message (stderr)
pipes.cpp: In function 'int main()':
pipes.cpp:41:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
41 | for(int i = 0; i < m; i++) cout << -2ll*val[i] << "\n"; cout << endl;
| ^~~
pipes.cpp:41:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
41 | for(int i = 0; i < m; i++) cout << -2ll*val[i] << "\n"; cout << endl;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |