Submission #1020139

# Submission time Handle Problem Language Result Execution time Memory
1020139 2024-07-11T15:33:46 Z Fatonim Graph (BOI20_graph) C++17
100 / 100
237 ms 49616 KB
// "We create our own demons"
#include <bits/stdc++.h>

using namespace std;

#ifdef ONPC
#include "debug.h"
#else
#define dbg(...)
#endif

#define int long long
#define ll long long
#define ld long double
#define pi pair<int, int>
// vector
#define sz(a) (int)((a).size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxpos(x) (max_element(all(x)) - x.begin())
#define minpos(x) (min_element(all(x)) - x.begin())
#define rev(x) (reverse(all(x)))
// bits
#define popcount(n) __builtin_popcountll(n)
#define clz(n) __builtin_clzll(n)
// math
#define sqr(n) ((n) * (n))
#define divup(a, b) (((a) + (b)-1) / (b))
// ostream
#define Fixed(a) cout << fixed << setprecision(12) << a;

template <class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; }

template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_queue = priority_queue<T, vector<T>, less<T>>;

template <class T> using V = vector<T>;
using vi = V<int>;
using vd = V<ld>;
using vb = V<bool>;
using vpi = V<pi>;
using vvi = V<vi>;
using vvb = V<vb>;

const int mod = 1e9 + 7; // 998244353 1e9 + 7
const ll inf = (int)(1e18) + 7;
const int inf_s = 1e9 + 7;
const ld eps = 1e-9;

const int B = 32;
const int N = 1000 + 3;
const int logn = 20;
const int maxn = 2e5 + 7;
/////////////////////////solve/////////////////////////

map <pi, int> e;
vpi g[maxn];
bool used[maxn];

void solve() {
    int n, m;
    cin >> n >> m;

    V<pair<int, ld>> x(n, {0, 0});

    bool good = 1;
    for (int i = 0; i < m; ++i) {
        int v, u, c;
        cin >> v >> u >> c;
        --v; --u;
        if (v > u) swap(v, u);
        if (e[{v, u}] != 0 && e[{v, u}] != c) good = 0;
        e[{v, u}] = c;

        g[v].push_back({u, c});
        g[u].push_back({v, c});
    }

    vi t;
    auto fill_val = [&](ld val) {
        for (auto v : t) {
            x[v].second += x[v].first * val;
            x[v].first = 0;
        }
    };

    function<void(int)> dfs = [&](int v) {
        used[v] = 1;
        t.push_back(v);
        for (auto& [u, c] : g[v]) {
            if (!good) return;

            pair<int, ld> xu = {-x[v].first, c - x[v].second};
            if (!used[u]) {
                x[u] = xu;
                dfs(u);
            }
            else {
                if (x[u] == xu) continue;
                ld a = x[u].first - xu.first;
                ld b = xu.second - x[u].second;
                if (a == 0) good = 0;
                else {
                    ld val = b / a;
                    fill_val(val);
                }
            }
        }
    };

    for (int i = 0; i < n; ++i) {
        if (used[i]) continue;
        x[i] = {1, 0};

        dfs(i);
        dbg(t);
        dbg(x);
        if (x[i].first != 0) {
            vd m;
            for (auto v : t) {
                if (x[v].first == 1) m.push_back(-x[v].second);
                else m.push_back(x[v].second);
            }
            sort(all(m));
            dbg(m);
            fill_val(m[sz(m) / 2]);
        }

        t.clear();
    }

    if (!good) {
        cout << "NO\n";
        return;
    }

    cout << "YES\n";
    for (int i = 0; i < n; ++i) {
        cout << x[i].second << " ";
    }
}

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

#ifdef ONPC
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif

    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
#ifdef ONPC
        cerr << "===========" << i << "===========" << '\n';
#endif
        solve();
    }

#ifdef ONPC
    cerr << endl << "Time " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 3 ms 5112 KB answer = NO
5 Correct 3 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 3 ms 4956 KB answer = NO
10 Correct 3 ms 4956 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5096 KB answer = YES
14 Correct 3 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4956 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 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 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 3 ms 5112 KB answer = NO
5 Correct 3 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 3 ms 4956 KB answer = NO
10 Correct 3 ms 4956 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5096 KB answer = YES
14 Correct 3 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4956 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 3 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 4 ms 4952 KB answer = YES
28 Correct 2 ms 4952 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 5164 KB answer = NO
31 Correct 3 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5156 KB answer = YES
34 Correct 3 ms 4956 KB answer = YES
35 Correct 2 ms 5104 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 3 ms 5112 KB answer = NO
5 Correct 3 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 3 ms 4956 KB answer = NO
10 Correct 3 ms 4956 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5096 KB answer = YES
14 Correct 3 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4956 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 3 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 4 ms 4952 KB answer = YES
28 Correct 2 ms 4952 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 5164 KB answer = NO
31 Correct 3 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5156 KB answer = YES
34 Correct 3 ms 4956 KB answer = YES
35 Correct 2 ms 5104 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
37 Correct 2 ms 4952 KB answer = YES
38 Correct 2 ms 4956 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5164 KB answer = YES
41 Correct 3 ms 5208 KB answer = NO
42 Correct 3 ms 5212 KB answer = YES
43 Correct 3 ms 5212 KB answer = YES
44 Correct 3 ms 5168 KB answer = YES
45 Correct 3 ms 5212 KB answer = YES
46 Correct 4 ms 5212 KB answer = YES
47 Correct 4 ms 5208 KB answer = YES
48 Correct 3 ms 5208 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 3 ms 5112 KB answer = NO
5 Correct 3 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 3 ms 4956 KB answer = NO
10 Correct 3 ms 4956 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5096 KB answer = YES
14 Correct 3 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4956 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 3 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 4 ms 4952 KB answer = YES
28 Correct 2 ms 4952 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 5164 KB answer = NO
31 Correct 3 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5156 KB answer = YES
34 Correct 3 ms 4956 KB answer = YES
35 Correct 2 ms 5104 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
37 Correct 2 ms 4952 KB answer = YES
38 Correct 2 ms 4956 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5164 KB answer = YES
41 Correct 3 ms 5208 KB answer = NO
42 Correct 3 ms 5212 KB answer = YES
43 Correct 3 ms 5212 KB answer = YES
44 Correct 3 ms 5168 KB answer = YES
45 Correct 3 ms 5212 KB answer = YES
46 Correct 4 ms 5212 KB answer = YES
47 Correct 4 ms 5208 KB answer = YES
48 Correct 3 ms 5208 KB answer = YES
49 Correct 12 ms 7004 KB answer = YES
50 Correct 11 ms 7260 KB answer = YES
51 Correct 13 ms 7516 KB answer = YES
52 Correct 8 ms 6748 KB answer = NO
53 Correct 3 ms 5360 KB answer = YES
54 Correct 4 ms 5468 KB answer = YES
55 Correct 7 ms 6152 KB answer = YES
56 Correct 12 ms 7004 KB answer = YES
57 Correct 12 ms 6748 KB answer = YES
58 Correct 11 ms 6748 KB answer = YES
59 Correct 10 ms 6492 KB answer = YES
60 Correct 11 ms 7000 KB answer = YES
61 Correct 6 ms 5976 KB answer = YES
62 Correct 155 ms 28628 KB answer = NO
63 Correct 164 ms 29524 KB answer = YES
64 Correct 154 ms 29492 KB answer = NO
65 Correct 177 ms 29616 KB answer = YES
66 Correct 4 ms 5464 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 4956 KB answer = YES
4 Correct 3 ms 5112 KB answer = NO
5 Correct 3 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 2 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 3 ms 4956 KB answer = NO
10 Correct 3 ms 4956 KB answer = YES
11 Correct 3 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5096 KB answer = YES
14 Correct 3 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4956 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 2 ms 4956 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 3 ms 4956 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4956 KB answer = YES
27 Correct 4 ms 4952 KB answer = YES
28 Correct 2 ms 4952 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 5164 KB answer = NO
31 Correct 3 ms 4956 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 3 ms 5156 KB answer = YES
34 Correct 3 ms 4956 KB answer = YES
35 Correct 2 ms 5104 KB answer = YES
36 Correct 3 ms 4956 KB answer = YES
37 Correct 2 ms 4952 KB answer = YES
38 Correct 2 ms 4956 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5164 KB answer = YES
41 Correct 3 ms 5208 KB answer = NO
42 Correct 3 ms 5212 KB answer = YES
43 Correct 3 ms 5212 KB answer = YES
44 Correct 3 ms 5168 KB answer = YES
45 Correct 3 ms 5212 KB answer = YES
46 Correct 4 ms 5212 KB answer = YES
47 Correct 4 ms 5208 KB answer = YES
48 Correct 3 ms 5208 KB answer = YES
49 Correct 12 ms 7004 KB answer = YES
50 Correct 11 ms 7260 KB answer = YES
51 Correct 13 ms 7516 KB answer = YES
52 Correct 8 ms 6748 KB answer = NO
53 Correct 3 ms 5360 KB answer = YES
54 Correct 4 ms 5468 KB answer = YES
55 Correct 7 ms 6152 KB answer = YES
56 Correct 12 ms 7004 KB answer = YES
57 Correct 12 ms 6748 KB answer = YES
58 Correct 11 ms 6748 KB answer = YES
59 Correct 10 ms 6492 KB answer = YES
60 Correct 11 ms 7000 KB answer = YES
61 Correct 6 ms 5976 KB answer = YES
62 Correct 155 ms 28628 KB answer = NO
63 Correct 164 ms 29524 KB answer = YES
64 Correct 154 ms 29492 KB answer = NO
65 Correct 177 ms 29616 KB answer = YES
66 Correct 4 ms 5464 KB answer = YES
67 Correct 92 ms 32812 KB answer = YES
68 Correct 86 ms 32848 KB answer = YES
69 Correct 85 ms 31060 KB answer = YES
70 Correct 136 ms 49616 KB answer = YES
71 Correct 83 ms 31180 KB answer = YES
72 Correct 131 ms 23892 KB answer = YES
73 Correct 117 ms 22224 KB answer = YES
74 Correct 69 ms 20724 KB answer = YES
75 Correct 55 ms 18512 KB answer = NO
76 Correct 15 ms 7512 KB answer = YES
77 Correct 28 ms 9820 KB answer = YES
78 Correct 64 ms 13488 KB answer = YES
79 Correct 104 ms 21580 KB answer = YES
80 Correct 74 ms 21756 KB answer = YES
81 Correct 89 ms 26316 KB answer = NO
82 Correct 118 ms 30476 KB answer = YES
83 Correct 117 ms 31432 KB answer = YES
84 Correct 132 ms 33060 KB answer = YES
85 Correct 95 ms 32852 KB answer = YES
86 Correct 88 ms 31320 KB answer = YES
87 Correct 103 ms 24652 KB answer = NO
88 Correct 133 ms 26056 KB answer = YES
89 Correct 104 ms 20816 KB answer = YES
90 Correct 108 ms 20820 KB answer = YES
91 Correct 101 ms 21196 KB answer = YES
92 Correct 53 ms 13780 KB answer = YES
93 Correct 71 ms 13724 KB answer = YES
94 Correct 94 ms 33100 KB answer = NO
95 Correct 72 ms 20732 KB answer = NO
96 Correct 237 ms 40556 KB answer = YES
97 Correct 63 ms 32848 KB answer = NO