Submission #482946

# Submission time Handle Problem Language Result Execution time Memory
482946 2021-10-27T03:16:12 Z sberens Graph (BOI20_graph) C++17
0 / 100
1 ms 316 KB
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename K> using hset = gp_hash_table<K, null_type>;
template <typename K, typename V> using hmap = gp_hash_table<K, V>;


using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define smax(x, y) (x = max(x, y))
#define smin(x, y) (x = min(x, y))

using ll = long long;
using ld = long double;

template <typename T>
using vv = vector<vector<T>>;

using vi = vector<int>;
using ii = array<int, 2>;
using vii = vector<ii>;
using vvi = vv<int>;

using vll = vector<ll>;
using l2 = array<ll, 2>;
using vl2 = vector<l2>;
using vvll = vv<ll>;

template <typename T>
using minq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxq = priority_queue<T>;

const ll M = 1000000007;

int n, m;
vv<ii> g;
using valt = ii;
vector<set<valt>> coefs;
vi vis;

tuple<bool, vvi> solve() {
    vvi comps;
    for (int i = 1; i <= n; ++i) {
        if (vis[i]) continue;
        comps.pb({});
        queue<tuple<int, valt>> q; // node, val
        q.push({i, {1, 0}});
        while (!q.empty()) {
            auto [u, val] = q.front(); q.pop();
            coefs[u].insert(val);
            if (vis[u]) {
                if (coefs[u].size() > 2) return {false, {}};
                continue;
            }
            vis[u] = true;
            comps.back().pb(u);
            auto [a, b] = val;
            for (auto [v, c]: g[u]) {
                q.push({v, {-a, c-b}});
            }
        }

    }
    return {true, comps};
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    g.assign(n + 1, vii{});
    coefs.assign(n + 1, set<valt>{});
    vis.assign(n + 1, false);

    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].pb({b, c});
        g[b].pb({a, c});
    }
    auto [poss, comps] = solve();
    if (poss) {
        cout << "YES\n";
        vector<long double> vals(n + 1);
        for (const auto & comp : comps) {
            vi pts;
            long double x;
            bool fixed = false;
            for (int u : comp) {
                if (coefs[u].size() == 2) {
                    auto [a1, b1] = *coefs[u].begin();
                    auto [a2, b2] = *(++coefs[u].begin());
                    long double tmp = (b2 - b1) / (0.l + a1 - a2);
                    if ((a1 == a2) || (fixed && x != tmp)) {
                        cout << "NO\n";
                        exit(0);
                    }
                    x = tmp;
                    fixed = true;
                }

                auto [a, b] = *coefs[u].begin();
                pts.pb(-a * b);
            }
            if (!fixed) {
                sort(all(pts));
                if (pts.size() % 2 == 1) x = pts[pts.size() / 2];
                else x = (pts[pts.size() / 2 - 1] + pts[pts.size() / 2]) / 2.l;
            }
            for (int u : comp) {
                auto [a, b] = *coefs[u].begin();
                vals[u] = a * x + b;
            }
        }
        for (int i = 1; i <= n; ++i) {
            cout << vals[i] << ' ';
        }
        cout << '\n';
    } else {
        cout << "NO\n";
    }

}

Compilation message

Graph.cpp: In function 'int main()':
Graph.cpp:99:51: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   99 |                     if ((a1 == a2) || (fixed && x != tmp)) {
      |                                                 ~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 0 ms 312 KB answer = YES
3 Correct 0 ms 204 KB answer = YES
4 Correct 0 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 0 ms 204 KB answer = YES
7 Correct 1 ms 204 KB answer = YES
8 Correct 1 ms 316 KB answer = YES
9 Incorrect 0 ms 204 KB Expected double, but "NO" found
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 0 ms 312 KB answer = YES
3 Correct 0 ms 204 KB answer = YES
4 Correct 0 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 0 ms 204 KB answer = YES
7 Correct 1 ms 204 KB answer = YES
8 Correct 1 ms 316 KB answer = YES
9 Incorrect 0 ms 204 KB Expected double, but "NO" found
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 0 ms 312 KB answer = YES
3 Correct 0 ms 204 KB answer = YES
4 Correct 0 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 0 ms 204 KB answer = YES
7 Correct 1 ms 204 KB answer = YES
8 Correct 1 ms 316 KB answer = YES
9 Incorrect 0 ms 204 KB Expected double, but "NO" found
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 0 ms 312 KB answer = YES
3 Correct 0 ms 204 KB answer = YES
4 Correct 0 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 0 ms 204 KB answer = YES
7 Correct 1 ms 204 KB answer = YES
8 Correct 1 ms 316 KB answer = YES
9 Incorrect 0 ms 204 KB Expected double, but "NO" found
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB answer = YES
2 Correct 0 ms 312 KB answer = YES
3 Correct 0 ms 204 KB answer = YES
4 Correct 0 ms 204 KB answer = NO
5 Correct 1 ms 204 KB answer = YES
6 Correct 0 ms 204 KB answer = YES
7 Correct 1 ms 204 KB answer = YES
8 Correct 1 ms 316 KB answer = YES
9 Incorrect 0 ms 204 KB Expected double, but "NO" found
10 Halted 0 ms 0 KB -