답안 #709065

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
709065 2023-03-13T05:37:44 Z null_awe Graph (BOI20_graph) C++14
58 / 100
700 ms 9128 KB
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

#define ll long long
#define pii pair<int, int>

const double EPS = 0.000001;

struct line {

  int m, b;

  line() {}

  line(int m, int b) : m(m), b(b) {}

  double combine(line other) {
    int mm = m - other.m, bb = other.b - b;
    if (mm == 0 && bb) {
      cout << "NO\n";
      exit(0);
    }
    if (mm == 0) return INT_MAX;
    return (double) bb / mm;
  }
};

int n, m;
vector<vector<pii>> adj;
vector<line> lines;
vector<bool> vis;
vector<double> all;

void solve(int fv) {
  if (vis[fv]) return;
  lines[fv] = line(1, 0);
  queue<int> q;
  vector<int> has;
  vis[fv] = true, q.push(fv), has.push_back(fv);
  double ans = INT_MAX;
  while (q.size()) {
    int v = q.front(); q.pop();
    for (pii p : adj[v]) {
      int u = p.first, c = p.second;
      line change(-lines[v].m, c - lines[v].b);
      if (!vis[u]) {
        vis[u] = true, q.push(u), has.push_back(u);
        lines[u] = change;
      } else {
        double comb = lines[u].combine(change);
        if (ans > INT_MAX - 1) ans = comb;
      }
    }
  }
  if (ans > INT_MAX - 1) {
    ll mn = LLONG_MAX;
    int val = 0;
    for (int v : has) {
      line l = lines[v];
      int t = -l.b / l.m;
      ll sum = 0;
      for (int u : has) {
        line _l = lines[u];
        sum += abs(_l.m * t + _l.b);
      }
      if (sum < mn) mn = sum, ans = t;
    }
  }
  for (int v : has) vis[v] = false;
  all[fv] = ans;
  q = queue<int>();
  vis[fv] = true, q.push(fv);
  while (q.size()) {
    int v = q.front(); q.pop();
    for (pii p : adj[v]) {
      int u = p.first, c = p.second;
      if (vis[u]) continue;
      vis[u] = true, q.push(u);
      all[u] = c - all[v];
    }
  }
}

int main() {
  cin >> n >> m;
  adj.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c; cin >> a >> b >> c; --a, --b;
    adj[a].push_back({b, c}), adj[b].push_back({a, c});
  }
  lines = vector<line>(n);
  vis = vector<bool>(n);
  all.resize(n);
  for (int i = 0; i < n; ++i) solve(i);
  cout << "YES\n";
  for (double val : all) cout << val << ' ';
  cout << '\n';
  return 0;
}

Compilation message

Graph.cpp: In function 'void solve(int)':
Graph.cpp:60:9: warning: unused variable 'val' [-Wunused-variable]
   60 |     int val = 0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB answer = YES
2 Correct 0 ms 300 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 212 KB answer = YES
9 Correct 0 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB answer = YES
2 Correct 0 ms 300 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 212 KB answer = YES
9 Correct 0 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 304 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 ms 304 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 300 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 1 ms 300 KB answer = YES
36 Correct 1 ms 300 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB answer = YES
2 Correct 0 ms 300 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 212 KB answer = YES
9 Correct 0 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 304 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 ms 304 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 300 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 1 ms 300 KB answer = YES
36 Correct 1 ms 300 KB answer = YES
37 Correct 1 ms 212 KB answer = YES
38 Correct 1 ms 212 KB answer = YES
39 Correct 2 ms 308 KB answer = YES
40 Correct 1 ms 340 KB answer = YES
41 Correct 2 ms 340 KB answer = NO
42 Correct 3 ms 308 KB answer = YES
43 Correct 2 ms 340 KB answer = YES
44 Correct 2 ms 340 KB answer = YES
45 Correct 3 ms 340 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 312 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB answer = YES
2 Correct 0 ms 300 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 212 KB answer = YES
9 Correct 0 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 304 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 ms 304 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 300 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 1 ms 300 KB answer = YES
36 Correct 1 ms 300 KB answer = YES
37 Correct 1 ms 212 KB answer = YES
38 Correct 1 ms 212 KB answer = YES
39 Correct 2 ms 308 KB answer = YES
40 Correct 1 ms 340 KB answer = YES
41 Correct 2 ms 340 KB answer = NO
42 Correct 3 ms 308 KB answer = YES
43 Correct 2 ms 340 KB answer = YES
44 Correct 2 ms 340 KB answer = YES
45 Correct 3 ms 340 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 312 KB answer = YES
49 Correct 174 ms 1280 KB answer = YES
50 Correct 13 ms 1212 KB answer = YES
51 Correct 186 ms 1216 KB answer = YES
52 Correct 8 ms 1080 KB answer = NO
53 Correct 3 ms 340 KB answer = YES
54 Correct 11 ms 544 KB answer = YES
55 Correct 44 ms 700 KB answer = YES
56 Correct 185 ms 1268 KB answer = YES
57 Correct 35 ms 1108 KB answer = YES
58 Correct 63 ms 1144 KB answer = YES
59 Correct 11 ms 1108 KB answer = YES
60 Correct 88 ms 1232 KB answer = YES
61 Correct 8 ms 724 KB answer = YES
62 Correct 144 ms 7504 KB answer = NO
63 Correct 132 ms 7540 KB answer = YES
64 Correct 138 ms 7600 KB answer = NO
65 Correct 142 ms 7512 KB answer = YES
66 Correct 6 ms 468 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB answer = YES
2 Correct 0 ms 300 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 212 KB answer = YES
9 Correct 0 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 296 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 304 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 ms 304 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 300 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 1 ms 300 KB answer = YES
36 Correct 1 ms 300 KB answer = YES
37 Correct 1 ms 212 KB answer = YES
38 Correct 1 ms 212 KB answer = YES
39 Correct 2 ms 308 KB answer = YES
40 Correct 1 ms 340 KB answer = YES
41 Correct 2 ms 340 KB answer = NO
42 Correct 3 ms 308 KB answer = YES
43 Correct 2 ms 340 KB answer = YES
44 Correct 2 ms 340 KB answer = YES
45 Correct 3 ms 340 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 312 KB answer = YES
49 Correct 174 ms 1280 KB answer = YES
50 Correct 13 ms 1212 KB answer = YES
51 Correct 186 ms 1216 KB answer = YES
52 Correct 8 ms 1080 KB answer = NO
53 Correct 3 ms 340 KB answer = YES
54 Correct 11 ms 544 KB answer = YES
55 Correct 44 ms 700 KB answer = YES
56 Correct 185 ms 1268 KB answer = YES
57 Correct 35 ms 1108 KB answer = YES
58 Correct 63 ms 1144 KB answer = YES
59 Correct 11 ms 1108 KB answer = YES
60 Correct 88 ms 1232 KB answer = YES
61 Correct 8 ms 724 KB answer = YES
62 Correct 144 ms 7504 KB answer = NO
63 Correct 132 ms 7540 KB answer = YES
64 Correct 138 ms 7600 KB answer = NO
65 Correct 142 ms 7512 KB answer = YES
66 Correct 6 ms 468 KB answer = YES
67 Execution timed out 1077 ms 9128 KB Time limit exceeded
68 Halted 0 ms 0 KB -