Submission #594549

# Submission time Handle Problem Language Result Execution time Memory
594549 2022-07-12T16:26:47 Z bogdanvladmihai Pipes (BOI13_pipes) C++14
100 / 100
314 ms 35268 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

/***
N equations and M unknowns - can only have a unique solution if M <= N
Since M >= N - 1 =>
* the graph is a tree, in wich case we can solve the problem
* the graph is a tree with a simple cycle (N edges). Solve the problem for leafs and solve from a cycle

Solve for a cycle:
* even length: multiple solution (for each node do +1 on one edge and -1 on the other)
* odd length: unique solution, do some math stuff and deduce first edge

N = the number of nodes that are left are the process

sum(1) = W(n, 1) + W(1, 2)
sum(2) = W(1, 2) + W(2, 3)

sum(1) = W(n, 1) + sum(2) - W(2, 3)
sum(1) = W(n, 1) + sum(2) - sum(3) + sum(4) - ... - sum(N) + W(n, 1)
2 * W(n, 1) = sum(1) - sum(2) + sum(3) - sum(4) + .... + sum(N)

***/

const int MAXN = 1000 * 100;

int n, m;
set<int> g[MAXN + 1];
pair<int, int> edges[MAXN + 1];
map<pair<int, int>, int> edgeId;
int weight[MAXN + 1], sum[MAXN + 1];
bool visited[MAXN + 1];

vector<int> cycle;
void dfs(int u, int dad = -1) {
    visited[u] = true;
    cycle.push_back(u);

    for (const int &v : g[u]) {
        if (v == dad || visited[v]) {
            continue;
        }

        dfs(v, u);
    }
}

signed main() {
    cin >> n >> m;

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

    for (int i = 1; i <= n; i++) {
        cin >> sum[i];
    }
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;

        edgeId[make_pair(u, v)] = edgeId[make_pair(v, u)] = i;

        g[u].insert(v);
        g[v].insert(u);
    }

    queue<int> q;
    for (int i = 1; i <= n; i++) {
        if ((int)g[i].size() == 1) {
            q.push(i);
        }
    }

    while (!q.empty()) {
        int u = q.front();
        q.pop();

        if ((int)g[u].size() > 0) {
            int v = *g[u].begin();
            int id = edgeId[make_pair(u, v)];

            weight[id] = sum[u];
            sum[v] -= weight[id];

            g[u].erase(v);
            g[v].erase(u);
            if ((int)g[v].size() == 1) {
                q.push(v);
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        if ((int)g[i].size() > 0) {
            dfs(i);
            break;
        }
    }

    if ((int)cycle.size() > 0 && (int)cycle.size() % 2 == 0) {
        cout << "0\n";
        return 0;
    }

    if ((int)cycle.size() > 0) {
        int s = 0;
        for (int i = 0; i < (int)cycle.size(); i++) {
            if (i % 2 == 0) {
                s += sum[cycle[i]];
            } else {
                s -= sum[cycle[i]];
            }
        }

        int id = edgeId[make_pair(cycle[0], cycle.back())];

        weight[id] = s / 2;

        g[cycle[0]].erase(cycle.back());
        g[cycle.back()].erase(cycle[0]);

        sum[cycle[0]] -= weight[id];
        sum[cycle.back()] -= weight[id];

        q.push(cycle[0]);
      //  q.push(cycle.back());
        while (!q.empty()) {
            int u = q.front();
            q.pop();

            int v = *g[u].begin();
            int id = edgeId[make_pair(u, v)];

            weight[id] = sum[u];
            sum[v] -= weight[id];

            g[u].erase(v);
            g[v].erase(u);
            if ((int)g[v].size() == 1) {
                q.push(v);
            }
        }
    }

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 5 ms 5204 KB Output is correct
4 Correct 287 ms 29168 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 4 ms 5204 KB Output is correct
10 Correct 6 ms 5204 KB Output is correct
11 Correct 4 ms 5204 KB Output is correct
12 Correct 4 ms 5204 KB Output is correct
13 Correct 233 ms 24292 KB Output is correct
14 Correct 256 ms 28248 KB Output is correct
15 Correct 298 ms 29292 KB Output is correct
16 Correct 229 ms 25548 KB Output is correct
17 Correct 282 ms 29156 KB Output is correct
18 Correct 268 ms 29316 KB Output is correct
19 Correct 289 ms 28916 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 4 ms 5204 KB Output is correct
22 Correct 269 ms 29324 KB Output is correct
23 Correct 208 ms 24256 KB Output is correct
24 Correct 270 ms 29284 KB Output is correct
25 Correct 234 ms 25248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5204 KB Output is correct
3 Correct 241 ms 30624 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4960 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5296 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 4 ms 5204 KB Output is correct
23 Correct 240 ms 28288 KB Output is correct
24 Correct 314 ms 32948 KB Output is correct
25 Correct 234 ms 30500 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 278 ms 31836 KB Output is correct
31 Correct 305 ms 35268 KB Output is correct
32 Correct 278 ms 30516 KB Output is correct
33 Correct 245 ms 32776 KB Output is correct
34 Correct 3 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 289 ms 32472 KB Output is correct
39 Correct 301 ms 29812 KB Output is correct
40 Correct 292 ms 32580 KB Output is correct
41 Correct 226 ms 34780 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 2 ms 4948 KB Output is correct
44 Correct 2 ms 4948 KB Output is correct
45 Correct 3 ms 4916 KB Output is correct
46 Correct 288 ms 31892 KB Output is correct
47 Correct 303 ms 32612 KB Output is correct
48 Correct 297 ms 35092 KB Output is correct
49 Correct 245 ms 28356 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 2 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 2 ms 4948 KB Output is correct
54 Correct 291 ms 31448 KB Output is correct