Submission #1094691

# Submission time Handle Problem Language Result Execution time Memory
1094691 2024-09-30T09:51:31 Z buzdi Graph (BOI20_graph) C++17
0 / 100
2 ms 2816 KB
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#define ll long long
#define int 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;
vector<pair<int, int>> G[NMAX + 1];
bool visited[NMAX + 1];
bool ok;
pair<int, int> unique_solution;

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);
    assert((solution_x.first >= 0 && solution_x.second >= 0) || (solution_x.first <= 0 && solution_x.second >= 0));
    if (unique_solution == make_pair(INF, INF)) {
        unique_solution = solution_x;
        return true;
    }
    return unique_solution == solution_x;
}

void DFS(int node, Value curent_value) {
    values[node] = curent_value;
    visited[node] = 1;
    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) {
    long double answer = 0;
    for (int i = 1; i <= n; i++) {
        answer += abs(values[i].a * x + values[i].b);
    }
    return answer;
}

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 });
    }

    ok = true;
    unique_solution = { INF, INF };
    for (int i = 1; i <= n; i++) {
        if (!visited[i]) {
            DFS(i, make_value(1, 0));
        }
    }

    if (ok) {
        cout << "YES" << '\n';
        if (unique_solution != make_pair(INF, INF)) {
            long double x = (long double) unique_solution.first / (long double) unique_solution.second;
            for (int i = 1; i <= n; i++) {
                cout << fixed << setprecision(6) << values[i].a * x + values[i].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);
                long double value2 = GetValue(mid2);
                if (value1 > value2) {
                    left = mid1;
                }
                else {
                    right = mid2;
                }
            }

            for (int i = 1; i <= n; i++) {
                cout << fixed << setprecision(6) << values[i].a * left + values[i].b << ' ';
            }
        }
    }
    else {
//        assert(false);
        cout << "NO" << '\n';
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2816 KB answer = YES
4 Correct 1 ms 2748 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 1 ms 2652 KB answer = YES
7 Correct 1 ms 2648 KB answer = YES
8 Correct 1 ms 2652 KB answer = YES
9 Correct 1 ms 2652 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2652 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 1 ms 2652 KB answer = YES
16 Correct 1 ms 2652 KB answer = YES
17 Correct 1 ms 2736 KB answer = YES
18 Correct 1 ms 2652 KB answer = YES
19 Incorrect 1 ms 2648 KB jury has the better answer: jans = YES, pans = NO
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2816 KB answer = YES
4 Correct 1 ms 2748 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 1 ms 2652 KB answer = YES
7 Correct 1 ms 2648 KB answer = YES
8 Correct 1 ms 2652 KB answer = YES
9 Correct 1 ms 2652 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2652 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 1 ms 2652 KB answer = YES
16 Correct 1 ms 2652 KB answer = YES
17 Correct 1 ms 2736 KB answer = YES
18 Correct 1 ms 2652 KB answer = YES
19 Incorrect 1 ms 2648 KB jury has the better answer: jans = YES, pans = NO
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2816 KB answer = YES
4 Correct 1 ms 2748 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 1 ms 2652 KB answer = YES
7 Correct 1 ms 2648 KB answer = YES
8 Correct 1 ms 2652 KB answer = YES
9 Correct 1 ms 2652 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2652 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 1 ms 2652 KB answer = YES
16 Correct 1 ms 2652 KB answer = YES
17 Correct 1 ms 2736 KB answer = YES
18 Correct 1 ms 2652 KB answer = YES
19 Incorrect 1 ms 2648 KB jury has the better answer: jans = YES, pans = NO
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2816 KB answer = YES
4 Correct 1 ms 2748 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 1 ms 2652 KB answer = YES
7 Correct 1 ms 2648 KB answer = YES
8 Correct 1 ms 2652 KB answer = YES
9 Correct 1 ms 2652 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2652 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 1 ms 2652 KB answer = YES
16 Correct 1 ms 2652 KB answer = YES
17 Correct 1 ms 2736 KB answer = YES
18 Correct 1 ms 2652 KB answer = YES
19 Incorrect 1 ms 2648 KB jury has the better answer: jans = YES, pans = NO
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2816 KB answer = YES
4 Correct 1 ms 2748 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 1 ms 2652 KB answer = YES
7 Correct 1 ms 2648 KB answer = YES
8 Correct 1 ms 2652 KB answer = YES
9 Correct 1 ms 2652 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2652 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 1 ms 2652 KB answer = YES
16 Correct 1 ms 2652 KB answer = YES
17 Correct 1 ms 2736 KB answer = YES
18 Correct 1 ms 2652 KB answer = YES
19 Incorrect 1 ms 2648 KB jury has the better answer: jans = YES, pans = NO
20 Halted 0 ms 0 KB -