Submission #995734

# Submission time Handle Problem Language Result Execution time Memory
995734 2024-06-09T20:34:00 Z MilosMilutinovic Pipes (BOI13_pipes) C++14
74.0741 / 100
92 ms 36616 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 = orig;
  vector<int> cyc;
  for (int i = 0; i < n; i++) {
    if (deg[i] > 1) {
      cyc.push_back(i);
    }
  }
  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]) {
        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;
  }
  res[ids.back()] = 2 * total;
  for (int i = 0; i < n; i++) {
    for (auto& p : g[i]) {
      if (p[2] == 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]) {
        continue;
      }
      Dfs(u);
      res[i] = 2 * a[u];
      a[v] -= a[u];
    }
  };
  Dfs(cyc.back());
  if (a != vector<long long>(n, 0LL)) {
    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 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 33 ms 8304 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 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 32 ms 6748 KB Output is correct
14 Correct 42 ms 8016 KB Output is correct
15 Correct 33 ms 8536 KB Output is correct
16 Correct 29 ms 7252 KB Output is correct
17 Correct 32 ms 8272 KB Output is correct
18 Correct 32 ms 8528 KB Output is correct
19 Correct 36 ms 12380 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 33 ms 8460 KB Output is correct
23 Correct 26 ms 6748 KB Output is correct
24 Correct 34 ms 8532 KB Output is correct
25 Correct 34 ms 6992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 6
2 Runtime error 1 ms 860 KB Execution killed with signal 6
3 Correct 26 ms 9048 KB Output is correct
4 Correct 23 ms 6744 KB Output is correct
5 Correct 25 ms 7004 KB Output is correct
6 Correct 92 ms 17268 KB Output is correct
7 Runtime error 1 ms 604 KB Execution killed with signal 6
8 Incorrect 0 ms 348 KB Output isn't 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 Runtime error 1 ms 604 KB Execution killed with signal 6
15 Runtime error 1 ms 860 KB Execution killed with signal 6
16 Incorrect 0 ms 348 KB Output isn't correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Incorrect 1 ms 604 KB Output isn't correct
23 Runtime error 41 ms 25036 KB Execution killed with signal 11
24 Incorrect 39 ms 16456 KB Output isn't correct
25 Correct 25 ms 9048 KB Output is correct
26 Correct 25 ms 6756 KB Output is correct
27 Correct 28 ms 6536 KB Output is correct
28 Correct 28 ms 7004 KB Output is correct
29 Correct 92 ms 14812 KB Output is correct
30 Runtime error 50 ms 28584 KB Execution killed with signal 6
31 Runtime error 50 ms 34960 KB Execution killed with signal 11
32 Runtime error 42 ms 24048 KB Execution killed with signal 6
33 Correct 30 ms 9428 KB Output is correct
34 Correct 27 ms 6736 KB Output is correct
35 Correct 23 ms 6820 KB Output is correct
36 Correct 26 ms 7000 KB Output is correct
37 Correct 91 ms 17180 KB Output is correct
38 Runtime error 52 ms 32780 KB Execution killed with signal 6
39 Runtime error 41 ms 21844 KB Execution killed with signal 6
40 Runtime error 47 ms 28276 KB Execution killed with signal 6
41 Correct 25 ms 9432 KB Output is correct
42 Correct 23 ms 6748 KB Output is correct
43 Correct 23 ms 6740 KB Output is correct
44 Correct 23 ms 6992 KB Output is correct
45 Correct 74 ms 14220 KB Output is correct
46 Runtime error 54 ms 36616 KB Execution killed with signal 11
47 Runtime error 46 ms 27896 KB Execution killed with signal 6
48 Runtime error 53 ms 34432 KB Execution killed with signal 6
49 Correct 26 ms 9172 KB Output is correct
50 Correct 24 ms 6868 KB Output is correct
51 Correct 23 ms 6996 KB Output is correct
52 Correct 26 ms 6744 KB Output is correct
53 Correct 81 ms 14420 KB Output is correct
54 Runtime error 48 ms 28568 KB Execution killed with signal 6