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> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
//#define int long long
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll inf = 2e6;
const ll mod = 1e9 + 7;
const int N = 3e6 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);
//ifstream in(".in");
//ofstream out(".out");
int n, m;
vector<pi> v[N];
ll val[N], vis[N], C[N], d[N], L = -1, R, ID = -1, bb, s;
void dfs(int nod, int p) {
ll cur = C[nod];
vis[nod] = 1;
if(d[nod] & 1) s -= C[nod];else s += C[nod];
for(auto i : v[nod]){
int it = i.ff, id = i.ss;
if(it == p)continue;
if(!vis[it]) {
d[it] = d[nod] + 1;
dfs(it, nod);
}else
if(d[it] < d[nod]) {
if(d[it] % 2 != d[nod] % 2){
cout << "0\n";
exit(0);
}
L = it, R = nod, ID = id, bb = d[nod] & 1;
}
}
}
ll recover(int nod, int p) {
ll cur = C[nod];
for(auto i : v[nod]){
int it = i.ff, id = i.ss;
if(it == p || id == ID)continue;
ll t = recover(it, nod);
val[id] = t;
cur += t;
}
return -cur;
}
void solve(){
cin >> n >> m;
if(m > n){
cout << "0" << '\n';
return;
}
for(int i = 1; i <= n; i++) cin >> C[i];
for(int i = 0, x, y; i < m; i++) {
cin >> x >> y;
v[x].push_back({y, i});
v[y].push_back({x, i});
}
dfs(1, 1);
if(L != -1) {
if(bb) s *= -1;
C[L] -= s/2;
C[R] -= s/2;
val[ID] = (-s)/2;
}
recover(1, 1);
for(int i = 0; i < m; i++) cout << -2ll*val[i] << '\n';
}
int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
//cout << setprecision(6) << fixed;
int T = 1;
//cin >> T;
while(T--){
solve();
}
}
Compilation message (stderr)
pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:33:9: warning: unused variable 'cur' [-Wunused-variable]
33 | ll cur = C[nod];
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |