Submission #853288

#TimeUsernameProblemLanguageResultExecution timeMemory
853288NK_Pipes (BOI13_pipes)C++17
100 / 100
58 ms16844 KiB
// Success consists of going from failure to failure without loss of enthusiasm #include <bits/stdc++.h> using namespace std; #define nl '\n' #define pb push_back #define pf push_front #define mp make_pair #define f first #define s second #define sz(x) int(x.size()) template<class T> using V = vector<T>; using pi = pair<int, int>; using vi = V<int>; using vpi = V<pi>; using ll = long long; using pl = pair<ll, ll>; using vpl = V<pl>; using vl = V<ll>; using db = long double; template<class T> using pq = priority_queue<T, V<T>, greater<T>>; const int MOD = 1e9 + 7; const ll INFL = ll(1e17); const int LG = 18; int main() { cin.tie(0)->sync_with_stdio(0); int N, M; cin >> N >> M; if (M > N) { cout << 0 << nl; exit(0-0); } // TREE OR 1 CYCLE vl A(N); for(auto& x : A) cin >> x; V<vpi> adj(N); for(int i = 0; i < M; i++) { int u, v; cin >> u >> v; --u, --v; adj[u].pb(mp(v, i)); adj[v].pb(mp(u, i)); } vl ans(M); vi vis(N); function<int(int)> fix = [&](int u) { // remove odd vis[u] = 1; for(auto v : adj[u]) if (!vis[v.f]) { ans[v.s] = fix(v.f); A[u] -= ans[v.s]; } int need = (A[u] % 2 == 0 ? 0 : 1); A[u] -= need; return need; }; if (fix(0)) { cout << 0 << nl; exit(0-0); } vi deg(N); vi seen(N); for(int u = 0; u < N; u++) for(auto v : adj[u]) { if (u > v.f) deg[u]++, deg[v.f]++; } queue<int> q; for(int u = 0; u < N; u++) if (deg[u] == 1) q.push(u); while(sz(q)) { int u = q.front(); q.pop(); seen[u] = 1; for(auto& v : adj[u]) { if (!seen[v.f]) { deg[v.f]--; ans[v.s] += A[u]; A[v.f] -= A[u]; A[u] = 0; if (deg[v.f] == 1) q.push(v.f); } } } // for(int i = 0; i < M; i++) cout << ans[i] << nl; // cout << nl; // for(int i = 0; i < N; i++) cout << A[i] << nl; // cout << nl; int len = count(begin(seen), end(seen), 0); if (len == 0) { for(int i = 0; i < M; i++) cout << ans[i] * 2LL << nl; exit(0-0); } if (len % 2 == 0) { cout << 0 << nl; exit(0-0); } vi P; function<void(int)> dfs = [&](int u) { if (sz(P) == 2*len) return; P.pb(u); for(auto& v : adj[u]) { if (seen[v.f]) continue; if (sz(P) > 1 && v.s == P[sz(P)-2]) continue; P.pb(v.s); dfs(v.f); if (sz(P) == 2*len) return; } }; int r = find(begin(seen), end(seen), 0) - begin(seen); dfs(r); int amt = 0, coef = 1; for(int i = 0; i < 2 * len; i += 2) { amt += coef * A[P[i]]; coef = -coef; } amt /= 2; for(int i = 0; i < 2 * len; i += 2) { ans[P[i+1]] += A[P[i]] - amt; amt = A[P[i]] - amt ; } for(int i = 0; i < M; i++) cout << ans[i] * 2LL << nl; exit(0-0); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...