Submission #1053809

#TimeUsernameProblemLanguageResultExecution timeMemory
1053809codexistentPipes (BOI13_pipes)C++14
74.07 / 100
521 ms100176 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define MAXN 100005 #define MAXM 500005 #define FOR(i, a, b) for(ll i = a; i <= b; i++) #define f first #define s second ll n, m, w[MAXN], d[MAXM]; set<ll> adj[MAXN]; pair<ll, pair<ll, ll>> e[MAXM]; bool v[MAXM]; map<pair<ll, ll>, ll> eid; int main(){ cin >> n >> m; FOR(i, 1, n) cin >> w[i]; FOR(i, 1, m){ e[i].f = 0; cin >> e[i].s.f >> e[i].s.s; adj[e[i].s.f].insert(e[i].s.s), adj[e[i].s.s].insert(e[i].s.f); eid.insert({{min(e[i].s.f, e[i].s.s), max(e[i].s.f, e[i].s.s)}, i}); } priority_queue<pair<ll, ll>> pq; FOR(i, 1, m) { v[i] = false; e[i].f = adj[e[i].s.f].size() + adj[e[i].s.s].size() - 2; //cout << "FOR EDGE " << i << " WE HAVE " << e[i].f << " CT " << endl; for(auto j : adj[e[i].s.f]){ if(j != e[i].s.s && adj[j].size() == 2 && adj[e[i].s.s].find(j) != adj[e[i].s.s].end()){ e[i].f -= 2; } } if(e[i].f == 0){ pq.push({0, i}); } else if(adj[e[i].s.f].size() == 1 || adj[e[i].s.s].size() == 1){ pq.push({1, i}); } } while(pq.size()){ //cout << pq.top().f << " ~ " << pq.top().s << " ==> " << e[pq.top().s].f << endl; auto pqi = pq.top(); pq.pop(); if(v[pqi.s]) continue; v[pqi.s] = true; if(pqi.f == 0){ ll s = 0; for(ll i2 : adj[e[pqi.s].s.f]) s += w[i2]; for(ll i2 : adj[e[pqi.s].s.s]) s += w[i2]; d[pqi.s] = w[e[pqi.s].s.f] + w[e[pqi.s].s.s] - s; }else if(adj[e[pqi.s].s.f].size() == 1){ d[pqi.s] = w[e[pqi.s].s.f] * 2; }else{ d[pqi.s] = w[e[pqi.s].s.s] * 2; } adj[e[pqi.s].s.f].erase(e[pqi.s].s.s); adj[e[pqi.s].s.s].erase(e[pqi.s].s.f); w[e[pqi.s].s.f] -= d[pqi.s] / 2; w[e[pqi.s].s.s] -= d[pqi.s] / 2; if(adj[e[pqi.s].s.f].size() == 2){ ll a = *(adj[e[pqi.s].s.f].begin()); ll b = *next(adj[e[pqi.s].s.f].begin(), 1); if(eid.find({min(a, b), max(a, b)}) != eid.end()){ e[eid[{min(a, b), max(a, b)}]].f -= 2; if(!v[eid[{min(a, b), max(a, b)}]] && e[eid[{min(a, b), max(a, b)}]].f == 0){ pq.push({0, eid[{min(a, b), max(a, b)}]}); } } }else if(adj[e[pqi.s].s.f].size() == 1){ ll a = *(adj[e[pqi.s].s.f].begin()); if(!v[eid[{min(a, e[pqi.s].s.f), max(a, e[pqi.s].s.f)}]]){ pq.push({1, eid[{min(a, e[pqi.s].s.f), max(a, e[pqi.s].s.f)}]}); } } if(adj[e[pqi.s].s.s].size() == 2){ ll a = *(adj[e[pqi.s].s.s].begin()); ll b = *next(adj[e[pqi.s].s.s].begin()); if(eid.find({min(a, b), max(a, b)}) != eid.end()){ e[eid[{min(a, b), max(a, b)}]].f -= 2; if(!v[eid[{min(a, b), max(a, b)}]] && e[eid[{min(a, b), max(a, b)}]].f == 0){ pq.push({0, eid[{min(a, b), max(a, b)}]}); } } }else if(adj[e[pqi.s].s.s].size() == 1){ ll a = *(adj[e[pqi.s].s.s].begin()); if(!v[eid[{min(a, e[pqi.s].s.s), max(a, e[pqi.s].s.s)}]]){ pq.push({1, eid[{min(a, e[pqi.s].s.s), max(a, e[pqi.s].s.s)}]}); } } } FOR(i, 1, m) if(!v[i] || d[i] % 2 == 1){ cout << 0 << endl; return 0; } FOR(i, 1, m) cout << d[i] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...