Submission #1094714

# Submission time Handle Problem Language Result Execution time Memory
1094714 2024-09-30T10:37:45 Z buzdi Graph (BOI20_graph) C++17
100 / 100
251 ms 24008 KB
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#define ll long long

using namespace std;

const int NMAX = 1e5;
const int INF = 1e9;
const long double EPS = 1e-6;

struct Value {
    int a, b;
}values[NMAX + 1];

int n, m, components;
vector<pair<int, int>> G[NMAX + 1];
bool visited[NMAX + 1];
bool ok;
long double unique_solution;
vector<int> component[NMAX + 1];
long double answer[NMAX + 1];

int GCD(int a, int b) {
    while (b) {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

Value make_value(int a, int b) {
    Value answer;
    answer.a = a;
    answer.b = b;
    return answer;
}

void Simplify(pair<int, int> &a) {
    int gcd = GCD(abs(a.first), abs(a.second));
    a.first /= gcd;
    a.second /= gcd;
}

bool GetSolution(Value x, Value y, int which) {
    if (x.a + y.a == 0) {
        return x.b + y.b == which;
    }

    pair<int, int> solution_x = { which - x.b - y.b, x.a + y.a };
    if ((solution_x.first <= 0 && solution_x.second <= 0) || (solution_x.first >= 0 && solution_x.second <= 0)) {
        solution_x.first = -solution_x.first;
        solution_x.second = -solution_x.second;
    }
    Simplify(solution_x);
    long double real_solution_x = (long double) solution_x.first / (long double) solution_x.second;
    if(unique_solution == INF) {
        unique_solution = real_solution_x;
        return true;
    }
    return abs(unique_solution - real_solution_x) <= EPS;
}

void DFS(int node, Value curent_value) {
    values[node] = curent_value;
    visited[node] = 1;
    component[components].push_back(node);
    for (auto [next_node, which] : G[node]) {
        if (!visited[next_node]) {
            DFS(next_node, make_value(-curent_value.a, which - curent_value.b));
        }
        else {
            if(!GetSolution(values[node], values[next_node], which)) {
                ok = false;
            }
        }
    }
}

long double GetValue(long double x, vector<int> &a) {
    long double answer = 0;
    for (int node : a) {
        answer += abs(values[node].a * x + values[node].b);
    }
    return answer;
}

void GetAnswer(vector<int> &a) {
    if (unique_solution != INF) {
        for(int node : a) {
            answer[node] = values[node].a * unique_solution + values[node].b;
        }
    }
    else {
        long double left = -INF, right = INF;
        int iterations = 400;
        while (iterations--) {
            long double mid1 = left + (right - left) / 3.0;
            long double mid2 = right - (right - left) / 3.0;

            long double value1 = GetValue(mid1, a);
            long double value2 = GetValue(mid2, a);
            if (value1 > value2) {
                left = mid1;
            }
            else {
                right = mid2;
            }
        }

        for(int node : a) {
            answer[node] = values[node].a * left + values[node].b;
        }
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a].push_back({ b, c });
        G[b].push_back({ a, c });
    }

    for (int i = 1; i <= n; i++) {
        if (!visited[i]) {
            components++;
            ok = true;
            unique_solution = INF;
            DFS(i, make_value(1, 0));
            if(!ok) {
                cout << "NO" << '\n';
                return 0;
            }
            GetAnswer(component[components]);
        }
    }
    cout << "YES" << '\n';
    for(int i = 1; i <= n; i++) {
        cout << fixed << setprecision(6) << answer[i] << ' ';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5420 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 2 ms 5104 KB answer = NO
5 Correct 3 ms 4956 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 3 ms 4952 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 3 ms 5212 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 4956 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4952 KB answer = YES
17 Correct 3 ms 4956 KB answer = YES
18 Correct 3 ms 4956 KB answer = YES
19 Correct 2 ms 4952 KB answer = YES
20 Correct 3 ms 4952 KB answer = YES
21 Correct 2 ms 4932 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5420 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 2 ms 5104 KB answer = NO
5 Correct 3 ms 4956 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 3 ms 4952 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 3 ms 5212 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 4956 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4952 KB answer = YES
17 Correct 3 ms 4956 KB answer = YES
18 Correct 3 ms 4956 KB answer = YES
19 Correct 2 ms 4952 KB answer = YES
20 Correct 3 ms 4952 KB answer = YES
21 Correct 2 ms 4932 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 2 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 3 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5212 KB answer = YES
30 Correct 2 ms 5172 KB answer = NO
31 Correct 2 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5036 KB answer = YES
34 Correct 2 ms 5176 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5420 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 2 ms 5104 KB answer = NO
5 Correct 3 ms 4956 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 3 ms 4952 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 3 ms 5212 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 4956 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4952 KB answer = YES
17 Correct 3 ms 4956 KB answer = YES
18 Correct 3 ms 4956 KB answer = YES
19 Correct 2 ms 4952 KB answer = YES
20 Correct 3 ms 4952 KB answer = YES
21 Correct 2 ms 4932 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 2 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 3 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5212 KB answer = YES
30 Correct 2 ms 5172 KB answer = NO
31 Correct 2 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5036 KB answer = YES
34 Correct 2 ms 5176 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
37 Correct 3 ms 4956 KB answer = YES
38 Correct 3 ms 4956 KB answer = YES
39 Correct 4 ms 5212 KB answer = YES
40 Correct 3 ms 5208 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 5 ms 5212 KB answer = YES
43 Correct 4 ms 5212 KB answer = YES
44 Correct 4 ms 5212 KB answer = YES
45 Correct 3 ms 5212 KB answer = YES
46 Correct 3 ms 5212 KB answer = YES
47 Correct 2 ms 5212 KB answer = YES
48 Correct 2 ms 5212 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5420 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 2 ms 5104 KB answer = NO
5 Correct 3 ms 4956 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 3 ms 4952 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 3 ms 5212 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 4956 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4952 KB answer = YES
17 Correct 3 ms 4956 KB answer = YES
18 Correct 3 ms 4956 KB answer = YES
19 Correct 2 ms 4952 KB answer = YES
20 Correct 3 ms 4952 KB answer = YES
21 Correct 2 ms 4932 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 2 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 3 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5212 KB answer = YES
30 Correct 2 ms 5172 KB answer = NO
31 Correct 2 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5036 KB answer = YES
34 Correct 2 ms 5176 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
37 Correct 3 ms 4956 KB answer = YES
38 Correct 3 ms 4956 KB answer = YES
39 Correct 4 ms 5212 KB answer = YES
40 Correct 3 ms 5208 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 5 ms 5212 KB answer = YES
43 Correct 4 ms 5212 KB answer = YES
44 Correct 4 ms 5212 KB answer = YES
45 Correct 3 ms 5212 KB answer = YES
46 Correct 3 ms 5212 KB answer = YES
47 Correct 2 ms 5212 KB answer = YES
48 Correct 2 ms 5212 KB answer = YES
49 Correct 24 ms 5848 KB answer = YES
50 Correct 9 ms 6232 KB answer = YES
51 Correct 25 ms 6148 KB answer = YES
52 Correct 5 ms 5980 KB answer = NO
53 Correct 4 ms 5124 KB answer = YES
54 Correct 8 ms 5208 KB answer = YES
55 Correct 14 ms 5468 KB answer = YES
56 Correct 24 ms 5852 KB answer = YES
57 Correct 30 ms 5972 KB answer = YES
58 Correct 24 ms 5724 KB answer = YES
59 Correct 7 ms 5980 KB answer = YES
60 Correct 25 ms 5968 KB answer = YES
61 Correct 5 ms 5468 KB answer = YES
62 Correct 47 ms 12624 KB answer = NO
63 Correct 45 ms 12884 KB answer = YES
64 Correct 42 ms 12628 KB answer = NO
65 Correct 44 ms 12888 KB answer = YES
66 Correct 8 ms 5208 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5420 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 2 ms 5104 KB answer = NO
5 Correct 3 ms 4956 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 3 ms 4952 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 3 ms 5212 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 4956 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4952 KB answer = YES
17 Correct 3 ms 4956 KB answer = YES
18 Correct 3 ms 4956 KB answer = YES
19 Correct 2 ms 4952 KB answer = YES
20 Correct 3 ms 4952 KB answer = YES
21 Correct 2 ms 4932 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 2 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 3 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5212 KB answer = YES
30 Correct 2 ms 5172 KB answer = NO
31 Correct 2 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5036 KB answer = YES
34 Correct 2 ms 5176 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
37 Correct 3 ms 4956 KB answer = YES
38 Correct 3 ms 4956 KB answer = YES
39 Correct 4 ms 5212 KB answer = YES
40 Correct 3 ms 5208 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 5 ms 5212 KB answer = YES
43 Correct 4 ms 5212 KB answer = YES
44 Correct 4 ms 5212 KB answer = YES
45 Correct 3 ms 5212 KB answer = YES
46 Correct 3 ms 5212 KB answer = YES
47 Correct 2 ms 5212 KB answer = YES
48 Correct 2 ms 5212 KB answer = YES
49 Correct 24 ms 5848 KB answer = YES
50 Correct 9 ms 6232 KB answer = YES
51 Correct 25 ms 6148 KB answer = YES
52 Correct 5 ms 5980 KB answer = NO
53 Correct 4 ms 5124 KB answer = YES
54 Correct 8 ms 5208 KB answer = YES
55 Correct 14 ms 5468 KB answer = YES
56 Correct 24 ms 5852 KB answer = YES
57 Correct 30 ms 5972 KB answer = YES
58 Correct 24 ms 5724 KB answer = YES
59 Correct 7 ms 5980 KB answer = YES
60 Correct 25 ms 5968 KB answer = YES
61 Correct 5 ms 5468 KB answer = YES
62 Correct 47 ms 12624 KB answer = NO
63 Correct 45 ms 12884 KB answer = YES
64 Correct 42 ms 12628 KB answer = NO
65 Correct 44 ms 12888 KB answer = YES
66 Correct 8 ms 5208 KB answer = YES
67 Correct 236 ms 19852 KB answer = YES
68 Correct 245 ms 19656 KB answer = YES
69 Correct 54 ms 19652 KB answer = YES
70 Correct 76 ms 24008 KB answer = YES
71 Correct 53 ms 19656 KB answer = YES
72 Correct 251 ms 14024 KB answer = YES
73 Correct 66 ms 14100 KB answer = YES
74 Correct 64 ms 13932 KB answer = YES
75 Correct 21 ms 12248 KB answer = NO
76 Correct 30 ms 6228 KB answer = YES
77 Correct 57 ms 7248 KB answer = YES
78 Correct 100 ms 9040 KB answer = YES
79 Correct 203 ms 12760 KB answer = YES
80 Correct 160 ms 13788 KB answer = YES
81 Correct 28 ms 14280 KB answer = NO
82 Correct 65 ms 19144 KB answer = YES
83 Correct 73 ms 19916 KB answer = YES
84 Correct 248 ms 19848 KB answer = YES
85 Correct 246 ms 19660 KB answer = YES
86 Correct 60 ms 19736 KB answer = YES
87 Correct 30 ms 13908 KB answer = NO
88 Correct 91 ms 16328 KB answer = YES
89 Correct 62 ms 13824 KB answer = YES
90 Correct 60 ms 13908 KB answer = YES
91 Correct 60 ms 13904 KB answer = YES
92 Correct 29 ms 9532 KB answer = YES
93 Correct 30 ms 9648 KB answer = YES
94 Correct 34 ms 17096 KB answer = NO
95 Correct 29 ms 12112 KB answer = NO
96 Correct 110 ms 21588 KB answer = YES
97 Correct 25 ms 17100 KB answer = NO