답안 #1101941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101941 2024-10-17T08:09:50 Z crafticat Graph (BOI20_graph) C++17
100 / 100
109 ms 17856 KB
#include "bits/stdc++.h"

using namespace std;
template<typename T>
using V = vector<T>;
using vi = V<int>;
using vvi = V<vi>;
using pi = pair<int,int>;
using vpi = V<pi>;

#define F0R(i, n) for(int i = 0; i < n;i++)
#define FOR(i,a, n) for(int i = a; i < n;i++)
#define pb push_back

V<bool> vis;
V<int> nodes;
long double determine = -1;
bool determineInit = false, possible = true;
V<vpi> g;
long double ep = 0.0000001;

struct info {
    int a, m;
    bool init = false;

    info() {

    }
    info(int a, int m) : a(a), m(m) {
        init = true;
    }
    bool operator==(info other) const {
        return a == other.a && m == other.m;
    }
    bool operator!=(info other) {
        return !(a == other.a && m == other.m);
    }
    info operator-(info other) const {
        return {a - other.a,m- other.m};
    }
    long double eval(long double x) {
        return a + m * x;
    }
};
V<info> dat;
V<long double> ans;

void calc(int a, int b, int edge) {
    auto d2 = info(edge,0) - dat[b];
    if (dat[a].m == d2.m && dat[a].a == d2.a) return;
    if (dat[a].m == d2.m && dat[a].a != d2.a) {
        possible = false;
        return;
    }
    long double results = (dat[a].a - d2.a) / ((long double) d2.m - dat[a].m);
    if (determineInit && abs(results - determine) > ep) possible = false;
    else {
        determine = results;
        determineInit = true;
    }
}

void solve(int x, int edge, int par) {
    nodes.pb(x);
    vis[x] = true;

    if (par == -1) dat[x] = info(0,1);
    else {
        dat[x] = info(edge, 0) - dat[par];
    }

    for (auto &[v,cost] : g[x]) {
        if (vis[v]) {
            if (dat[v].init)
                calc(v, x, cost);
            continue;
        }

        solve(v, cost, x);
    }
}

long double eval(long double x) {
    long double result = 0;
    for (auto v : nodes) {
        result += abs(dat[v].eval(x));
    }
    return result;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int n, m; cin >> n >> m;
    g.resize(n + 1);

    F0R(i, m) {
        int a, b, c; cin >> a >> b >> c;
        g[a].pb({b,c});
        g[b].pb({a,c});
    }

    vis.resize(n + 1);
    dat.resize(n + 1);
    ans.resize(n + 1);

    FOR(i, 1, n + 1) {
        if (vis[i]) continue;
        solve(i, -1, -1);

        if (!possible) {
            cout << "NO\n";
            return 0;
        }
        if (!determineInit) {
            // Binary search results
            long double l = -1e9, r = 1e9;
            while (r > l + ep) {
                long double mid = l + (r - l) / 2;
                if (eval(mid + ep) < eval(mid)) {
                    l = mid + ep;
                } else {
                    r = mid;
                }
            }

            determine = l;
        }
        for (auto x : nodes)
            ans[x] = dat[x].eval(determine);

        determine = -1;
        possible = true;
        determineInit = false;

        nodes.clear();
    }

    cout << "YES\n";
    FOR(i, 1, n + 1) {
        cout << fixed << setprecision(6) << ans[i] << " ";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 336 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 504 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 508 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 336 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 504 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 508 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 336 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 336 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 336 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 336 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 504 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 508 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 336 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 336 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 336 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
37 Correct 1 ms 336 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 348 KB answer = YES
40 Correct 1 ms 336 KB answer = YES
41 Correct 1 ms 336 KB answer = NO
42 Correct 1 ms 336 KB answer = YES
43 Correct 2 ms 336 KB answer = YES
44 Correct 1 ms 336 KB answer = YES
45 Correct 1 ms 336 KB answer = YES
46 Correct 1 ms 336 KB answer = YES
47 Correct 1 ms 336 KB answer = YES
48 Correct 1 ms 336 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 336 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 504 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 508 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 336 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 336 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 336 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
37 Correct 1 ms 336 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 348 KB answer = YES
40 Correct 1 ms 336 KB answer = YES
41 Correct 1 ms 336 KB answer = NO
42 Correct 1 ms 336 KB answer = YES
43 Correct 2 ms 336 KB answer = YES
44 Correct 1 ms 336 KB answer = YES
45 Correct 1 ms 336 KB answer = YES
46 Correct 1 ms 336 KB answer = YES
47 Correct 1 ms 336 KB answer = YES
48 Correct 1 ms 336 KB answer = YES
49 Correct 9 ms 1360 KB answer = YES
50 Correct 8 ms 1616 KB answer = YES
51 Correct 9 ms 1616 KB answer = YES
52 Correct 6 ms 1616 KB answer = NO
53 Correct 2 ms 336 KB answer = YES
54 Correct 3 ms 760 KB answer = YES
55 Correct 5 ms 848 KB answer = YES
56 Correct 9 ms 1360 KB answer = YES
57 Correct 8 ms 1360 KB answer = YES
58 Correct 8 ms 1360 KB answer = YES
59 Correct 6 ms 1364 KB answer = YES
60 Correct 9 ms 1364 KB answer = YES
61 Correct 3 ms 848 KB answer = YES
62 Correct 42 ms 5704 KB answer = NO
63 Correct 46 ms 5960 KB answer = YES
64 Correct 42 ms 5712 KB answer = NO
65 Correct 44 ms 5960 KB answer = YES
66 Correct 2 ms 592 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB answer = YES
2 Correct 1 ms 336 KB answer = YES
3 Correct 1 ms 336 KB answer = YES
4 Correct 1 ms 336 KB answer = NO
5 Correct 1 ms 336 KB answer = YES
6 Correct 1 ms 336 KB answer = YES
7 Correct 1 ms 336 KB answer = YES
8 Correct 1 ms 336 KB answer = YES
9 Correct 1 ms 336 KB answer = NO
10 Correct 1 ms 504 KB answer = YES
11 Correct 1 ms 336 KB answer = YES
12 Correct 1 ms 336 KB answer = NO
13 Correct 1 ms 336 KB answer = YES
14 Correct 1 ms 336 KB answer = YES
15 Correct 1 ms 336 KB answer = YES
16 Correct 1 ms 336 KB answer = YES
17 Correct 1 ms 336 KB answer = YES
18 Correct 1 ms 336 KB answer = YES
19 Correct 1 ms 336 KB answer = YES
20 Correct 1 ms 508 KB answer = YES
21 Correct 1 ms 336 KB answer = YES
22 Correct 1 ms 336 KB answer = NO
23 Correct 1 ms 336 KB answer = NO
24 Correct 1 ms 336 KB answer = YES
25 Correct 1 ms 336 KB answer = YES
26 Correct 1 ms 336 KB answer = YES
27 Correct 1 ms 336 KB answer = YES
28 Correct 1 ms 336 KB answer = YES
29 Correct 1 ms 336 KB answer = YES
30 Correct 1 ms 336 KB answer = NO
31 Correct 1 ms 336 KB answer = YES
32 Correct 1 ms 336 KB answer = YES
33 Correct 1 ms 336 KB answer = YES
34 Correct 1 ms 336 KB answer = YES
35 Correct 1 ms 336 KB answer = YES
36 Correct 1 ms 336 KB answer = YES
37 Correct 1 ms 336 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 348 KB answer = YES
40 Correct 1 ms 336 KB answer = YES
41 Correct 1 ms 336 KB answer = NO
42 Correct 1 ms 336 KB answer = YES
43 Correct 2 ms 336 KB answer = YES
44 Correct 1 ms 336 KB answer = YES
45 Correct 1 ms 336 KB answer = YES
46 Correct 1 ms 336 KB answer = YES
47 Correct 1 ms 336 KB answer = YES
48 Correct 1 ms 336 KB answer = YES
49 Correct 9 ms 1360 KB answer = YES
50 Correct 8 ms 1616 KB answer = YES
51 Correct 9 ms 1616 KB answer = YES
52 Correct 6 ms 1616 KB answer = NO
53 Correct 2 ms 336 KB answer = YES
54 Correct 3 ms 760 KB answer = YES
55 Correct 5 ms 848 KB answer = YES
56 Correct 9 ms 1360 KB answer = YES
57 Correct 8 ms 1360 KB answer = YES
58 Correct 8 ms 1360 KB answer = YES
59 Correct 6 ms 1364 KB answer = YES
60 Correct 9 ms 1364 KB answer = YES
61 Correct 3 ms 848 KB answer = YES
62 Correct 42 ms 5704 KB answer = NO
63 Correct 46 ms 5960 KB answer = YES
64 Correct 42 ms 5712 KB answer = NO
65 Correct 44 ms 5960 KB answer = YES
66 Correct 2 ms 592 KB answer = YES
67 Correct 92 ms 14924 KB answer = YES
68 Correct 78 ms 14784 KB answer = YES
69 Correct 55 ms 14672 KB answer = YES
70 Correct 77 ms 17856 KB answer = YES
71 Correct 56 ms 14772 KB answer = YES
72 Correct 84 ms 10688 KB answer = YES
73 Correct 65 ms 10688 KB answer = YES
74 Correct 44 ms 9156 KB answer = YES
75 Correct 21 ms 8396 KB answer = NO
76 Correct 10 ms 1616 KB answer = YES
77 Correct 22 ms 2896 KB answer = YES
78 Correct 39 ms 4808 KB answer = YES
79 Correct 76 ms 9224 KB answer = YES
80 Correct 59 ms 9128 KB answer = YES
81 Correct 28 ms 11632 KB answer = NO
82 Correct 72 ms 14272 KB answer = YES
83 Correct 73 ms 14784 KB answer = YES
84 Correct 109 ms 14784 KB answer = YES
85 Correct 97 ms 14784 KB answer = YES
86 Correct 64 ms 14784 KB answer = YES
87 Correct 31 ms 10064 KB answer = NO
88 Correct 75 ms 11748 KB answer = YES
89 Correct 56 ms 9564 KB answer = YES
90 Correct 55 ms 9572 KB answer = YES
91 Correct 59 ms 9540 KB answer = YES
92 Correct 35 ms 5596 KB answer = YES
93 Correct 31 ms 5592 KB answer = YES
94 Correct 41 ms 13760 KB answer = NO
95 Correct 26 ms 8528 KB answer = NO
96 Correct 106 ms 15808 KB answer = YES
97 Correct 27 ms 13768 KB answer = NO