Submission #447748

#TimeUsernameProblemLanguageResultExecution timeMemory
447748prvocisloPipes (BOI13_pipes)C++17
100 / 100
84 ms19424 KiB
#include <bits/stdc++.h> typedef long long ll; using namespace std; void end() { cout << 0 << endl; exit(0); } const int maxn = 1e5 + 5; vector<pair<int, int> > g[maxn]; int deg[maxn], vis[maxn], n[maxn], e[maxn]; ll sum[maxn*2]; vector<int> cy; void dfs(int u, int p, int st) { if (u == st && p != -1) return; sum[cy.size()] = n[u]; for (pair<int, int> v : g[u]) if (!vis[v.first] && v.first != p) { cy.push_back(v.second); dfs(v.first, u, st); if (u == st) return; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int N, M; cin >> N >> M; if (M > N) end(); for (int i = 0; i < N; i++) cin >> n[i]; for (int i = 0, u, v; i < M; i++) { cin >> u >> v; g[--u].push_back({--v, i}), g[v].push_back({u, i}); deg[u]++, deg[v]++; } vector<int> stk; for (int i = 0; i < N; i++) if (deg[i] == 1) stk.push_back(i); while (!stk.empty()) { int u = stk.back(); stk.pop_back(); vis[u] = true; for (pair<int, int> v : g[u]) if (!vis[v.first]) { e[v.second] = n[u]*2; n[v.first] -= n[u]; deg[v.first]--; if (deg[v.first] == 1) stk.push_back(v.first); } } if (N == M) // ostal nam cyklus { for (int i = 0; i < N; i++) if (!vis[i]) { dfs(i, -1, i); break; } int k = cy.size(); if (!(k&1)) end(); for (int i = k; i < 2*k; i++) sum[i] = sum[i-cy.size()]; for (int i = 0; i < 2*k; i++) if (i&1) sum[i] *= -1; for (int i = 1; i < 2*k; i++) sum[i] += sum[i-1]; for (int i = 0; i < k; i++) { e[cy[i]] = sum[i+k]-sum[i]; if (!(i&1)) e[cy[i]] *= -1; } } for (int i = 0; i < M; i++) cout << e[i] << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...