제출 #253849

#제출 시각아이디문제언어결과실행 시간메모리
253849aZvezdaPipes (BOI13_pipes)C++14
74.07 / 100
252 ms31096 KiB
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize ("O3") //#pragma GCC target ("sse4") #define endl "\n" typedef long long ll; typedef long double ld; typedef unsigned long long ull; template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const ll mod = 1e9 + 7; template<class T> inline void fix(T &x) {if(x >= mod | x <= -mod) {x %= mod;} if(x < 0) {x += mod;}} #define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl //If we have an even cycle then we can add 1 on odd edges and -1 on even edges so the solutions stay the same. We can remove leaves one by one const ll MAX_N = 1e5 + 10; set<pair<ll, ll> > g[MAX_N]; ll ans[MAX_N], arr[MAX_N]; struct cmp { bool operator ()(const ll &a, const ll &b) const { if(g[a].size() == g[b].size()) { return a < b; } return g[a].size() < g[b].size(); } }; multiset<ll, cmp> pq; void removeBranches() { while(!pq.empty() && g[*(pq.begin())].size() == 1) { ll curr = *(pq.begin()); pq.erase(pq.begin()); auto other = *(g[curr].begin()); pq.erase(other.first); ans[other.second] = 2 * arr[curr]; arr[other.first] -= arr[curr]; g[other.first].erase({curr, other.second}); pq.insert(other.first); } } int rret = 0; void dfs(int x, int p, int start, int flag) { rret += arr[x] * flag; //cout << x << " " << p << " " << ans[x] << " " << rret << endl; for(auto it : g[x]) { if(it.first == p || it.first == start) {continue;} dfs(it.first, x, start, -flag); } } void removeCycle() { if(pq.size() < 2) {return;} auto now = pq.begin(); if(g[*now].size() > 2) { cout << 0 << endl; exit(0); } now = pq.end(); now --; if(g[*now].size() > 2) { cout << 0 << endl; exit(0); } if(pq.size() % 2 == 0) { cout << 0 << endl; exit(0); } int curr = *(pq.begin()); dfs(curr, (*(g[curr].begin())).first, curr, 1); rret /= 2; auto sec = (g[curr].begin()); arr[curr] -= rret; arr[(*sec).first] -= rret; ans[(*sec).second] = rret; pq.erase(curr); pq.erase((*sec).first); g[curr].erase(sec); g[(*sec).first].erase({curr, (*sec).second}); pq.insert(curr); pq.insert((*sec).first); //cout << curr << " " << (*sec).first << " " << rret << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; if(m > n) {cout << 0 << endl; return 0;} for(ll i = 1; i <= n; i ++) { cin >> arr[i]; } for(ll i = 0; i < m; i ++) { ll a, b; cin >> a >> b; g[a].insert({b, i}); g[b].insert({a, i}); } for(ll i = 1; i <= n; i ++) { pq.insert(i); } removeBranches(); removeCycle(); removeBranches(); for(ll i = 0; i < m; i ++) { cout << ans[i] << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...