Submission #816032

#TimeUsernameProblemLanguageResultExecution timeMemory
816032QwertyPiPipes (BOI13_pipes)C++14
70.19 / 100
589 ms131072 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int MAXN = 1e5 + 11; const int MAXM = 5e5 + 11; struct edge{ int u, v, w; int exc(int x){ return u + v - x; } }; vector<edge> E; set<int> G[MAXN]; int a[MAXN], b[MAXN], c[MAXN]; int32_t main(){ cin.tie(0); cout.tie(0)->sync_with_stdio(false); int n, m; cin >> n >> m; for(int i = 1; i <= n; i++) cin >> a[i]; int s = accumulate(a + 1, a + n + 1, 0LL); if(s % 2 == 1){ cout << 0 << endl; return 0; } for(int i = 0; i < m; i++){ int u, v; cin >> u >> v; E.push_back({u, v, 0}); G[u].insert(i); G[v].insert(i); } vector<int> d1; for(int i = 1; i <= n; i++){ if(G[i].size() == 1) d1.push_back(i); } while(!d1.empty()){ int v = d1.back(); d1.pop_back(); if(G[v].empty()) continue; for(auto e : G[v]){ int u = E[e].u + E[e].v - v; G[u].erase(e); if(G[u].size() == 1) d1.push_back(u); } for(auto e : G[v]){ c[e] = (a[v] - b[v]) * 2; b[E[e].u] += c[e] / 2; b[E[e].v] += c[e] / 2; } G[v].clear(); } int node = 0, x = -1; for(int i = 1; i <= n; i++){ assert(G[i].size() != 1); if(G[i].size() == 2){ node++; x = i; }else if(G[i].size() > 2){ cout << 0 << endl; return 0; } } if(node > 0 && node % 2 == 0){ cout << 0 << endl; return 0; } if(node >= 3){ vector<int> V, paths; V.push_back(x); paths.push_back(E[*G[x].begin()].u); x = E[*G[x].begin()].u; for(int i = 1; i < node; i++){ V.push_back(x); paths.push_back(E[*G[x].begin()].exc(x) == V[i - 1] ? E[*--G[x].end()].exc(x) : E[*G[x].begin()].exc(x)); } vector<int> tmp; tmp.push_back(0); for(int i = 1; i < node; i++) tmp.push_back((a[V[i]] - b[V[i]]) * 2 - tmp.back()); int d = ((a[V[0]] - b[V[0]]) * 2 - tmp[0] - tmp[node - 1]) / 2; tmp.clear(); tmp.push_back(d); for(int i = 1; i < node; i++) tmp.push_back((a[V[i]] - b[V[i]]) * 2 - tmp.back()); for(int i = 0; i < node; i++) c[paths[i]] = tmp[i]; } for(int i = 0; i < m; i++){ cout << c[i] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...