#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct IOST { IOST() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); } } IOST; using mint = atcoder::modint998244353; void solve() { int n, m; cin >> n >> m; vector b(n); rep(i, 0, n) cin >> b[i]; vector>> g(n); vector cnt(n, 0); { int flg = 0; atcoder::dsu uf(n); atcoder::dsu check(n * 2); rep(i, 0, m) { int u, v; cin >> u >> v; u--; v--; if (uf.same(u, v)) { if (flg || check.same(u, v + n)) continue; flg = 1; } else { uf.merge(u, v); check.merge(u, v + n); check.merge(u + n, v); } cnt[u]++; cnt[v]++; g[u].push_back({i, v}); g[v].push_back({i, u}); } } vector ans(m, -1); vector a(n, 0); queue q; rep(i, 0, n) if (cnt[i] == 1) q.push(i); while (!q.empty()) { int nw = q.front(); q.pop(); for (auto [id, nx] : g[nw]) { if (ans[id] == -1) { mint ad = b[nw] - a[nw]; ans[id] = ad.val(); a[nw] += ad; a[nx] += ad; } cnt[nx]--; if (cnt[nx] == 1) q.push(nx); } } vector> pid; rep(i, 0, n) { if (cnt[i] != 2) continue; q.push(i); while (!q.empty()) { int nw = q.front(); q.pop(); for (auto [id, nx] : g[nw]) { if (cnt[nx] != 2) continue; if (ans[id] != -1) continue; pid.push_back({id, nw, nx}); mint ad = b[nw] - a[nw]; ans[id] = ad.val(); a[nw] += ad; a[nx] += ad; q.push(nx); break; } } break; } int flg = 0; for (auto [id, nw, nx] : pid) { mint ad = b[nw] - a[nw]; if (!flg) { ad /= 2; flg = 1; } ans[id] = (ans[id] + ad).val(); a[nw] += ad; a[nx] += ad; } rep(i, 0, n) if (a[i] != b[i]) { cout << "-1\n"; return; } rep(i, 0, m) if (ans[i] == -1) ans[i] = 0; rep(i, 0, m) cout << ans[i] << " "; cout << "\n"; } int main() { int t = 1; // cin >> t; rep(i, 0, t) solve(); }