답안 #254860

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
254860 2020-07-30T18:26:49 Z dolphingarlic Graph (BOI20_graph) C++14
100 / 100
195 ms 21876 KB
/*
Baltic 2020 Graph
- N.B. The graph isn't necessarily connected, so apply the following on all connected components
- Let node 1 have value X
- Then do a DFS
- We know the value of each node in terms of X from the previously visited node and edge
- This means we get a bunch of linear equations (mx + c) for each node
- If there is more than 1 linear equation for any node, we can either find X or show that a labelling
  is not possible
- If all nodes only have 1 linear equation though, then we must find X to minimize the sum of their
  absolute values
    - Notice how we can simply sort by (-m * c) and take the median in this case, since abs(m) = 1
- Complexity: O(N log N)
*/

#include <bits/stdc++.h>
#define FOR(i, x, y) for (ll i = x; i < y; i++)
typedef long long ll;
using namespace std;

pair<ll, ll> labels[100001];
vector<pair<ll, ll>> graph[100001];
ll twice_x, ans[100001];
vector<ll> processed;
bool know_x = false, visited[100001];

bool incompatible(pair<ll, ll> A, pair<ll, ll> B) {
    if (know_x)
        return (A.first * twice_x + 2 * A.second !=
                B.first * twice_x + 2 * B.second);
    return (A.first == B.first && A.second != B.second);
}

void calc_x(pair<ll, ll> A, pair<ll, ll> B) {
    if (A.first == -1) swap(A, B);
    twice_x = B.second - A.second;
    know_x = true;
}

void dfs(ll node = 1) {
    visited[node] = true;
    processed.push_back(node);
    for (pair<ll, ll> i : graph[node]) {
        pair<ll, ll> assign = {-labels[node].first,
                               i.second - labels[node].second};
        if (!visited[i.first]) {
            labels[i.first] = assign;
            dfs(i.first);
        } else {
            if (labels[i.first] == assign) continue;
            if (incompatible(labels[i.first], assign)) {
                cout << "NO\n";
                exit(0);
            }
            calc_x(labels[i.first], assign);
        }
    }
}

void assign_opt_x() {
    vector<int> salient_points;
    for (int i : processed)
        salient_points.push_back(-labels[i].first * labels[i].second);
    sort(salient_points.begin(), salient_points.end());
    twice_x = 2 * salient_points[salient_points.size() / 2];
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, m;
    cin >> n >> m;
    while (m--) {
        ll u, v, w;
        cin >> u >> v >> w;
        graph[u].push_back({v, w});
        graph[v].push_back({u, w});
    }

    FOR(i, 1, n + 1) {
        if (!visited[i]) {
            processed.clear();
            know_x = false;
            labels[i] = {1, 0};
            dfs(i);
            if (!know_x) assign_opt_x();
            for (int i : processed)
                ans[i] = labels[i].first * twice_x + 2 * labels[i].second;
        }
    }

    cout << "YES\n";
    FOR(i, 1, n + 1) cout << ans[i] / 2.0 << " \n"[i == n];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 2 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 2 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 2 ms 2688 KB answer = YES
12 Correct 2 ms 2596 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 2 ms 2688 KB answer = YES
20 Correct 2 ms 2688 KB answer = YES
21 Correct 2 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 2 ms 2688 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 2 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 2 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 2 ms 2688 KB answer = YES
12 Correct 2 ms 2596 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 2 ms 2688 KB answer = YES
20 Correct 2 ms 2688 KB answer = YES
21 Correct 2 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 2 ms 2688 KB answer = NO
24 Correct 2 ms 2688 KB answer = YES
25 Correct 2 ms 2688 KB answer = YES
26 Correct 2 ms 2688 KB answer = YES
27 Correct 2 ms 2688 KB answer = YES
28 Correct 2 ms 2688 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 2 ms 2688 KB answer = NO
31 Correct 2 ms 2688 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2688 KB answer = YES
34 Correct 2 ms 2688 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2688 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 2 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 2 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 2 ms 2688 KB answer = YES
12 Correct 2 ms 2596 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 2 ms 2688 KB answer = YES
20 Correct 2 ms 2688 KB answer = YES
21 Correct 2 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 2 ms 2688 KB answer = NO
24 Correct 2 ms 2688 KB answer = YES
25 Correct 2 ms 2688 KB answer = YES
26 Correct 2 ms 2688 KB answer = YES
27 Correct 2 ms 2688 KB answer = YES
28 Correct 2 ms 2688 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 2 ms 2688 KB answer = NO
31 Correct 2 ms 2688 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2688 KB answer = YES
34 Correct 2 ms 2688 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2688 KB answer = YES
37 Correct 2 ms 2688 KB answer = YES
38 Correct 2 ms 2688 KB answer = YES
39 Correct 2 ms 2688 KB answer = YES
40 Correct 3 ms 2816 KB answer = YES
41 Correct 2 ms 2816 KB answer = NO
42 Correct 3 ms 2816 KB answer = YES
43 Correct 3 ms 2816 KB answer = YES
44 Correct 2 ms 2816 KB answer = YES
45 Correct 2 ms 2816 KB answer = YES
46 Correct 2 ms 2688 KB answer = YES
47 Correct 2 ms 2816 KB answer = YES
48 Correct 3 ms 2816 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 2 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 2 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 2 ms 2688 KB answer = YES
12 Correct 2 ms 2596 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 2 ms 2688 KB answer = YES
20 Correct 2 ms 2688 KB answer = YES
21 Correct 2 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 2 ms 2688 KB answer = NO
24 Correct 2 ms 2688 KB answer = YES
25 Correct 2 ms 2688 KB answer = YES
26 Correct 2 ms 2688 KB answer = YES
27 Correct 2 ms 2688 KB answer = YES
28 Correct 2 ms 2688 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 2 ms 2688 KB answer = NO
31 Correct 2 ms 2688 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2688 KB answer = YES
34 Correct 2 ms 2688 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2688 KB answer = YES
37 Correct 2 ms 2688 KB answer = YES
38 Correct 2 ms 2688 KB answer = YES
39 Correct 2 ms 2688 KB answer = YES
40 Correct 3 ms 2816 KB answer = YES
41 Correct 2 ms 2816 KB answer = NO
42 Correct 3 ms 2816 KB answer = YES
43 Correct 3 ms 2816 KB answer = YES
44 Correct 2 ms 2816 KB answer = YES
45 Correct 2 ms 2816 KB answer = YES
46 Correct 2 ms 2688 KB answer = YES
47 Correct 2 ms 2816 KB answer = YES
48 Correct 3 ms 2816 KB answer = YES
49 Correct 10 ms 3712 KB answer = YES
50 Correct 10 ms 3968 KB answer = YES
51 Correct 10 ms 3968 KB answer = YES
52 Correct 5 ms 3328 KB answer = NO
53 Correct 2 ms 2816 KB answer = YES
54 Correct 3 ms 2944 KB answer = YES
55 Correct 5 ms 3200 KB answer = YES
56 Correct 10 ms 3712 KB answer = YES
57 Correct 9 ms 3584 KB answer = YES
58 Correct 13 ms 3456 KB answer = YES
59 Correct 9 ms 3584 KB answer = YES
60 Correct 10 ms 3712 KB answer = YES
61 Correct 6 ms 3200 KB answer = YES
62 Correct 63 ms 11896 KB answer = NO
63 Correct 72 ms 12664 KB answer = YES
64 Correct 66 ms 12456 KB answer = NO
65 Correct 77 ms 12536 KB answer = YES
66 Correct 3 ms 2944 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB answer = YES
2 Correct 2 ms 2688 KB answer = YES
3 Correct 2 ms 2688 KB answer = YES
4 Correct 2 ms 2688 KB answer = NO
5 Correct 2 ms 2688 KB answer = YES
6 Correct 2 ms 2688 KB answer = YES
7 Correct 2 ms 2688 KB answer = YES
8 Correct 2 ms 2688 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2688 KB answer = YES
11 Correct 2 ms 2688 KB answer = YES
12 Correct 2 ms 2596 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2688 KB answer = YES
16 Correct 2 ms 2688 KB answer = YES
17 Correct 2 ms 2688 KB answer = YES
18 Correct 2 ms 2688 KB answer = YES
19 Correct 2 ms 2688 KB answer = YES
20 Correct 2 ms 2688 KB answer = YES
21 Correct 2 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 2 ms 2688 KB answer = NO
24 Correct 2 ms 2688 KB answer = YES
25 Correct 2 ms 2688 KB answer = YES
26 Correct 2 ms 2688 KB answer = YES
27 Correct 2 ms 2688 KB answer = YES
28 Correct 2 ms 2688 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 2 ms 2688 KB answer = NO
31 Correct 2 ms 2688 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2688 KB answer = YES
34 Correct 2 ms 2688 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2688 KB answer = YES
37 Correct 2 ms 2688 KB answer = YES
38 Correct 2 ms 2688 KB answer = YES
39 Correct 2 ms 2688 KB answer = YES
40 Correct 3 ms 2816 KB answer = YES
41 Correct 2 ms 2816 KB answer = NO
42 Correct 3 ms 2816 KB answer = YES
43 Correct 3 ms 2816 KB answer = YES
44 Correct 2 ms 2816 KB answer = YES
45 Correct 2 ms 2816 KB answer = YES
46 Correct 2 ms 2688 KB answer = YES
47 Correct 2 ms 2816 KB answer = YES
48 Correct 3 ms 2816 KB answer = YES
49 Correct 10 ms 3712 KB answer = YES
50 Correct 10 ms 3968 KB answer = YES
51 Correct 10 ms 3968 KB answer = YES
52 Correct 5 ms 3328 KB answer = NO
53 Correct 2 ms 2816 KB answer = YES
54 Correct 3 ms 2944 KB answer = YES
55 Correct 5 ms 3200 KB answer = YES
56 Correct 10 ms 3712 KB answer = YES
57 Correct 9 ms 3584 KB answer = YES
58 Correct 13 ms 3456 KB answer = YES
59 Correct 9 ms 3584 KB answer = YES
60 Correct 10 ms 3712 KB answer = YES
61 Correct 6 ms 3200 KB answer = YES
62 Correct 63 ms 11896 KB answer = NO
63 Correct 72 ms 12664 KB answer = YES
64 Correct 66 ms 12456 KB answer = NO
65 Correct 77 ms 12536 KB answer = YES
66 Correct 3 ms 2944 KB answer = YES
67 Correct 91 ms 15760 KB answer = YES
68 Correct 88 ms 15696 KB answer = YES
69 Correct 90 ms 15596 KB answer = YES
70 Correct 131 ms 21876 KB answer = YES
71 Correct 103 ms 15852 KB answer = YES
72 Correct 124 ms 11364 KB answer = YES
73 Correct 129 ms 11376 KB answer = YES
74 Correct 64 ms 10612 KB answer = YES
75 Correct 30 ms 8440 KB answer = NO
76 Correct 11 ms 3840 KB answer = YES
77 Correct 22 ms 5112 KB answer = YES
78 Correct 41 ms 6596 KB answer = YES
79 Correct 102 ms 10140 KB answer = YES
80 Correct 64 ms 10612 KB answer = YES
81 Correct 53 ms 12396 KB answer = NO
82 Correct 108 ms 15596 KB answer = YES
83 Correct 117 ms 16368 KB answer = YES
84 Correct 123 ms 16348 KB answer = YES
85 Correct 97 ms 15836 KB answer = YES
86 Correct 93 ms 15724 KB answer = YES
87 Correct 62 ms 12148 KB answer = NO
88 Correct 121 ms 13384 KB answer = YES
89 Correct 92 ms 10104 KB answer = YES
90 Correct 96 ms 10140 KB answer = YES
91 Correct 105 ms 10744 KB answer = YES
92 Correct 48 ms 7280 KB answer = YES
93 Correct 45 ms 7152 KB answer = YES
94 Correct 68 ms 15212 KB answer = NO
95 Correct 56 ms 10332 KB answer = NO
96 Correct 195 ms 19824 KB answer = YES
97 Correct 49 ms 14700 KB answer = NO