답안 #557631

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557631 2022-05-05T16:25:04 Z Jomnoi Graph (BOI20_graph) C++17
100 / 100
160 ms 25408 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 7;

int cnt_component;
vector <pair <int, int>> graph[MAX_N];
vector <int> node[MAX_N];
int component[MAX_N];
int A[MAX_N], B[MAX_N];
double X[MAX_N];

void dfs(int u, int p, int now_component, int a, int b) {
    A[u] = a, B[u] = b;
    component[u] = now_component;
    node[now_component].push_back(u);
    for(auto [v, c] : graph[u]) {
        if(v == p) {
            continue;
        }

        int na = -a, nb = c - b;
        if(component[v] != 0) {
            if(A[v] == na) {
                if(B[v] != nb) {
                    cout << "NO";
                    exit(0);
                }
            }
            else {
                double x = 1.0 * (B[v] - nb) / (na - A[v]);
                if(X[now_component] != INF and X[now_component] != x) {
                    cout << "NO";
                    exit(0);
                }
                X[now_component] = x;
            }
        }
        else {
            dfs(v, u, now_component, na, nb);
        }
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    while(M--) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a].emplace_back(b, c);
        graph[b].emplace_back(a, c);
    }

    fill(X + 1, X + N + 1, INF);
    for(int i = 1; i <= N; i++) {
        if(component[i] != 0) {
            continue;
        }
        
        dfs(i, -1, ++cnt_component, 1, 0);

        if(X[cnt_component] == INF) {
            vector <int> median;
            for(auto v : node[cnt_component]) {
                median.push_back(-A[v] * B[v]);
            }

            sort(median.begin(), median.end());
            X[cnt_component] = median[median.size() / 2];
        }
    }

    cout << "YES\n";
    for(int i = 1; i <= N; i++) {
        cout << fixed << setprecision(9) << A[i] * X[component[i]] + B[i] << ' ';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 5036 KB answer = NO
5 Correct 3 ms 5036 KB answer = YES
6 Correct 3 ms 5028 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 5032 KB answer = NO
10 Correct 3 ms 5076 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 4948 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 5032 KB answer = YES
20 Correct 3 ms 5036 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 3 ms 4948 KB answer = NO
23 Correct 3 ms 5028 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 5036 KB answer = NO
5 Correct 3 ms 5036 KB answer = YES
6 Correct 3 ms 5028 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 5032 KB answer = NO
10 Correct 3 ms 5076 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 4948 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 5032 KB answer = YES
20 Correct 3 ms 5036 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 3 ms 4948 KB answer = NO
23 Correct 3 ms 5028 KB answer = NO
24 Correct 3 ms 5032 KB answer = YES
25 Correct 3 ms 5028 KB answer = YES
26 Correct 3 ms 5032 KB answer = YES
27 Correct 3 ms 5036 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 4 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 5032 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5032 KB answer = YES
36 Correct 3 ms 5032 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 5036 KB answer = NO
5 Correct 3 ms 5036 KB answer = YES
6 Correct 3 ms 5028 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 5032 KB answer = NO
10 Correct 3 ms 5076 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 4948 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 5032 KB answer = YES
20 Correct 3 ms 5036 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 3 ms 4948 KB answer = NO
23 Correct 3 ms 5028 KB answer = NO
24 Correct 3 ms 5032 KB answer = YES
25 Correct 3 ms 5028 KB answer = YES
26 Correct 3 ms 5032 KB answer = YES
27 Correct 3 ms 5036 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 4 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 5032 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5032 KB answer = YES
36 Correct 3 ms 5032 KB answer = YES
37 Correct 3 ms 4948 KB answer = YES
38 Correct 3 ms 4948 KB answer = YES
39 Correct 4 ms 5036 KB answer = YES
40 Correct 4 ms 5076 KB answer = YES
41 Correct 3 ms 5088 KB answer = NO
42 Correct 4 ms 5076 KB answer = YES
43 Correct 4 ms 5044 KB answer = YES
44 Correct 3 ms 5040 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 3 ms 5036 KB answer = YES
47 Correct 4 ms 5040 KB answer = YES
48 Correct 4 ms 5076 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 5036 KB answer = NO
5 Correct 3 ms 5036 KB answer = YES
6 Correct 3 ms 5028 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 5032 KB answer = NO
10 Correct 3 ms 5076 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 4948 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 5032 KB answer = YES
20 Correct 3 ms 5036 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 3 ms 4948 KB answer = NO
23 Correct 3 ms 5028 KB answer = NO
24 Correct 3 ms 5032 KB answer = YES
25 Correct 3 ms 5028 KB answer = YES
26 Correct 3 ms 5032 KB answer = YES
27 Correct 3 ms 5036 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 4 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 5032 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5032 KB answer = YES
36 Correct 3 ms 5032 KB answer = YES
37 Correct 3 ms 4948 KB answer = YES
38 Correct 3 ms 4948 KB answer = YES
39 Correct 4 ms 5036 KB answer = YES
40 Correct 4 ms 5076 KB answer = YES
41 Correct 3 ms 5088 KB answer = NO
42 Correct 4 ms 5076 KB answer = YES
43 Correct 4 ms 5044 KB answer = YES
44 Correct 3 ms 5040 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 3 ms 5036 KB answer = YES
47 Correct 4 ms 5040 KB answer = YES
48 Correct 4 ms 5076 KB answer = YES
49 Correct 11 ms 5972 KB answer = YES
50 Correct 11 ms 6228 KB answer = YES
51 Correct 14 ms 6320 KB answer = YES
52 Correct 6 ms 5680 KB answer = NO
53 Correct 4 ms 5036 KB answer = YES
54 Correct 6 ms 5204 KB answer = YES
55 Correct 7 ms 5460 KB answer = YES
56 Correct 11 ms 5936 KB answer = YES
57 Correct 12 ms 5848 KB answer = YES
58 Correct 13 ms 5812 KB answer = YES
59 Correct 10 ms 5772 KB answer = YES
60 Correct 11 ms 5940 KB answer = YES
61 Correct 7 ms 5460 KB answer = YES
62 Correct 59 ms 12024 KB answer = NO
63 Correct 59 ms 12796 KB answer = YES
64 Correct 53 ms 12752 KB answer = NO
65 Correct 59 ms 12816 KB answer = YES
66 Correct 5 ms 5204 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB answer = YES
2 Correct 3 ms 4948 KB answer = YES
3 Correct 3 ms 4948 KB answer = YES
4 Correct 3 ms 5036 KB answer = NO
5 Correct 3 ms 5036 KB answer = YES
6 Correct 3 ms 5028 KB answer = YES
7 Correct 3 ms 4948 KB answer = YES
8 Correct 3 ms 4948 KB answer = YES
9 Correct 3 ms 5032 KB answer = NO
10 Correct 3 ms 5076 KB answer = YES
11 Correct 3 ms 4948 KB answer = YES
12 Correct 3 ms 4948 KB answer = NO
13 Correct 3 ms 4948 KB answer = YES
14 Correct 3 ms 4948 KB answer = YES
15 Correct 3 ms 4948 KB answer = YES
16 Correct 3 ms 4948 KB answer = YES
17 Correct 3 ms 4948 KB answer = YES
18 Correct 3 ms 4948 KB answer = YES
19 Correct 3 ms 5032 KB answer = YES
20 Correct 3 ms 5036 KB answer = YES
21 Correct 3 ms 4948 KB answer = YES
22 Correct 3 ms 4948 KB answer = NO
23 Correct 3 ms 5028 KB answer = NO
24 Correct 3 ms 5032 KB answer = YES
25 Correct 3 ms 5028 KB answer = YES
26 Correct 3 ms 5032 KB answer = YES
27 Correct 3 ms 5036 KB answer = YES
28 Correct 3 ms 4948 KB answer = YES
29 Correct 4 ms 4948 KB answer = YES
30 Correct 3 ms 4948 KB answer = NO
31 Correct 3 ms 4948 KB answer = YES
32 Correct 3 ms 5032 KB answer = YES
33 Correct 3 ms 4948 KB answer = YES
34 Correct 3 ms 4948 KB answer = YES
35 Correct 3 ms 5032 KB answer = YES
36 Correct 3 ms 5032 KB answer = YES
37 Correct 3 ms 4948 KB answer = YES
38 Correct 3 ms 4948 KB answer = YES
39 Correct 4 ms 5036 KB answer = YES
40 Correct 4 ms 5076 KB answer = YES
41 Correct 3 ms 5088 KB answer = NO
42 Correct 4 ms 5076 KB answer = YES
43 Correct 4 ms 5044 KB answer = YES
44 Correct 3 ms 5040 KB answer = YES
45 Correct 4 ms 5076 KB answer = YES
46 Correct 3 ms 5036 KB answer = YES
47 Correct 4 ms 5040 KB answer = YES
48 Correct 4 ms 5076 KB answer = YES
49 Correct 11 ms 5972 KB answer = YES
50 Correct 11 ms 6228 KB answer = YES
51 Correct 14 ms 6320 KB answer = YES
52 Correct 6 ms 5680 KB answer = NO
53 Correct 4 ms 5036 KB answer = YES
54 Correct 6 ms 5204 KB answer = YES
55 Correct 7 ms 5460 KB answer = YES
56 Correct 11 ms 5936 KB answer = YES
57 Correct 12 ms 5848 KB answer = YES
58 Correct 13 ms 5812 KB answer = YES
59 Correct 10 ms 5772 KB answer = YES
60 Correct 11 ms 5940 KB answer = YES
61 Correct 7 ms 5460 KB answer = YES
62 Correct 59 ms 12024 KB answer = NO
63 Correct 59 ms 12796 KB answer = YES
64 Correct 53 ms 12752 KB answer = NO
65 Correct 59 ms 12816 KB answer = YES
66 Correct 5 ms 5204 KB answer = YES
67 Correct 95 ms 21048 KB answer = YES
68 Correct 85 ms 20920 KB answer = YES
69 Correct 83 ms 20868 KB answer = YES
70 Correct 112 ms 25408 KB answer = YES
71 Correct 83 ms 20896 KB answer = YES
72 Correct 99 ms 13752 KB answer = YES
73 Correct 98 ms 13868 KB answer = YES
74 Correct 65 ms 14488 KB answer = YES
75 Correct 28 ms 11340 KB answer = NO
76 Correct 12 ms 6100 KB answer = YES
77 Correct 22 ms 7272 KB answer = YES
78 Correct 37 ms 8840 KB answer = YES
79 Correct 80 ms 12412 KB answer = YES
80 Correct 68 ms 14484 KB answer = YES
81 Correct 42 ms 15940 KB answer = NO
82 Correct 99 ms 20440 KB answer = YES
83 Correct 122 ms 21200 KB answer = YES
84 Correct 131 ms 21068 KB answer = YES
85 Correct 94 ms 21120 KB answer = YES
86 Correct 91 ms 21040 KB answer = YES
87 Correct 53 ms 13876 KB answer = NO
88 Correct 110 ms 16612 KB answer = YES
89 Correct 95 ms 13468 KB answer = YES
90 Correct 93 ms 13592 KB answer = YES
91 Correct 92 ms 13516 KB answer = YES
92 Correct 47 ms 9404 KB answer = YES
93 Correct 55 ms 9428 KB answer = YES
94 Correct 52 ms 19684 KB answer = NO
95 Correct 40 ms 11456 KB answer = NO
96 Correct 160 ms 22324 KB answer = YES
97 Correct 40 ms 19672 KB answer = NO