Submission #821692

# Submission time Handle Problem Language Result Execution time Memory
821692 2023-08-11T13:15:31 Z tch1cherin Graph (BOI20_graph) C++17
58 / 100
700 ms 24276 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 5;
vector<pair<int, int>> G[MAX_N];
int color[MAX_N];
vector<int> comp, path, path_edges, cycle, cycle_edges;
bool visited[MAX_N] = {};
long double A[MAX_N];

void DFS(int u) {
  comp.push_back(u);
  for (auto [v, c] : G[u]) {
    if (color[v] == -1) {
      color[v] = !color[u];
      DFS(v);
    }
  }
}

bool find_cycle(int u) {
  visited[u] = true;
  path.push_back(u);
  for (auto [v, c] : G[u]) {
    if (!visited[v]) {
      path_edges.push_back(c);
      if (find_cycle(v)) {
        return true;
      }
      path_edges.pop_back();
    } else if (color[u] == color[v]) {
      path_edges.push_back(c);
      while (path.back() != v) {
        cycle.push_back(path.back());
        cycle_edges.push_back(path_edges.back());
        path.pop_back();
        path_edges.pop_back();
      }
      cycle.push_back(path.back());
      cycle_edges.push_back(path_edges.back());
      return true;
    }
  }
  path.pop_back();
  return false;
}

long double check(int s, long double value) {
  for (int v : comp) {
    visited[v] = false;
  }
  A[s] = value;
  queue<int> Q;
  Q.push(s);
  visited[s] = true;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (auto [v, c] : G[u]) {
      if (!visited[v]) {
        A[v] = c - A[u];
        visited[v] = true;
        Q.push(v);
      } else if (abs(A[u] + A[v] - c) > 1e-15l) {
        cout << "NO\n";
        exit(0);
      }
    }
  }
  long double ans = 0;
  for (int v : comp) {
    ans += abs(A[v]);
  }
  return ans;
}

void solve(int u) {
  cycle = cycle_edges = path = path_edges = vector<int>();
  if (find_cycle(u)) {
    long double sum = 0;
    rotate(cycle_edges.begin(), cycle_edges.begin() + 1, cycle_edges.end());
    for (int i = 0; i < (int)cycle_edges.size(); i++) {
      sum += cycle_edges[i] * (i % 2 ? -1 : +1);
    }
    check(cycle[0], sum /= 2);
  } else {
    long double l = -1e6, r = 1e6;
    for (int i = 0; i < 100; i++) {
      long double m1 = l + (r - l) / 3;
      long double m2 = r - (r - l) / 3;
      if (check(u, m1) < check(u, m2)) {
        r = m2;
      } else {
        l = m1;
      }
    }
    check(u, (l + r) / 2);
  }
}

int main() {
  cout << fixed << setprecision(9);
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back(pair {b, c});
    G[b].push_back(pair {a, c});
  }
  memset(color, -1, sizeof color);
  for (int i = 0; i < N; i++) {
    if (color[i] == -1) {
      comp = vector<int>();
      color[i] = 0;
      DFS(i);
      solve(i);
    }
  }
  cout << "YES\n";
  for (int i = 0; i < N; i++) {
    cout << A[i] << " \n"[i + 1 == N];
  }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB answer = YES
2 Correct 1 ms 3028 KB answer = YES
3 Correct 2 ms 3028 KB answer = YES
4 Correct 1 ms 3028 KB answer = NO
5 Correct 2 ms 3064 KB answer = YES
6 Correct 2 ms 3068 KB answer = YES
7 Correct 2 ms 3028 KB answer = YES
8 Correct 2 ms 3028 KB answer = YES
9 Correct 2 ms 3028 KB answer = NO
10 Correct 1 ms 3068 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 1 ms 3028 KB answer = NO
13 Correct 2 ms 3028 KB answer = YES
14 Correct 2 ms 3028 KB answer = YES
15 Correct 2 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 2 ms 3028 KB answer = YES
18 Correct 2 ms 3068 KB answer = YES
19 Correct 2 ms 3028 KB answer = YES
20 Correct 2 ms 3064 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 2 ms 3028 KB answer = NO
23 Correct 2 ms 3028 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB answer = YES
2 Correct 1 ms 3028 KB answer = YES
3 Correct 2 ms 3028 KB answer = YES
4 Correct 1 ms 3028 KB answer = NO
5 Correct 2 ms 3064 KB answer = YES
6 Correct 2 ms 3068 KB answer = YES
7 Correct 2 ms 3028 KB answer = YES
8 Correct 2 ms 3028 KB answer = YES
9 Correct 2 ms 3028 KB answer = NO
10 Correct 1 ms 3068 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 1 ms 3028 KB answer = NO
13 Correct 2 ms 3028 KB answer = YES
14 Correct 2 ms 3028 KB answer = YES
15 Correct 2 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 2 ms 3028 KB answer = YES
18 Correct 2 ms 3068 KB answer = YES
19 Correct 2 ms 3028 KB answer = YES
20 Correct 2 ms 3064 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 2 ms 3028 KB answer = NO
23 Correct 2 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 2 ms 3028 KB answer = YES
26 Correct 2 ms 3064 KB answer = YES
27 Correct 2 ms 3028 KB answer = YES
28 Correct 1 ms 3028 KB answer = YES
29 Correct 2 ms 3028 KB answer = YES
30 Correct 1 ms 3028 KB answer = NO
31 Correct 2 ms 3028 KB answer = YES
32 Correct 1 ms 3028 KB answer = YES
33 Correct 2 ms 3028 KB answer = YES
34 Correct 2 ms 3028 KB answer = YES
35 Correct 2 ms 3028 KB answer = YES
36 Correct 2 ms 3028 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB answer = YES
2 Correct 1 ms 3028 KB answer = YES
3 Correct 2 ms 3028 KB answer = YES
4 Correct 1 ms 3028 KB answer = NO
5 Correct 2 ms 3064 KB answer = YES
6 Correct 2 ms 3068 KB answer = YES
7 Correct 2 ms 3028 KB answer = YES
8 Correct 2 ms 3028 KB answer = YES
9 Correct 2 ms 3028 KB answer = NO
10 Correct 1 ms 3068 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 1 ms 3028 KB answer = NO
13 Correct 2 ms 3028 KB answer = YES
14 Correct 2 ms 3028 KB answer = YES
15 Correct 2 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 2 ms 3028 KB answer = YES
18 Correct 2 ms 3068 KB answer = YES
19 Correct 2 ms 3028 KB answer = YES
20 Correct 2 ms 3064 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 2 ms 3028 KB answer = NO
23 Correct 2 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 2 ms 3028 KB answer = YES
26 Correct 2 ms 3064 KB answer = YES
27 Correct 2 ms 3028 KB answer = YES
28 Correct 1 ms 3028 KB answer = YES
29 Correct 2 ms 3028 KB answer = YES
30 Correct 1 ms 3028 KB answer = NO
31 Correct 2 ms 3028 KB answer = YES
32 Correct 1 ms 3028 KB answer = YES
33 Correct 2 ms 3028 KB answer = YES
34 Correct 2 ms 3028 KB answer = YES
35 Correct 2 ms 3028 KB answer = YES
36 Correct 2 ms 3028 KB answer = YES
37 Correct 3 ms 3028 KB answer = YES
38 Correct 2 ms 3028 KB answer = YES
39 Correct 3 ms 3028 KB answer = YES
40 Correct 2 ms 3156 KB answer = YES
41 Correct 2 ms 3156 KB answer = NO
42 Correct 5 ms 3156 KB answer = YES
43 Correct 5 ms 3156 KB answer = YES
44 Correct 4 ms 3156 KB answer = YES
45 Correct 2 ms 3156 KB answer = YES
46 Correct 3 ms 3028 KB answer = YES
47 Correct 2 ms 3076 KB answer = YES
48 Correct 2 ms 3156 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB answer = YES
2 Correct 1 ms 3028 KB answer = YES
3 Correct 2 ms 3028 KB answer = YES
4 Correct 1 ms 3028 KB answer = NO
5 Correct 2 ms 3064 KB answer = YES
6 Correct 2 ms 3068 KB answer = YES
7 Correct 2 ms 3028 KB answer = YES
8 Correct 2 ms 3028 KB answer = YES
9 Correct 2 ms 3028 KB answer = NO
10 Correct 1 ms 3068 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 1 ms 3028 KB answer = NO
13 Correct 2 ms 3028 KB answer = YES
14 Correct 2 ms 3028 KB answer = YES
15 Correct 2 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 2 ms 3028 KB answer = YES
18 Correct 2 ms 3068 KB answer = YES
19 Correct 2 ms 3028 KB answer = YES
20 Correct 2 ms 3064 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 2 ms 3028 KB answer = NO
23 Correct 2 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 2 ms 3028 KB answer = YES
26 Correct 2 ms 3064 KB answer = YES
27 Correct 2 ms 3028 KB answer = YES
28 Correct 1 ms 3028 KB answer = YES
29 Correct 2 ms 3028 KB answer = YES
30 Correct 1 ms 3028 KB answer = NO
31 Correct 2 ms 3028 KB answer = YES
32 Correct 1 ms 3028 KB answer = YES
33 Correct 2 ms 3028 KB answer = YES
34 Correct 2 ms 3028 KB answer = YES
35 Correct 2 ms 3028 KB answer = YES
36 Correct 2 ms 3028 KB answer = YES
37 Correct 3 ms 3028 KB answer = YES
38 Correct 2 ms 3028 KB answer = YES
39 Correct 3 ms 3028 KB answer = YES
40 Correct 2 ms 3156 KB answer = YES
41 Correct 2 ms 3156 KB answer = NO
42 Correct 5 ms 3156 KB answer = YES
43 Correct 5 ms 3156 KB answer = YES
44 Correct 4 ms 3156 KB answer = YES
45 Correct 2 ms 3156 KB answer = YES
46 Correct 3 ms 3028 KB answer = YES
47 Correct 2 ms 3076 KB answer = YES
48 Correct 2 ms 3156 KB answer = YES
49 Correct 55 ms 3944 KB answer = YES
50 Correct 8 ms 4308 KB answer = YES
51 Correct 38 ms 4236 KB answer = YES
52 Correct 4 ms 4180 KB answer = NO
53 Correct 5 ms 3156 KB answer = YES
54 Correct 10 ms 3288 KB answer = YES
55 Correct 22 ms 3408 KB answer = YES
56 Correct 52 ms 3816 KB answer = YES
57 Correct 43 ms 3720 KB answer = YES
58 Correct 36 ms 3724 KB answer = YES
59 Correct 7 ms 3796 KB answer = YES
60 Correct 48 ms 3892 KB answer = YES
61 Correct 5 ms 3412 KB answer = YES
62 Correct 42 ms 10536 KB answer = NO
63 Correct 44 ms 10572 KB answer = YES
64 Correct 44 ms 10572 KB answer = NO
65 Correct 45 ms 10572 KB answer = YES
66 Correct 8 ms 3156 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB answer = YES
2 Correct 1 ms 3028 KB answer = YES
3 Correct 2 ms 3028 KB answer = YES
4 Correct 1 ms 3028 KB answer = NO
5 Correct 2 ms 3064 KB answer = YES
6 Correct 2 ms 3068 KB answer = YES
7 Correct 2 ms 3028 KB answer = YES
8 Correct 2 ms 3028 KB answer = YES
9 Correct 2 ms 3028 KB answer = NO
10 Correct 1 ms 3068 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 1 ms 3028 KB answer = NO
13 Correct 2 ms 3028 KB answer = YES
14 Correct 2 ms 3028 KB answer = YES
15 Correct 2 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 2 ms 3028 KB answer = YES
18 Correct 2 ms 3068 KB answer = YES
19 Correct 2 ms 3028 KB answer = YES
20 Correct 2 ms 3064 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 2 ms 3028 KB answer = NO
23 Correct 2 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 2 ms 3028 KB answer = YES
26 Correct 2 ms 3064 KB answer = YES
27 Correct 2 ms 3028 KB answer = YES
28 Correct 1 ms 3028 KB answer = YES
29 Correct 2 ms 3028 KB answer = YES
30 Correct 1 ms 3028 KB answer = NO
31 Correct 2 ms 3028 KB answer = YES
32 Correct 1 ms 3028 KB answer = YES
33 Correct 2 ms 3028 KB answer = YES
34 Correct 2 ms 3028 KB answer = YES
35 Correct 2 ms 3028 KB answer = YES
36 Correct 2 ms 3028 KB answer = YES
37 Correct 3 ms 3028 KB answer = YES
38 Correct 2 ms 3028 KB answer = YES
39 Correct 3 ms 3028 KB answer = YES
40 Correct 2 ms 3156 KB answer = YES
41 Correct 2 ms 3156 KB answer = NO
42 Correct 5 ms 3156 KB answer = YES
43 Correct 5 ms 3156 KB answer = YES
44 Correct 4 ms 3156 KB answer = YES
45 Correct 2 ms 3156 KB answer = YES
46 Correct 3 ms 3028 KB answer = YES
47 Correct 2 ms 3076 KB answer = YES
48 Correct 2 ms 3156 KB answer = YES
49 Correct 55 ms 3944 KB answer = YES
50 Correct 8 ms 4308 KB answer = YES
51 Correct 38 ms 4236 KB answer = YES
52 Correct 4 ms 4180 KB answer = NO
53 Correct 5 ms 3156 KB answer = YES
54 Correct 10 ms 3288 KB answer = YES
55 Correct 22 ms 3408 KB answer = YES
56 Correct 52 ms 3816 KB answer = YES
57 Correct 43 ms 3720 KB answer = YES
58 Correct 36 ms 3724 KB answer = YES
59 Correct 7 ms 3796 KB answer = YES
60 Correct 48 ms 3892 KB answer = YES
61 Correct 5 ms 3412 KB answer = YES
62 Correct 42 ms 10536 KB answer = NO
63 Correct 44 ms 10572 KB answer = YES
64 Correct 44 ms 10572 KB answer = NO
65 Correct 45 ms 10572 KB answer = YES
66 Correct 8 ms 3156 KB answer = YES
67 Correct 315 ms 20088 KB answer = YES
68 Correct 305 ms 19868 KB answer = YES
69 Correct 76 ms 20764 KB answer = YES
70 Correct 94 ms 24276 KB answer = YES
71 Correct 80 ms 20792 KB answer = YES
72 Correct 699 ms 11624 KB answer = YES
73 Correct 73 ms 11500 KB answer = YES
74 Correct 52 ms 10832 KB answer = YES
75 Correct 22 ms 11900 KB answer = NO
76 Correct 66 ms 4088 KB answer = YES
77 Correct 144 ms 5184 KB answer = YES
78 Correct 260 ms 6768 KB answer = YES
79 Correct 629 ms 10304 KB answer = YES
80 Correct 315 ms 12748 KB answer = YES
81 Correct 33 ms 13736 KB answer = NO
82 Correct 83 ms 16356 KB answer = YES
83 Correct 103 ms 20884 KB answer = YES
84 Execution timed out 763 ms 19952 KB Time limit exceeded
85 Halted 0 ms 0 KB -