結果

問題 No.3104 Simple Graph Problem
ユーザー cho435
提出日時 2025-04-12 02:39:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 2,252 bytes
コンパイル時間 4,785 ms
コンパイル使用メモリ 269,456 KB
実行使用メモリ 12,500 KB
最終ジャッジ日時 2025-04-12 02:39:53
合計ジャッジ時間 11,799 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
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 <typename T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <typename T> 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<int> b(n);
	rep(i, 0, n) cin >> b[i];
	vector<vector<pair<int, int>>> g(n);
	vector<int> 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<int> ans(m, -1);
	vector<mint> a(n, 0);
	queue<int> 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<tuple<int, int, int>> 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();
}
0