Submission #995749

# Submission time Handle Problem Language Result Execution time Memory
995749 2024-06-09T21:05:14 Z MilosMilutinovic Pipes (BOI13_pipes) C++14
79.2593 / 100
99 ms 18892 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<vector<array<int, 2>>> g(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    g[x].push_back({y, i});
    g[y].push_back({x, i});
  }
  if (m > n) {
    cout << 0 << '\n';
    return 0;
  }
  if (n == 1) {
    cout << 2 * a[0] << '\n';
    return 0;
  }
  if (m < n) {
    vector<long long> res(m);
    function<void(int, int)> Dfs = [&](int v, int pv) {
      for (auto& e : g[v]) {
        int u = e[0];
        if (u == pv) {
          continue;
        }
        Dfs(u, v);
        if (a[u] != 0) {
          res[e[1]] = 2 * a[u];
          a[v] -= a[u];
        }
      }
    };
    Dfs(0, 0);
    if (a[0] != 0) {
      cout << 0 << '\n';
      return 0;
    }
    for (int i = 0; i < m; i++) {
      cout << res[i] << '\n';
    }
    return 0;
  }
  vector<int> deg(n);
  for (int i = 0; i < n; i++) {
    deg[i] = (int) g[i].size();
  }
  vector<int> que;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 1) {
      que.push_back(i);
    }
  }
  auto orig = a;
  vector<long long> res(m);
  for (int b = 0; b < (int) que.size(); b++) {
    int i = que[b];
    for (auto& e : g[i]) {
      int j = e[0];
      deg[j] -= 1;
      if (deg[j] == 1) {
        que.push_back(j);
        res[e[1]] = 2 * a[i];
        a[j] -= a[i];
        a[i] = 0;
      }
    }
  }
  vector<int> cyc;
  for (int i = 0; i < n; i++) {
    if (deg[i] > 1) {
      cyc.push_back(i);
    }
  }
  for (int i : cyc) {
    for (auto& e : g[i]) {
      int j = e[0];
      if (deg[j] < 2) {
        res[e[1]] = 2 * a[j];
        a[i] -= a[j];
        a[j] = 0;
      }
    }
  }
  if ((int) cyc.size() % 2 == 0) {
    cout << 0 << '\n';
    return 0;
  }
  vector<bool> was(n);
  vector<long long> vals;
  vector<int> ids;
  function<void(int)> Dfs = [&](int v) {
    was[v] = true;
    vals.push_back(a[v]);
    int back = -1;
    bool found = false;
    for (auto& e : g[v]) {
      int u = e[0];
      int i = e[1];
      if (u == cyc[0]) {
        back = i;
      }
      if (was[u] || deg[u] <= 1) {
        continue;
      }
      found = true;
      ids.push_back(i);
      Dfs(u);
    }
    if (!found) {
      ids.push_back(back);
    }
  }; 
  Dfs(cyc[0]);
  long long total = 0;
  for (int i = 0; i < (int) vals.size(); i++) {
    total = vals[i] - total;
  }
  if (total % 2 == 1) {
    cout << 0 << '\n';
    return 0;
  }
  total /= 2;
  res[ids.back()] = 2 * total;
  for (int i = 0; i < n; i++) {
    for (auto& p : g[i]) {
      if (p[1] == ids.back()) {
        a[i] -= total;
      }
    }
  }
  was = vector<bool>(n, false);
  Dfs = [&](int v) {
    was[v] = true;
    for (auto& p : g[v]) {
      int u = p[0];
      int i = p[1];
      if (deg[u] <= 1 || was[u] || i == ids.back()) {
        continue;
      }
      Dfs(u);
      res[i] = 2 * a[u];
      a[v] -= a[u];
      a[u] = 0;
    }
  };
  Dfs(cyc.back());
  a = orig;
  for (int i = 0; i < n; i++) {
    long long sum = 0;
    for (auto& p : g[i]) {
      sum += res[p[1]] / 2;
    }
    if (sum != a[i]) {
      cout << 0 << '\n';
      return 0;
    }
  }
  for (int i = 0; i < m; i++) {
    cout << res[i] << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 41 ms 8416 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 424 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 29 ms 6652 KB Output is correct
14 Correct 39 ms 7936 KB Output is correct
15 Correct 33 ms 8528 KB Output is correct
16 Correct 27 ms 7256 KB Output is correct
17 Correct 33 ms 8276 KB Output is correct
18 Correct 36 ms 8532 KB Output is correct
19 Correct 37 ms 12380 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 33 ms 8536 KB Output is correct
23 Correct 27 ms 6744 KB Output is correct
24 Correct 33 ms 8532 KB Output is correct
25 Correct 27 ms 7000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Incorrect 0 ms 604 KB Output isn't correct
3 Correct 27 ms 8980 KB Output is correct
4 Correct 22 ms 6844 KB Output is correct
5 Correct 23 ms 7000 KB Output is correct
6 Correct 99 ms 17204 KB Output is correct
7 Incorrect 0 ms 348 KB Output isn't correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Incorrect 0 ms 344 KB Output isn't correct
15 Incorrect 0 ms 604 KB Output isn't correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Incorrect 1 ms 348 KB Output isn't correct
23 Incorrect 28 ms 13320 KB Output isn't correct
24 Incorrect 38 ms 15112 KB Output isn't correct
25 Correct 26 ms 9040 KB Output is correct
26 Correct 25 ms 6748 KB Output is correct
27 Correct 29 ms 6492 KB Output is correct
28 Correct 29 ms 7080 KB Output is correct
29 Correct 88 ms 14672 KB Output is correct
30 Correct 43 ms 14156 KB Output is correct
31 Incorrect 40 ms 18892 KB Output isn't correct
32 Incorrect 34 ms 11468 KB Output isn't correct
33 Correct 31 ms 9364 KB Output is correct
34 Correct 24 ms 6748 KB Output is correct
35 Correct 31 ms 6760 KB Output is correct
36 Correct 23 ms 7036 KB Output is correct
37 Correct 95 ms 17232 KB Output is correct
38 Incorrect 38 ms 14156 KB Output isn't correct
39 Incorrect 29 ms 10700 KB Output isn't correct
40 Incorrect 34 ms 14676 KB Output isn't correct
41 Correct 25 ms 9420 KB Output is correct
42 Correct 24 ms 6492 KB Output is correct
43 Correct 27 ms 6748 KB Output is correct
44 Correct 22 ms 6952 KB Output is correct
45 Correct 69 ms 14412 KB Output is correct
46 Incorrect 34 ms 13652 KB Output isn't correct
47 Incorrect 45 ms 14676 KB Output isn't correct
48 Incorrect 49 ms 18372 KB Output isn't correct
49 Correct 27 ms 9168 KB Output is correct
50 Correct 23 ms 6748 KB Output is correct
51 Correct 24 ms 7000 KB Output is correct
52 Correct 22 ms 6736 KB Output is correct
53 Correct 79 ms 14676 KB Output is correct
54 Correct 40 ms 13656 KB Output is correct