This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/BOI13_pipes
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(0);
#define FASTIOF ios_base::sync_with_stdio(false); fin.tie(0);
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
const int MOD = 1e9 + 7;
const int MX = 1e5 + 10;
const ll INF = 1e18;
int N, M; ll C[MX]; vpi adj[MX]; bool inCyc[MX]; int inDeg[MX]; ll ans[MX];
int main() {
FASTIO;
cin >> N >> M;
if (M > N) {
return cout << "0", 0;
}
FOR(i, 1, N + 1) {
cin >> C[i];
}
FOR(i, 1, M + 1) {
int A, B; cin >> A >> B;
adj[A].pb({B, i});
adj[B].pb({A, i});
inDeg[A]++;
inDeg[B]++;
}
FOR(i, 1, N + 1) inCyc[i] = true;
queue<int> Q;
FOR(i, 1, N + 1) {
if (inDeg[i] == 1) {
Q.push(i);
}
}
while (!Q.empty()) {
int cur = Q.front(); Q.pop();
inCyc[cur] = false;
EACH(Y, adj[cur]) {
if (!inCyc[Y.f]) continue;
ans[Y.s] = C[cur];
inDeg[Y.f]--;
C[Y.f] -= ans[Y.s];
adj[Y.f].erase(find(all(adj[Y.f]), pi{cur, Y.s}));
if (inDeg[Y.f] == 1) {
Q.push(Y.f);
}
}
}
// cout << ans[11] << "HI " << "\n";
if (M == N - 1) {
FOR(i, 1, M + 1) {
cout << 2 * ans[i] << "\n";
}
return 0;
}
vi cyc, edges; int curNode = -1;
FOR(i, 1, N + 1) {
if (inCyc[i]) {
curNode = i;
edges.pb(adj[i][0].s);
break;
}
}
while (inCyc[curNode]) {
inCyc[curNode] = false;
cyc.pb(curNode);
EACH(Y, adj[curNode]) {
if (Y.s != edges.bk) {
curNode = Y.f;
edges.pb(Y.s);
break;
}
}
}
edges.pop_back();
N = sz(cyc);
if (N % 2 == 0) {
return cout << "0", 0;
}
ll sum = 0;
EACH(i, cyc) {
sum += C[i];
}
cout << sum << "\n";
sum /= 2;
vpi newCyc; int ind = 0;
while (sz(newCyc) < N) {
newCyc.pb({cyc[ind], ind});
ind = (ind + 2) % N;
}
F0R(i, N) {
newCyc.pb(newCyc[i]);
}
vl pref(sz(newCyc) + 1, 0);
FOR(i, 1, sz(newCyc) + 1) {
pref[i] = pref[i - 1] + C[newCyc[i - 1].f];
}
int numForward = (N - 1) / 2;
F0R(i, N) {
int curEdge = edges[(newCyc[i].s + N - 1) % N];
ans[curEdge] = sum - (pref[i + numForward] - pref[i]);
}
FOR(i, 1, M + 1) {
cout << 2 * ans[i] << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |