結果

問題 No.3104 Simple Graph Problem
ユーザー shobonvip
提出日時 2025-01-31 23:33:54
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 3,704 bytes
コンパイル時間 3,426 ms
コンパイル使用メモリ 213,844 KB
実行使用メモリ 33,088 KB
最終ジャッジ日時 2025-01-31 23:40:39
合計ジャッジ時間 14,090 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/modint>
typedef atcoder::modint998244353 mint;

int main() {
	int n, m; cin >> n >> m;
	vector<mint> b(n);
	for (int i=0; i<n; i++) {
		int x; cin >> x;
		b[i] = x;
	}

	vector<int> u(m), v(m);
	vector ikeru(n, vector<pair<int,int>>(0));
	for (int i=0; i<m; i++) {
		cin >> u[i] >> v[i];
		u[i]--; v[i]--;
		ikeru[u[i]].push_back(pair(v[i], i));
		ikeru[v[i]].push_back(pair(u[i], i));
	}

	vector<int> color(n);
	bool is_bipartite = true;
	vector<int> odd_cycle_edge_list;
	vector<int> odd_cycle_vertex_list;
	vector<bool> odd_cycle_vertex(n);
	vector<bool> tansaku(n);

	auto dfs = [&](auto self, int i) -> int {
		for (auto [j,e]: ikeru[i]) {
			if (tansaku[j]) {
				if (color[i] == color[j]) {
					is_bipartite = false;
					odd_cycle_edge_list.push_back(e);
					odd_cycle_vertex[i] = true;
					odd_cycle_vertex_list.push_back(i);
					return j;
				}
			}else{
				tansaku[j] = true;
				color[j] = color[i] ^ 1;
				int v = self(self, j);
				if (v >= 0) {
					odd_cycle_edge_list.push_back(e);
					odd_cycle_vertex[i] = true;
					odd_cycle_vertex_list.push_back(i);
					if (v == i) return -1;
					return v;
				}
			}
			if (!is_bipartite) {
				return -1;
			}
		}
		return -1;
	};

	tansaku[0] = true;
	dfs(dfs, 0);

	reverse(odd_cycle_edge_list.begin(), odd_cycle_edge_list.end());
	reverse(odd_cycle_vertex_list.begin(), odd_cycle_vertex_list.end());


	if (is_bipartite) {
		mint total = 0;
		for (int i=0; i<n; i++) {
			if (color[i] == 0) total += b[i];
			else total -= b[i];
		}
		if (total.val() != 0) {
			cout << -1 << '\n';
			return 0;
		}
	}

	vector new_ikeru(n, vector<pair<int,int>>(0));
	auto dfs2 = [&](auto self, int i) -> void {
		for(auto [j,e]: ikeru[i]) {
			if (tansaku[j]) continue;
			if (odd_cycle_vertex[j]) continue;
			tansaku[j] = 1;
			new_ikeru[i].push_back(pair(j, e));
			new_ikeru[j].push_back(pair(i, e));
			self(self, j);
		}
	};

	fill(tansaku.begin(), tansaku.end(), false);
	if (is_bipartite) {
		tansaku[0] = true;
		dfs2(dfs2, 0);
	}else{
		for (int i: odd_cycle_edge_list) {
			new_ikeru[u[i]].push_back(pair(v[i], i));
			new_ikeru[v[i]].push_back(pair(u[i], i));
		}
		for (int i: odd_cycle_vertex_list) {
			assert(!tansaku[i]);
			tansaku[i] = true;
			dfs2(dfs2, i);
		}
	}

	vector<int> deg(n);
	for (int i=0; i<n; i++) {
		deg[i] = (int)new_ikeru[i].size();
	}
		
	vector<int> mada;
	for (int i=0; i<n; i++) {
		assert(deg[i] != 0);
		if (deg[i] == 1) {
			mada.push_back(i);
		}
	}

	fill(tansaku.begin(), tansaku.end(), false);
	vector<mint> ans(m);
	while(!mada.empty()) {
		int i = mada.back();
		mada.pop_back();
		tansaku[i] = true;
		for (auto [j,e]: new_ikeru[i]) {
			if (tansaku[j]) continue;
			ans[e] = b[i];
			b[i] -= ans[e];
			b[j] -= ans[e];
			deg[j] -= 1;
			if (deg[j] == 1) {
				mada.push_back(j);
			}
		}
	}

	if (!is_bipartite) {
		int k = (int)odd_cycle_edge_list.size();
		mint total_weight = 0;
		for (int i: odd_cycle_vertex_list){
			total_weight += b[i];
		}
		total_weight *= mint(2).inv();
		
		{
			int e = odd_cycle_edge_list[0];
			ans[e] = total_weight;
			for (int i=2; i<k; i+=2) {
				int x = odd_cycle_vertex_list[i];
				ans[e] -= b[x];
			}
		}
		
		for (int i=1; i<k; i++) {
			int x = odd_cycle_vertex_list[i];
			int e = odd_cycle_edge_list[i];
			int eprev = odd_cycle_edge_list[i-1];
			ans[e] = b[x] - ans[eprev];
		}

		for (int i=0; i<k; i++) {
			b[odd_cycle_vertex_list[i]] -= ans[odd_cycle_edge_list[i]];
			b[odd_cycle_vertex_list[(i+1)%k]] -= ans[odd_cycle_edge_list[i]];
		}
	}

	for (int i=0; i<n; i++) {
		assert(b[i].val() == 0);
	}

	for (int i=0; i<m; i++) {
		cout << ans[i].val() << ' ';
	}
	cout << endl;
}
0