結果
問題 |
No.3104 Simple Graph Problem
|
ユーザー |
![]() |
提出日時 | 2025-02-01 01:33:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 193 ms / 2,000 ms |
コード長 | 1,516 bytes |
コンパイル時間 | 2,843 ms |
コンパイル使用メモリ | 210,192 KB |
実行使用メモリ | 31,744 KB |
最終ジャッジ日時 | 2025-02-01 01:33:57 |
合計ジャッジ時間 | 13,245 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 65 |
ソースコード
// 軽めの実装 #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 new_ikeru(n, vector<pair<int,int>>(0)); vector<int> color(n); vector<bool> tansaku(n); vector<mint> ans(m); mint total = 0; auto dfs = [&](auto self, int i) -> void { tansaku[i] = 1; if (color[i] == 0) total += b[i]; else total -= b[i]; for (auto [j,c]: ikeru[i]) { if (tansaku[j]) continue; color[j] = color[i] ^ 1; new_ikeru[i].push_back(pair(j,c)); new_ikeru[j].push_back(pair(i,c)); self(self, j); } }; dfs(dfs, 0); for (int i=0; i<m; i++) { if (color[u[i]] == color[v[i]]) { if (color[u[i]] == 0) { ans[i] = total/2; } else { ans[i] = - total/2; } b[u[i]] -= ans[i]; b[v[i]] -= ans[i]; total = 0; break; } } if (total.val() != 0) { cout << -1 << '\n'; return 0; } auto dfs2 = [&](auto self, int i, int p) -> void { for (auto [j,c]: new_ikeru[i]) { if (j == p) continue; self(self, j, i); ans[c] = b[j]; b[j] -= ans[c]; b[i] -= ans[c]; } }; dfs2(dfs2, 0, -1); for (int i=0; i<m; i++) { cout << ans[i].val() << ' '; } cout << endl; }