Submission #677786

# Submission time Handle Problem Language Result Execution time Memory
677786 2023-01-04T11:15:23 Z dooompy Pipes (BOI13_pipes) C++17
74.0741 / 100
67 ms 17696 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

ll v[100005];
int deg[100005];

vector<pair<int, int>> adj[100005];
ll calc[500005];

bool seen[100005];

bool seen2[100005];
vector<int> ord;

void dfs(int node, int par = -1) {
    ord.push_back(node);
    seen2[node] = true;

    for (auto nxt : adj[node]) {
        if (deg[nxt.first] != 2) continue;
        if (nxt.first == par) continue;
        if (nxt.first == ord.front()) continue;
        if (seen2[nxt.first]) continue;
        dfs(nxt.first, node);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n, m; cin >> n >> m;

    if (m > n) {
        cout << 0 << endl;
        return 0;
    }

    for (int i = 1; i <= n; i++) cin >> v[i];
    for (int i = 1; i <= m; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back({b, i});
        adj[b].push_back({a, i});
        deg[a]++; deg[b]++;
    }

    queue<int> q;

    for (int i = 1; i <= n; i++) {
        if (deg[i] == 1) {
            q.push(i);
        }
    }

    while (!q.empty()) {
        // deg 1
        auto cur = q.front(); q.pop();
        seen[cur] = true;
        deg[cur] = 0;

        for (auto to : adj[cur]) {
            if (seen[to.first]) continue;
            ll weight = v[cur];

            v[to.first] -= weight;
            calc[to.second] = weight;

            if (--deg[to.first] == 1) {
                q.push(to.first);
            }
        }
    }

    if (m == n - 1) {
        for (int i = 1; i <= m; i++) {
            cout << calc[i] * 2 << "\n";
        }
        return 0;
    }

    vector<int> left;

    for (int i = 1; i <= n; i++) {
        if (deg[i] == 2) left.push_back(i);
    }

    if (left.size() % 2 == 0) {
        cout << 0 << endl;
        return 0;
    }

    if (left.empty()) {
        // wtf
        cout << 0 << endl;
        return 0;
    }

    dfs(left.front());

    ll prev = 0;

    for (int i = 0; i < ord.size() - 1; i++) {
        // assum back ->0 is X

        ll now = v[ord[i]] - prev;

        prev = now;
    }

    ll x = (v[ord[0]] - prev) / 2;

//    cout << x << endl;

    ll orig = x;

    for (int i = 0; i < ord.size() - 1; i++) {
        for (auto nxt : adj[ord[i]]) {
            if (nxt.first == ord[i + 1]) {
                calc[nxt.second] = v[ord[i]] - x;
                x = calc[nxt.second];
                break;
            }
        }
    }

    for (auto nxt : adj[ord[0]]) {
        if (nxt.first == ord.back()) {
            calc[nxt.second] = orig;
            break;
        }
    }

    for (int i = 1; i <= m; i++) {
        cout << calc[i] * 2 << "\n";
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:131:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     for (int i = 0; i < ord.size() - 1; i++) {
      |                     ~~^~~~~~~~~~~~~~~~
pipes.cpp:145:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for (int i = 0; i < ord.size() - 1; i++) {
      |                     ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 55 ms 10544 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2696 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 40 ms 9020 KB Output is correct
14 Correct 50 ms 10200 KB Output is correct
15 Correct 55 ms 10820 KB Output is correct
16 Correct 46 ms 9428 KB Output is correct
17 Correct 55 ms 10572 KB Output is correct
18 Correct 53 ms 10688 KB Output is correct
19 Correct 63 ms 9888 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 57 ms 10608 KB Output is correct
23 Correct 44 ms 9036 KB Output is correct
24 Correct 53 ms 10712 KB Output is correct
25 Correct 42 ms 9296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Incorrect 2 ms 2772 KB Output isn't correct
3 Correct 43 ms 9852 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Incorrect 2 ms 2644 KB Output isn't correct
8 Incorrect 2 ms 2644 KB Output isn't correct
9 Correct 1 ms 2688 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2692 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Incorrect 2 ms 2644 KB Output isn't correct
15 Incorrect 2 ms 2772 KB Output isn't correct
16 Incorrect 2 ms 2772 KB Output isn't correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2704 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Incorrect 2 ms 2772 KB Output isn't correct
23 Incorrect 47 ms 13380 KB Output isn't correct
24 Incorrect 60 ms 14936 KB Output isn't correct
25 Correct 39 ms 9880 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2700 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Incorrect 63 ms 13780 KB Output isn't correct
31 Incorrect 65 ms 17696 KB Output isn't correct
32 Incorrect 54 ms 12080 KB Output isn't correct
33 Correct 42 ms 10116 KB Output is correct
34 Correct 2 ms 2704 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2708 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Incorrect 60 ms 14200 KB Output isn't correct
39 Incorrect 53 ms 11524 KB Output isn't correct
40 Incorrect 61 ms 14580 KB Output isn't correct
41 Correct 45 ms 10060 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Incorrect 58 ms 13564 KB Output isn't correct
47 Incorrect 64 ms 14788 KB Output isn't correct
48 Incorrect 67 ms 17460 KB Output isn't correct
49 Correct 40 ms 9720 KB Output is correct
50 Correct 1 ms 2644 KB Output is correct
51 Correct 1 ms 2696 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2652 KB Output is correct
54 Incorrect 59 ms 13428 KB Output isn't correct