結果

問題 No.3104 Simple Graph Problem
ユーザー Kude
提出日時 2025-04-11 22:40:23
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,335 bytes
コンパイル時間 3,649 ms
コンパイル使用メモリ 309,704 KB
実行使用メモリ 22,120 KB
最終ジャッジ日時 2025-04-11 22:40:34
合計ジャッジ時間 9,820 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<mint> b(n);
  rep(i, n) {
    int x;
    cin >> x;
    b[i] = x;
  }
  vector<vector<P>> to(n);
  dsu uf(2 * n);
  int x = -1, y = -1, xyi = -1;
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    if (uf.same(u, v + n)) continue;
    if (!uf.same(u, v)) {
      to[u].emplace_back(v, i);
      to[v].emplace_back(u, i);
      uf.merge(u, v + n);
      uf.merge(u + n, v);
    } else if (xyi == -1) {
      x = u, y = v, xyi = i;
    }
  }
  vector<mint> ans(m), a(n);
  auto dfs = [&](auto&& self, int u, int p) -> void {
    for (auto [v, ei] : to[u]) if (v != p) {
      self(self, v, u);
      mint add = b[v] - a[v];
      a[u] += add;
      a[v] += add;
      ans[ei] += add;
    }
  };
  int root = x != -1 ? x : 0;
  dfs(dfs, root, -1);
  if (x != -1) {
    bool found = false;
    VI path;
    auto dfs2 = [&](auto&& self, int u, int p) -> void {
      if (u == y) {
        found = true;
        return;
      }
      for (auto [v, ei] : to[u]) if (v != p) {
        path.emplace_back(ei);
        self(self, v, u);
        if (found) return;
        path.pop_back();
      }
    };
    dfs2(dfs2, x, -1);
    path.emplace_back(xyi);
    assert(ssize(path) % 2);
    mint add = (b[x] - a[x]) / 2;
    rep(idx, ssize(path)) {
      int ei = path[idx];
      ans[ei] += idx % 2 == 0 ? add : -add;
    }
    a[x] += 2 * add;
  }
  if (a[root] == b[root]) {
    rep(i, m) cout << ans[i].val() << " \n"[i + 1 == m];
  } else {
    cout << -1 << '\n';
  }
}
0