Submission #1017766

# Submission time Handle Problem Language Result Execution time Memory
1017766 2024-07-09T10:03:51 Z VMaksimoski008 Graph (BOI20_graph) C++17
100 / 100
447 ms 51012 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;

int n, m;
vector<pii> graph[maxn+5];
vector<int> val(maxn+5), coef(maxn+5), vis(maxn+5);
vector<int> bad(maxn+5);
vector<double> ans(maxn+5);
vector<vector<int> > C;
map<pii, int> mp;

vector<array<int, 3> > E;
void dfs(int u, int p) {
    C.back().push_back(u);
    vis[u] = 1;

    for(auto &[v, w] : graph[u]) {
        if(v == p) continue;
        E.push_back({ u, v, w });
        if(vis[v]) continue;
        if(coef[u] == 1) coef[v] = -1;
        else coef[v] = 1;
        val[v] = w - val[u];
        dfs(v, u);
    }
}

//da vidam dali e do NO
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    vector<array<int, 3> > edges;
    for(int i=0; i<m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        if(a > b) swap(a, b);
        if(a == b) bad[a] = c;

        if(mp[{a, b}] & (1 << (c - 1))) continue;
        mp[{a, b}] |= (1 << (c - 1));

        if(mp[{a, b}] == 3) {
            cout << "NO\n";
            return 0;
        }

        graph[a].push_back({ b, c });
        graph[b].push_back({ a, c });
        edges.push_back({ a, b, c });
    }

    for(int i=1; i<=n; i++) {
        if(vis[i]) continue;
        C.push_back({ });
        E.clear();
        coef[i] = 1;
        dfs(i, i);
        // for(auto &x : C.back()) cout << "(" << coef[x] << " , " << val[x] << ") ";
        // cout << '\n';
        set<double> s;

        for(auto &[a, b, c] : E) {
            ll sC = coef[a] + coef[b];
            ll sV = val[a] + val[b];

            if(sC == 0) {
                if(sV != c) {
                    cout << "NO\n";
                    return 0;
                }
                continue;
            }

            double res = double(c - sV) / (double)sC;
            s.insert(res);
        }

        if(s.size() > 1) {
            cout << "NO\n";
            return 0;
        }

        int cnt = 0;
        for(auto &x : C.back()) if(bad[x]) cnt = x;


        if(s.size() == 1) {
            double res = *s.begin();
            for(int &x : C.back()) {
                if(bad[x]) {
                    if(2 * coef[x] * res + 2 * val[x] != bad[x]) {
                        cout << "NO\n";
                        return 0;
                    }
                }
            }

            for(auto &x : C.back())
                ans[x] = coef[x] * res + val[x];

            for(auto &[a, b, c] : E) {
                if(ans[a] + ans[b] != c) {
                    cout << "NO\n";
                    return 0;
                }
            }
        } else {
            if(cnt) {
                double V = (double)bad[cnt] / 2;
                double res = (V - val[cnt]) / coef[cnt];

                for(auto &[a, b, c] : E) {
                    if(coef[a] * res + val[a] + coef[b] * res + val[b] != c) {
                        cout << "NO\n";
                        return 0;
                    }
                }

                for(auto &x : C.back()) {
                    ans[x] = coef[x] * res + val[x];
                }
            } else {
                double V = -1e9, total = 1e18;

                for(double i=-100; i<=100; i+=0.5) {
                    double total2 = 0;
                    for(int &x : C.back()) total2 += abs(coef[x] * i + val[x]);
                    bool ok = 1;

                    for(auto &[a, b, c] : E) {
                        if(coef[a] * i + val[a] + coef[b] * i + val[b] != c) {
                            ok = 0;
                            break;
                        }
                    }

                    for(auto &x : C.back()) {
                        if(bad[x]) {
                            if(2 * coef[x] * i + 2 * val[x] != bad[x]) {
                                cout << "NO\n";
                                return 0;
                            }
                        }
                    }
                    
                    // cout << i << ": " << total2 << " " << total << " " << V << '\n';
                    if(ok && total2 < total) {
                        V = i;
                        total = total2;
                    }
                }

                if(V == -1e9) {
                    cout << "NO\n";
                    return 0;
                }

                for(int &x : C.back()) ans[x] = coef[x] * V + val[x];
            }
        }
    }

    // for(int i=1; i<=3; i++) cout << coef[i] << " " << val[i] << '\n'; 
    cout << "YES\n";
    for(int i=1; i<=n; i++) cout << ans[i] << " ";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5176 KB answer = YES
2 Correct 2 ms 5172 KB answer = YES
3 Correct 2 ms 5172 KB answer = YES
4 Correct 1 ms 4956 KB answer = NO
5 Correct 2 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 1 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 1 ms 4956 KB answer = YES
14 Correct 2 ms 4952 KB answer = YES
15 Correct 3 ms 4956 KB answer = YES
16 Correct 2 ms 5128 KB answer = YES
17 Correct 1 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 2 ms 4952 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 5176 KB answer = YES
2 Correct 2 ms 5172 KB answer = YES
3 Correct 2 ms 5172 KB answer = YES
4 Correct 1 ms 4956 KB answer = NO
5 Correct 2 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 1 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 1 ms 4956 KB answer = YES
14 Correct 2 ms 4952 KB answer = YES
15 Correct 3 ms 4956 KB answer = YES
16 Correct 2 ms 5128 KB answer = YES
17 Correct 1 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 2 ms 4952 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 2 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 5024 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5336 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5176 KB answer = YES
2 Correct 2 ms 5172 KB answer = YES
3 Correct 2 ms 5172 KB answer = YES
4 Correct 1 ms 4956 KB answer = NO
5 Correct 2 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 1 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 1 ms 4956 KB answer = YES
14 Correct 2 ms 4952 KB answer = YES
15 Correct 3 ms 4956 KB answer = YES
16 Correct 2 ms 5128 KB answer = YES
17 Correct 1 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 2 ms 4952 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 2 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 5024 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5336 KB answer = YES
37 Correct 3 ms 5180 KB answer = YES
38 Correct 3 ms 4956 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5212 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 4 ms 5276 KB answer = YES
43 Correct 6 ms 5212 KB answer = YES
44 Correct 4 ms 5208 KB answer = YES
45 Correct 2 ms 5212 KB answer = YES
46 Correct 3 ms 5184 KB answer = YES
47 Correct 3 ms 5212 KB answer = YES
48 Correct 2 ms 5212 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5176 KB answer = YES
2 Correct 2 ms 5172 KB answer = YES
3 Correct 2 ms 5172 KB answer = YES
4 Correct 1 ms 4956 KB answer = NO
5 Correct 2 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 1 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 1 ms 4956 KB answer = YES
14 Correct 2 ms 4952 KB answer = YES
15 Correct 3 ms 4956 KB answer = YES
16 Correct 2 ms 5128 KB answer = YES
17 Correct 1 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 2 ms 4952 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 2 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 5024 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5336 KB answer = YES
37 Correct 3 ms 5180 KB answer = YES
38 Correct 3 ms 4956 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5212 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 4 ms 5276 KB answer = YES
43 Correct 6 ms 5212 KB answer = YES
44 Correct 4 ms 5208 KB answer = YES
45 Correct 2 ms 5212 KB answer = YES
46 Correct 3 ms 5184 KB answer = YES
47 Correct 3 ms 5212 KB answer = YES
48 Correct 2 ms 5212 KB answer = YES
49 Correct 30 ms 6744 KB answer = YES
50 Correct 12 ms 7220 KB answer = YES
51 Correct 29 ms 7184 KB answer = YES
52 Correct 8 ms 7004 KB answer = NO
53 Correct 5 ms 5212 KB answer = YES
54 Correct 7 ms 5376 KB answer = YES
55 Correct 15 ms 5988 KB answer = YES
56 Correct 28 ms 6692 KB answer = YES
57 Correct 25 ms 6232 KB answer = YES
58 Correct 23 ms 6504 KB answer = YES
59 Correct 10 ms 6492 KB answer = YES
60 Correct 27 ms 6488 KB answer = YES
61 Correct 6 ms 5980 KB answer = YES
62 Correct 10 ms 6772 KB answer = NO
63 Correct 227 ms 34156 KB answer = YES
64 Correct 194 ms 35008 KB answer = NO
65 Correct 206 ms 33844 KB answer = YES
66 Correct 7 ms 5464 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5176 KB answer = YES
2 Correct 2 ms 5172 KB answer = YES
3 Correct 2 ms 5172 KB answer = YES
4 Correct 1 ms 4956 KB answer = NO
5 Correct 2 ms 4952 KB answer = YES
6 Correct 2 ms 4956 KB answer = YES
7 Correct 1 ms 4956 KB answer = YES
8 Correct 2 ms 4956 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 1 ms 4956 KB answer = YES
14 Correct 2 ms 4952 KB answer = YES
15 Correct 3 ms 4956 KB answer = YES
16 Correct 2 ms 5128 KB answer = YES
17 Correct 1 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 2 ms 4952 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 2 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 5024 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5336 KB answer = YES
37 Correct 3 ms 5180 KB answer = YES
38 Correct 3 ms 4956 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5212 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 4 ms 5276 KB answer = YES
43 Correct 6 ms 5212 KB answer = YES
44 Correct 4 ms 5208 KB answer = YES
45 Correct 2 ms 5212 KB answer = YES
46 Correct 3 ms 5184 KB answer = YES
47 Correct 3 ms 5212 KB answer = YES
48 Correct 2 ms 5212 KB answer = YES
49 Correct 30 ms 6744 KB answer = YES
50 Correct 12 ms 7220 KB answer = YES
51 Correct 29 ms 7184 KB answer = YES
52 Correct 8 ms 7004 KB answer = NO
53 Correct 5 ms 5212 KB answer = YES
54 Correct 7 ms 5376 KB answer = YES
55 Correct 15 ms 5988 KB answer = YES
56 Correct 28 ms 6692 KB answer = YES
57 Correct 25 ms 6232 KB answer = YES
58 Correct 23 ms 6504 KB answer = YES
59 Correct 10 ms 6492 KB answer = YES
60 Correct 27 ms 6488 KB answer = YES
61 Correct 6 ms 5980 KB answer = YES
62 Correct 10 ms 6772 KB answer = NO
63 Correct 227 ms 34156 KB answer = YES
64 Correct 194 ms 35008 KB answer = NO
65 Correct 206 ms 33844 KB answer = YES
66 Correct 7 ms 5464 KB answer = YES
67 Correct 260 ms 29732 KB answer = YES
68 Correct 262 ms 29428 KB answer = YES
69 Correct 86 ms 29476 KB answer = YES
70 Correct 167 ms 51012 KB answer = YES
71 Correct 84 ms 29700 KB answer = YES
72 Correct 447 ms 20740 KB answer = YES
73 Correct 116 ms 20720 KB answer = YES
74 Correct 92 ms 19468 KB answer = YES
75 Correct 57 ms 19216 KB answer = NO
76 Correct 32 ms 6964 KB answer = YES
77 Correct 82 ms 8944 KB answer = YES
78 Correct 141 ms 12020 KB answer = YES
79 Correct 342 ms 18836 KB answer = YES
80 Correct 251 ms 19744 KB answer = YES
81 Correct 93 ms 24836 KB answer = NO
82 Correct 153 ms 29192 KB answer = YES
83 Correct 131 ms 29632 KB answer = YES
84 Correct 445 ms 29752 KB answer = YES
85 Correct 254 ms 29820 KB answer = YES
86 Correct 84 ms 29692 KB answer = YES
87 Correct 101 ms 20536 KB answer = NO
88 Correct 174 ms 22756 KB answer = YES
89 Correct 115 ms 18948 KB answer = YES
90 Correct 154 ms 18940 KB answer = YES
91 Correct 103 ms 18912 KB answer = YES
92 Correct 53 ms 12808 KB answer = YES
93 Correct 79 ms 12752 KB answer = YES
94 Correct 98 ms 29436 KB answer = NO
95 Correct 72 ms 17176 KB answer = NO
96 Correct 273 ms 43840 KB answer = YES
97 Correct 66 ms 29396 KB answer = NO