Submission #1111252

#TimeUsernameProblemLanguageResultExecution timeMemory
1111252codexistentPipes (BOI13_pipes)C++14
74.07 / 100
289 ms131072 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, t[MAXN], en[MAXN], r[MAXM]; pair<ll, ll> e[MAXM]; vector<pair<ll, ll>> adj[MAXN]; bool v[MAXN]; int main(){ cin >> n >> m; FOR(i, 1, n) { cin >> t[i]; en[i] = 0, v[i] = false; } FOR(i, 1, m){ cin >> e[i].f >> e[i].s; adj[e[i].f].push_back({e[i].s, i}), en[e[i].f]++; adj[e[i].s].push_back({e[i].f, i}), en[e[i].s]++; r[i] = 1000000001; } if(!(m == n - 1 || m == n)) return cout << 0 << endl, 0; queue<ll> q; FOR(i, 1, n) if(en[i] == 1) q.push(i); ll d = n; while(q.size()){ ll qi = q.front(); q.pop(); if(v[qi]) continue; v[qi] = true, d--; if(en[qi] == 0) continue; for(pair<ll, ll> p : adj[qi]) if(r[p.s] == 1000000001) { r[p.s] = 2 * t[qi]; t[p.f] -= t[qi], en[p.f]--; if(!v[p.f] && en[p.f] == 1) q.push(p.f); } } if(d != 0 && d % 2 == 0) return cout << 0 << endl, 0; ll stt, ci = -1; FOR(i, 1, n) if(!v[i]) ci = i; if(ci == -1) { FOR(i, 1, m) cout << r[i] << endl; return 0; } stt = ci; stack<ll> st; ll alts = 0; do{ for(pair<ll, ll> p : adj[ci]) if(r[p.s] == 1000000001) { alts = p.s + (-1)*(alts); ci = p.f; break; } st.push(ci); }while(stt != ci); ll prvi; for(pair<ll, ll> p : adj[stt]) if(r[p.s] == 1000000001) { prvi = r[p.s] = t[stt] - alts; if(prvi % 2) return cout << 0 << endl, 0; } st.pop(); while(!st.empty()){ ll sti = st.top(); st.pop(); for(pair<ll, ll> p : adj[sti]) if(e[p.s].f == prvi || e[p.s].s == prvi) { prvi = r[p.s] = 2 * (r[sti] - prvi / 2); if(prvi % 2) return cout << 0 << endl, 0; } } FOR(i, 1, m) cout << r[i] << endl; }

Compilation message (stderr)

pipes.cpp: In function 'int main()':
pipes.cpp:78:60: warning: 'prvi' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |         for(pair<ll, ll> p : adj[sti]) if(e[p.s].f == prvi || e[p.s].s == prvi) {
      |                                           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...