結果
問題 |
No.3104 Simple Graph Problem
|
ユーザー |
![]() |
提出日時 | 2025-02-01 01:24:15 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 137 ms / 2,000 ms |
コード長 | 4,292 bytes |
コンパイル時間 | 5,055 ms |
コンパイル使用メモリ | 262,432 KB |
実行使用メモリ | 18,028 KB |
最終ジャッジ日時 | 2025-02-01 01:24:30 |
合計ジャッジ時間 | 13,405 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 65 |
ソースコード
/** author: shobonvip created: 2025.02.01 00:32:28 **/ #include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<mint> b(n); rep(i,0,n) { int x; cin >> x; b[i] = x; } vector<int> u(m), v(m); vector ikeru(n, vector<pair<int,int>>(0)); rep(i,0,m) { 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); vector<int> mada; vector<bool> tansaku(n); tansaku[0] = 1; mada.push_back(0); while(!mada.empty()){ int i = mada.back(); mada.pop_back(); for (auto [j,c]:ikeru[i]){ if (!tansaku[j]){ tansaku[j] = 1; color[j] = color[i] ^ 1; mada.push_back(j); } } } int cycle_ind = -1; vector<int> cycle_v; vector<int> cycle_e; rep(i,0,m) { if (color[u[i]] == color[v[i]]) { cycle_ind = i; break; } } bool is_bipartite = 1; if (cycle_ind >= 0) { is_bipartite = 0; int x = u[cycle_ind]; int y = v[cycle_ind]; vector<int> cf(n, -1); fill(all(tansaku), 0); tansaku[x] = 1; mada.push_back(x); while(!mada.empty()){ int i = mada.back(); mada.pop_back(); for (auto [j,c]: ikeru[i]) { if (tansaku[j]) continue; if (color[j] == color[i]) continue; tansaku[j] = 1; cf[j] = c; mada.push_back(j); } } int i = y; cycle_e.push_back(cycle_ind); cycle_v.push_back(i); while(cf[i] != -1) { cycle_e.push_back(cf[i]); i = i ^ u[cf[i]] ^ v[cf[i]]; cycle_v.push_back(i); } reverse(all(cycle_e)); reverse(all(cycle_v)); } vector new_ikeru(n, vector<pair<int,int>>(0)); for (int i: cycle_e) { new_ikeru[u[i]].push_back(pair(v[i], i)); new_ikeru[v[i]].push_back(pair(u[i], i)); } fill(all(tansaku), 0); if (is_bipartite) { tansaku[0] = 1; mada.push_back(0); }else{ for (int i: cycle_v) { tansaku[i] = 1; mada.push_back(i); } } while(!mada.empty()) { int i = mada.back(); mada.pop_back(); for (auto [j,c]: ikeru[i]) { if (tansaku[j]) continue; new_ikeru[i].push_back(pair(j, c)); new_ikeru[j].push_back(pair(i, c)); tansaku[j] = 1; mada.push_back(j); } } fill(all(tansaku), 0); vector<int> deg(n); rep(i,0,n) { deg[i] = (int)new_ikeru[i].size(); } rep(i,0,n) { if (deg[i] == 1) { mada.push_back(i); } } vector<mint> ans(m); while(!mada.empty()) { int i = mada.back(); mada.pop_back(); tansaku[i] = 1; for (auto [j,c]: new_ikeru[i]) { if (tansaku[j]) continue; ans[c] = b[i]; b[i] -= ans[c]; b[j] -= ans[c]; deg[j] -= 1; if (deg[j] == 1) { mada.push_back(j); } } } if (!is_bipartite) { mint total = 0; for (ll i: cycle_v) { total += b[i]; } total /= 2; mint tmp = total; for (int i=2; i<(int)cycle_v.size(); i+=2){ tmp -= b[cycle_v[i]]; } ans[cycle_e[0]] = tmp; rep(i,1,(int)cycle_e.size()) { ans[cycle_e[i]] = b[cycle_v[i]] - ans[cycle_e[i-1]]; } for (int i: cycle_e) { b[u[i]] -= ans[i]; b[v[i]] -= ans[i]; } } rep(i,0,n) { //cout << i << ' ' << b[i].val() << endl; if (b[i].val() != 0) { cout << -1 << '\n'; return 0; } } rep(i,0,m) { cout << ans[i].val(); if (i==m-1) cout << '\n'; else cout << ' '; } }