답안 #821696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
821696 2023-08-11T13:17:39 Z tch1cherin Graph (BOI20_graph) C++17
100 / 100
603 ms 21636 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 < 60; i++) {
      long double m1 = l + (r - l) / 2.1;
      long double m2 = r - (r - l) / 2.1;
      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];
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3028 KB answer = YES
2 Correct 1 ms 3016 KB answer = YES
3 Correct 1 ms 3028 KB answer = YES
4 Correct 2 ms 3028 KB answer = NO
5 Correct 1 ms 3028 KB answer = YES
6 Correct 1 ms 3028 KB answer = YES
7 Correct 1 ms 3028 KB answer = YES
8 Correct 1 ms 3028 KB answer = YES
9 Correct 1 ms 3028 KB answer = NO
10 Correct 2 ms 3028 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 2 ms 3028 KB answer = NO
13 Correct 1 ms 3028 KB answer = YES
14 Correct 1 ms 3028 KB answer = YES
15 Correct 1 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 1 ms 3028 KB answer = YES
18 Correct 1 ms 3028 KB answer = YES
19 Correct 1 ms 3028 KB answer = YES
20 Correct 1 ms 3028 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 1 ms 3028 KB answer = NO
23 Correct 1 ms 3028 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3028 KB answer = YES
2 Correct 1 ms 3016 KB answer = YES
3 Correct 1 ms 3028 KB answer = YES
4 Correct 2 ms 3028 KB answer = NO
5 Correct 1 ms 3028 KB answer = YES
6 Correct 1 ms 3028 KB answer = YES
7 Correct 1 ms 3028 KB answer = YES
8 Correct 1 ms 3028 KB answer = YES
9 Correct 1 ms 3028 KB answer = NO
10 Correct 2 ms 3028 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 2 ms 3028 KB answer = NO
13 Correct 1 ms 3028 KB answer = YES
14 Correct 1 ms 3028 KB answer = YES
15 Correct 1 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 1 ms 3028 KB answer = YES
18 Correct 1 ms 3028 KB answer = YES
19 Correct 1 ms 3028 KB answer = YES
20 Correct 1 ms 3028 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 1 ms 3028 KB answer = NO
23 Correct 1 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 1 ms 3028 KB answer = YES
26 Correct 2 ms 3028 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 1 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 1 ms 3028 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3028 KB answer = YES
2 Correct 1 ms 3016 KB answer = YES
3 Correct 1 ms 3028 KB answer = YES
4 Correct 2 ms 3028 KB answer = NO
5 Correct 1 ms 3028 KB answer = YES
6 Correct 1 ms 3028 KB answer = YES
7 Correct 1 ms 3028 KB answer = YES
8 Correct 1 ms 3028 KB answer = YES
9 Correct 1 ms 3028 KB answer = NO
10 Correct 2 ms 3028 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 2 ms 3028 KB answer = NO
13 Correct 1 ms 3028 KB answer = YES
14 Correct 1 ms 3028 KB answer = YES
15 Correct 1 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 1 ms 3028 KB answer = YES
18 Correct 1 ms 3028 KB answer = YES
19 Correct 1 ms 3028 KB answer = YES
20 Correct 1 ms 3028 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 1 ms 3028 KB answer = NO
23 Correct 1 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 1 ms 3028 KB answer = YES
26 Correct 2 ms 3028 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 1 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 1 ms 3028 KB answer = YES
37 Correct 2 ms 3028 KB answer = YES
38 Correct 1 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 4 ms 3156 KB answer = YES
43 Correct 4 ms 3028 KB answer = YES
44 Correct 5 ms 3028 KB answer = YES
45 Correct 2 ms 3028 KB answer = YES
46 Correct 2 ms 3028 KB answer = YES
47 Correct 2 ms 3156 KB answer = YES
48 Correct 2 ms 3156 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3028 KB answer = YES
2 Correct 1 ms 3016 KB answer = YES
3 Correct 1 ms 3028 KB answer = YES
4 Correct 2 ms 3028 KB answer = NO
5 Correct 1 ms 3028 KB answer = YES
6 Correct 1 ms 3028 KB answer = YES
7 Correct 1 ms 3028 KB answer = YES
8 Correct 1 ms 3028 KB answer = YES
9 Correct 1 ms 3028 KB answer = NO
10 Correct 2 ms 3028 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 2 ms 3028 KB answer = NO
13 Correct 1 ms 3028 KB answer = YES
14 Correct 1 ms 3028 KB answer = YES
15 Correct 1 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 1 ms 3028 KB answer = YES
18 Correct 1 ms 3028 KB answer = YES
19 Correct 1 ms 3028 KB answer = YES
20 Correct 1 ms 3028 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 1 ms 3028 KB answer = NO
23 Correct 1 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 1 ms 3028 KB answer = YES
26 Correct 2 ms 3028 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 1 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 1 ms 3028 KB answer = YES
37 Correct 2 ms 3028 KB answer = YES
38 Correct 1 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 4 ms 3156 KB answer = YES
43 Correct 4 ms 3028 KB answer = YES
44 Correct 5 ms 3028 KB answer = YES
45 Correct 2 ms 3028 KB answer = YES
46 Correct 2 ms 3028 KB answer = YES
47 Correct 2 ms 3156 KB answer = YES
48 Correct 2 ms 3156 KB answer = YES
49 Correct 36 ms 3712 KB answer = YES
50 Correct 9 ms 4308 KB answer = YES
51 Correct 26 ms 4124 KB answer = YES
52 Correct 4 ms 4052 KB answer = NO
53 Correct 4 ms 3156 KB answer = YES
54 Correct 7 ms 3156 KB answer = YES
55 Correct 15 ms 3440 KB answer = YES
56 Correct 36 ms 3708 KB answer = YES
57 Correct 30 ms 3612 KB answer = YES
58 Correct 24 ms 3612 KB answer = YES
59 Correct 7 ms 3668 KB answer = YES
60 Correct 34 ms 3668 KB answer = YES
61 Correct 4 ms 3412 KB answer = YES
62 Correct 38 ms 8140 KB answer = NO
63 Correct 43 ms 8328 KB answer = YES
64 Correct 41 ms 8224 KB answer = NO
65 Correct 43 ms 8284 KB answer = YES
66 Correct 6 ms 3156 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3028 KB answer = YES
2 Correct 1 ms 3016 KB answer = YES
3 Correct 1 ms 3028 KB answer = YES
4 Correct 2 ms 3028 KB answer = NO
5 Correct 1 ms 3028 KB answer = YES
6 Correct 1 ms 3028 KB answer = YES
7 Correct 1 ms 3028 KB answer = YES
8 Correct 1 ms 3028 KB answer = YES
9 Correct 1 ms 3028 KB answer = NO
10 Correct 2 ms 3028 KB answer = YES
11 Correct 2 ms 3028 KB answer = YES
12 Correct 2 ms 3028 KB answer = NO
13 Correct 1 ms 3028 KB answer = YES
14 Correct 1 ms 3028 KB answer = YES
15 Correct 1 ms 3028 KB answer = YES
16 Correct 2 ms 3028 KB answer = YES
17 Correct 1 ms 3028 KB answer = YES
18 Correct 1 ms 3028 KB answer = YES
19 Correct 1 ms 3028 KB answer = YES
20 Correct 1 ms 3028 KB answer = YES
21 Correct 2 ms 3028 KB answer = YES
22 Correct 1 ms 3028 KB answer = NO
23 Correct 1 ms 3028 KB answer = NO
24 Correct 2 ms 3028 KB answer = YES
25 Correct 1 ms 3028 KB answer = YES
26 Correct 2 ms 3028 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 1 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 1 ms 3028 KB answer = YES
37 Correct 2 ms 3028 KB answer = YES
38 Correct 1 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 4 ms 3156 KB answer = YES
43 Correct 4 ms 3028 KB answer = YES
44 Correct 5 ms 3028 KB answer = YES
45 Correct 2 ms 3028 KB answer = YES
46 Correct 2 ms 3028 KB answer = YES
47 Correct 2 ms 3156 KB answer = YES
48 Correct 2 ms 3156 KB answer = YES
49 Correct 36 ms 3712 KB answer = YES
50 Correct 9 ms 4308 KB answer = YES
51 Correct 26 ms 4124 KB answer = YES
52 Correct 4 ms 4052 KB answer = NO
53 Correct 4 ms 3156 KB answer = YES
54 Correct 7 ms 3156 KB answer = YES
55 Correct 15 ms 3440 KB answer = YES
56 Correct 36 ms 3708 KB answer = YES
57 Correct 30 ms 3612 KB answer = YES
58 Correct 24 ms 3612 KB answer = YES
59 Correct 7 ms 3668 KB answer = YES
60 Correct 34 ms 3668 KB answer = YES
61 Correct 4 ms 3412 KB answer = YES
62 Correct 38 ms 8140 KB answer = NO
63 Correct 43 ms 8328 KB answer = YES
64 Correct 41 ms 8224 KB answer = NO
65 Correct 43 ms 8284 KB answer = YES
66 Correct 6 ms 3156 KB answer = YES
67 Correct 236 ms 18584 KB answer = YES
68 Correct 209 ms 18536 KB answer = YES
69 Correct 70 ms 19344 KB answer = YES
70 Correct 93 ms 21636 KB answer = YES
71 Correct 72 ms 19376 KB answer = YES
72 Correct 437 ms 10180 KB answer = YES
73 Correct 72 ms 10132 KB answer = YES
74 Correct 51 ms 10060 KB answer = YES
75 Correct 26 ms 11000 KB answer = NO
76 Correct 44 ms 3916 KB answer = YES
77 Correct 94 ms 4884 KB answer = YES
78 Correct 164 ms 6200 KB answer = YES
79 Correct 375 ms 9156 KB answer = YES
80 Correct 220 ms 11876 KB answer = YES
81 Correct 48 ms 12440 KB answer = NO
82 Correct 78 ms 14996 KB answer = YES
83 Correct 113 ms 19588 KB answer = YES
84 Correct 603 ms 18596 KB answer = YES
85 Correct 240 ms 19956 KB answer = YES
86 Correct 97 ms 20856 KB answer = YES
87 Correct 46 ms 12068 KB answer = NO
88 Correct 153 ms 14372 KB answer = YES
89 Correct 88 ms 10316 KB answer = YES
90 Correct 94 ms 10320 KB answer = YES
91 Correct 98 ms 10320 KB answer = YES
92 Correct 42 ms 7104 KB answer = YES
93 Correct 38 ms 7164 KB answer = YES
94 Correct 48 ms 18668 KB answer = NO
95 Correct 31 ms 9088 KB answer = NO
96 Correct 145 ms 17800 KB answer = YES
97 Correct 33 ms 18624 KB answer = NO