답안 #490283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490283 2021-11-26T18:43:46 Z JerryLiu06 Pipes (BOI13_pipes) C++17
67.5926 / 100
66 ms 11732 KB
// 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, C[MX]; vpi adj[MX]; bool inCyc[MX]; int inDeg[MX];

bool vis[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];

            if (inDeg[Y.f] == 1) {
                Q.push(Y.f);
            }
        }
    }
    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];
    }
    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";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 58 ms 10288 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 2 ms 2632 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 40 ms 8604 KB Output is correct
14 Correct 48 ms 9748 KB Output is correct
15 Correct 49 ms 10300 KB Output is correct
16 Correct 42 ms 9020 KB Output is correct
17 Correct 66 ms 10292 KB Output is correct
18 Correct 50 ms 10184 KB Output is correct
19 Correct 50 ms 9476 KB Output is correct
20 Correct 1 ms 2636 KB Output is correct
21 Correct 2 ms 2764 KB Output is correct
22 Correct 50 ms 10308 KB Output is correct
23 Correct 42 ms 8644 KB Output is correct
24 Correct 55 ms 10288 KB Output is correct
25 Correct 40 ms 8900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2636 KB Output isn't correct
2 Incorrect 2 ms 2636 KB Output isn't correct
3 Incorrect 46 ms 9412 KB Output isn't correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 1 ms 2636 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Incorrect 1 ms 2636 KB Output isn't correct
8 Incorrect 2 ms 2636 KB Output isn't correct
9 Incorrect 1 ms 2676 KB Output isn't correct
10 Correct 1 ms 2636 KB Output is correct
11 Correct 1 ms 2584 KB Output is correct
12 Correct 1 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Incorrect 1 ms 2636 KB Output isn't correct
15 Incorrect 2 ms 2636 KB Output isn't correct
16 Incorrect 2 ms 2636 KB Output isn't correct
17 Incorrect 2 ms 2764 KB Output isn't correct
18 Correct 1 ms 2636 KB Output is correct
19 Correct 1 ms 2636 KB Output is correct
20 Correct 1 ms 2636 KB Output is correct
21 Correct 1 ms 2636 KB Output is correct
22 Incorrect 2 ms 2636 KB Output isn't correct
23 Incorrect 32 ms 8100 KB Output isn't correct
24 Incorrect 51 ms 11232 KB Output isn't correct
25 Incorrect 44 ms 9416 KB Output isn't correct
26 Correct 2 ms 2688 KB Output is correct
27 Correct 1 ms 2636 KB Output is correct
28 Correct 1 ms 2636 KB Output is correct
29 Correct 1 ms 2636 KB Output is correct
30 Incorrect 44 ms 8944 KB Output isn't correct
31 Incorrect 42 ms 9284 KB Output isn't correct
32 Incorrect 46 ms 9616 KB Output isn't correct
33 Correct 51 ms 9620 KB Output is correct
34 Correct 1 ms 2636 KB Output is correct
35 Correct 2 ms 2684 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Incorrect 48 ms 10128 KB Output isn't correct
39 Incorrect 49 ms 10076 KB Output isn't correct
40 Incorrect 52 ms 11732 KB Output isn't correct
41 Correct 36 ms 9116 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 1 ms 2688 KB Output is correct
45 Correct 2 ms 2640 KB Output is correct
46 Incorrect 47 ms 9356 KB Output isn't correct
47 Incorrect 63 ms 10968 KB Output isn't correct
48 Incorrect 43 ms 9284 KB Output isn't correct
49 Incorrect 46 ms 9676 KB Output isn't correct
50 Correct 1 ms 2636 KB Output is correct
51 Correct 1 ms 2636 KB Output is correct
52 Correct 1 ms 2636 KB Output is correct
53 Correct 2 ms 2636 KB Output is correct
54 Incorrect 46 ms 9428 KB Output isn't correct