Submission #1077640

# Submission time Handle Problem Language Result Execution time Memory
1077640 2024-08-27T08:28:36 Z ShadowShark Pipes (BOI13_pipes) C++17
100 / 100
135 ms 19404 KB
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e5 + 5;
long long c[maxN];
vector<pair<int, int>> adj[maxN];
int n, m;

namespace sub1 {
    long long ans[maxN];

    void dfs(int u, int pre) {
        for (auto [v, id]: adj[u]) {
            if (v == pre) continue;
            dfs(v, u);
            ans[id] += 2 * c[v];
            c[u] -= c[v];
        }
    }

    void solve() {
        dfs(1, 1);

        for (int i = 1; i <= m; i++)
            cout << ans[i] << '\n';
    }
}

namespace even_cycle {
    bool even_exist = false; int color[maxN];

    void dfs(int u, int pre) {
        for (auto [v, id]: adj[u]) {
            if (v == pre) continue;
            if (color[v]) {
                if (color[v] != color[u]) even_exist = true;
                continue;
            }
            color[v] = 3 - color[u];
            dfs(v, u);
        }
    }

    bool check() {
        color[1] = 1;
        dfs(1, 1);
        if (even_exist) return true;
        return false;
    }
}

namespace sub2 {
    long long ans[maxN];
    bool visited[maxN], cycle[maxN];
    int nxt[maxN], par[maxN];
    vector<int> edge, node;

    bool dfs_cycle(int u, int pre) {
        visited[u] = true;
        for (auto [v, id]: adj[u]) {
            if (v == pre) continue;
            if (visited[v]) {
                int x = u;
                edge.push_back(id);
                node.push_back(u);
                cycle[u] = true;
                do {
                    edge.push_back(nxt[x]);
                    node.push_back(par[x]);
                    cycle[par[x]] = true;
                    x = par[x];
                } while (x != v);
                return true;
            }
            par[v] = u;
            nxt[v] = id;
            if (dfs_cycle(v, u)) return true;
        }
        return false;
    }

    void dfs_cal(int u, int pre) {
        for (auto [v, id]: adj[u]) {
            if (v == pre || cycle[v]) continue;
            dfs_cal(v, u);
            ans[id] = 2 * c[v];
            c[u] -= c[v];
        }
    }

    void solve() {
        dfs_cycle(1, 1);

        for (int i = 1; i <= n; i++)
            if (cycle[i]) dfs_cal(i, i);

        long long res = 0;
        for (int i = 0; i < node.size(); i++)
            if (i % 2) res = res - c[node[i]];
                else res = res + c[node[i]];

        for (int i = 0; i < edge.size(); i++) {
            ans[edge[i]] = res;
            res -= 2 * c[node[i]];
            res = res * -1;
        }

        for (int i = 1; i <= m; i++)
            cout << ans[i] << '\n';
    }
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;

        adj[u].push_back({v, i});
        adj[v].push_back({u, i});
    }

    if (m == n - 1) sub1::solve();
        else if (m > n || even_cycle::check()) {
            cout << 0;
            return 0;
        }
        else sub2::solve();

    return 0;
}

Compilation message

pipes.cpp: In function 'void sub2::solve()':
pipes.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         for (int i = 0; i < node.size(); i++)
      |                         ~~^~~~~~~~~~~~~
pipes.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for (int i = 0; i < edge.size(); i++) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5468 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5464 KB Output is correct
4 Correct 38 ms 10320 KB Output is correct
5 Correct 2 ms 5464 KB Output is correct
6 Correct 1 ms 5468 KB Output is correct
7 Correct 1 ms 5468 KB Output is correct
8 Correct 1 ms 5468 KB Output is correct
9 Correct 1 ms 5468 KB Output is correct
10 Correct 1 ms 5468 KB Output is correct
11 Correct 1 ms 5468 KB Output is correct
12 Correct 1 ms 5468 KB Output is correct
13 Correct 27 ms 9316 KB Output is correct
14 Correct 33 ms 10068 KB Output is correct
15 Correct 42 ms 10324 KB Output is correct
16 Correct 27 ms 9556 KB Output is correct
17 Correct 35 ms 10320 KB Output is correct
18 Correct 36 ms 10320 KB Output is correct
19 Correct 33 ms 13652 KB Output is correct
20 Correct 1 ms 5468 KB Output is correct
21 Correct 1 ms 5464 KB Output is correct
22 Correct 31 ms 10452 KB Output is correct
23 Correct 28 ms 9488 KB Output is correct
24 Correct 33 ms 10448 KB Output is correct
25 Correct 25 ms 9552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5468 KB Output is correct
2 Correct 2 ms 5468 KB Output is correct
3 Correct 28 ms 12124 KB Output is correct
4 Correct 23 ms 7556 KB Output is correct
5 Correct 22 ms 7772 KB Output is correct
6 Correct 100 ms 18000 KB Output is correct
7 Correct 1 ms 5468 KB Output is correct
8 Correct 1 ms 5468 KB Output is correct
9 Correct 1 ms 5468 KB Output is correct
10 Correct 1 ms 3420 KB Output is correct
11 Correct 1 ms 3352 KB Output is correct
12 Correct 1 ms 3416 KB Output is correct
13 Correct 1 ms 3420 KB Output is correct
14 Correct 1 ms 5576 KB Output is correct
15 Correct 1 ms 5468 KB Output is correct
16 Correct 1 ms 5468 KB Output is correct
17 Correct 1 ms 5468 KB Output is correct
18 Correct 1 ms 3420 KB Output is correct
19 Correct 1 ms 3372 KB Output is correct
20 Correct 1 ms 3420 KB Output is correct
21 Correct 1 ms 3420 KB Output is correct
22 Correct 1 ms 5468 KB Output is correct
23 Correct 35 ms 14688 KB Output is correct
24 Correct 42 ms 15832 KB Output is correct
25 Correct 27 ms 12124 KB Output is correct
26 Correct 25 ms 7380 KB Output is correct
27 Correct 28 ms 7260 KB Output is correct
28 Correct 25 ms 7772 KB Output is correct
29 Correct 85 ms 15264 KB Output is correct
30 Correct 48 ms 18556 KB Output is correct
31 Correct 63 ms 19108 KB Output is correct
32 Correct 43 ms 12372 KB Output is correct
33 Correct 31 ms 13144 KB Output is correct
34 Correct 40 ms 7504 KB Output is correct
35 Correct 24 ms 7508 KB Output is correct
36 Correct 25 ms 7764 KB Output is correct
37 Correct 135 ms 18004 KB Output is correct
38 Correct 42 ms 15312 KB Output is correct
39 Correct 36 ms 11548 KB Output is correct
40 Correct 41 ms 15312 KB Output is correct
41 Correct 37 ms 14940 KB Output is correct
42 Correct 25 ms 7248 KB Output is correct
43 Correct 35 ms 7248 KB Output is correct
44 Correct 26 ms 7624 KB Output is correct
45 Correct 89 ms 16220 KB Output is correct
46 Correct 60 ms 19404 KB Output is correct
47 Correct 52 ms 15588 KB Output is correct
48 Correct 67 ms 18700 KB Output is correct
49 Correct 28 ms 10500 KB Output is correct
50 Correct 44 ms 6740 KB Output is correct
51 Correct 36 ms 6912 KB Output is correct
52 Correct 29 ms 7252 KB Output is correct
53 Correct 84 ms 15372 KB Output is correct
54 Correct 43 ms 16848 KB Output is correct