Submission #605071

# Submission time Handle Problem Language Result Execution time Memory
605071 2022-07-25T12:44:43 Z OttoTheDino Graph (BOI20_graph) C++17
100 / 100
172 ms 37852 KB
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i,s,e)                  for (ll i = s; i <= e; ++i)
#define pb                          push_back
#define all(a)                      a.begin(), a.end()
#define len(a)                      (ll)a.size()
typedef long long ll;
typedef vector<pair<ll,ll>> vpll;
 
const ll mx = 3e5;

vpll cur, adj[mx+1];
ll coeff[mx+1], ans[mx+1], suc = 1, been[mx+1], c[mx+1];
set<ll> st;

void dfs (ll u, ll p) {
    cur.pb({coeff[u], u});
    for (auto [v,e] : adj[u]) {
        if (e==p) continue;
        if (been[v]) {
            if (been[v]==been[u]) st.insert(been[u]*(c[e]-coeff[u]-coeff[v])/2);
            else if (coeff[u]+coeff[v]!=c[e]) suc = 0;
            continue;
        }
        been[v] = been[u]*-1;
        coeff[v] = c[e]-coeff[u];
        dfs (v,e);
    }
}
 
int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    ll n, m; cin >> n >> m;
 
    rep (i,1,m) {
        ll u, v; cin >> u >> v;
        adj[u].pb({v,i});
        adj[v].pb({u,i});
        cin >> c[i];
        c[i] *= 2;
    }

    rep (i,1,n) {
        if (been[i]) continue;
        been[i] = 1;
        dfs (i,0);
        ll x = -1;
        if (len(st)>1) {
            suc =0;
            break;
        }
        else if (len(st)==1) x = *st.begin();
        else {
            vpll changes;
            ll sum = 0, d = 0, best = 5e18;
            for (auto [val, u] : cur) {
                ll imp = -val*been[u];
                sum += -been[u]*val;
                d--;
                changes.pb({imp, u});
            }
            sort(all(changes));
            for (auto [imp, u] : changes) {
                ll cand = sum + d * imp;
                sum -= 2*-been[u]*coeff[u];
                d+=2;
                if (cand<best) {
                    best = cand;
                    x = imp;
                }
            }
        }
        for (auto [val, u] : cur) ans[u] = val+been[u]*x;
        cur.clear();
        st.clear();
    }
 
    if (suc) {
        cout << "YES\n";
        rep (i,1,n) cout << ans[i]/2.0 << " \n"[i==n];
    }
    else cout << "NO\n";
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB answer = YES
2 Correct 4 ms 7380 KB answer = YES
3 Correct 5 ms 7380 KB answer = YES
4 Correct 3 ms 7380 KB answer = NO
5 Correct 4 ms 7380 KB answer = YES
6 Correct 4 ms 7380 KB answer = YES
7 Correct 4 ms 7380 KB answer = YES
8 Correct 4 ms 7380 KB answer = YES
9 Correct 4 ms 7388 KB answer = NO
10 Correct 4 ms 7380 KB answer = YES
11 Correct 4 ms 7380 KB answer = YES
12 Correct 4 ms 7380 KB answer = NO
13 Correct 4 ms 7380 KB answer = YES
14 Correct 5 ms 7364 KB answer = YES
15 Correct 4 ms 7380 KB answer = YES
16 Correct 4 ms 7380 KB answer = YES
17 Correct 4 ms 7380 KB answer = YES
18 Correct 4 ms 7324 KB answer = YES
19 Correct 3 ms 7380 KB answer = YES
20 Correct 4 ms 7380 KB answer = YES
21 Correct 4 ms 7380 KB answer = YES
22 Correct 4 ms 7340 KB answer = NO
23 Correct 5 ms 7380 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB answer = YES
2 Correct 4 ms 7380 KB answer = YES
3 Correct 5 ms 7380 KB answer = YES
4 Correct 3 ms 7380 KB answer = NO
5 Correct 4 ms 7380 KB answer = YES
6 Correct 4 ms 7380 KB answer = YES
7 Correct 4 ms 7380 KB answer = YES
8 Correct 4 ms 7380 KB answer = YES
9 Correct 4 ms 7388 KB answer = NO
10 Correct 4 ms 7380 KB answer = YES
11 Correct 4 ms 7380 KB answer = YES
12 Correct 4 ms 7380 KB answer = NO
13 Correct 4 ms 7380 KB answer = YES
14 Correct 5 ms 7364 KB answer = YES
15 Correct 4 ms 7380 KB answer = YES
16 Correct 4 ms 7380 KB answer = YES
17 Correct 4 ms 7380 KB answer = YES
18 Correct 4 ms 7324 KB answer = YES
19 Correct 3 ms 7380 KB answer = YES
20 Correct 4 ms 7380 KB answer = YES
21 Correct 4 ms 7380 KB answer = YES
22 Correct 4 ms 7340 KB answer = NO
23 Correct 5 ms 7380 KB answer = NO
24 Correct 4 ms 7380 KB answer = YES
25 Correct 5 ms 7380 KB answer = YES
26 Correct 4 ms 7384 KB answer = YES
27 Correct 4 ms 7380 KB answer = YES
28 Correct 4 ms 7380 KB answer = YES
29 Correct 4 ms 7380 KB answer = YES
30 Correct 4 ms 7380 KB answer = NO
31 Correct 4 ms 7372 KB answer = YES
32 Correct 4 ms 7380 KB answer = YES
33 Correct 4 ms 7380 KB answer = YES
34 Correct 4 ms 7508 KB answer = YES
35 Correct 4 ms 7380 KB answer = YES
36 Correct 6 ms 7380 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB answer = YES
2 Correct 4 ms 7380 KB answer = YES
3 Correct 5 ms 7380 KB answer = YES
4 Correct 3 ms 7380 KB answer = NO
5 Correct 4 ms 7380 KB answer = YES
6 Correct 4 ms 7380 KB answer = YES
7 Correct 4 ms 7380 KB answer = YES
8 Correct 4 ms 7380 KB answer = YES
9 Correct 4 ms 7388 KB answer = NO
10 Correct 4 ms 7380 KB answer = YES
11 Correct 4 ms 7380 KB answer = YES
12 Correct 4 ms 7380 KB answer = NO
13 Correct 4 ms 7380 KB answer = YES
14 Correct 5 ms 7364 KB answer = YES
15 Correct 4 ms 7380 KB answer = YES
16 Correct 4 ms 7380 KB answer = YES
17 Correct 4 ms 7380 KB answer = YES
18 Correct 4 ms 7324 KB answer = YES
19 Correct 3 ms 7380 KB answer = YES
20 Correct 4 ms 7380 KB answer = YES
21 Correct 4 ms 7380 KB answer = YES
22 Correct 4 ms 7340 KB answer = NO
23 Correct 5 ms 7380 KB answer = NO
24 Correct 4 ms 7380 KB answer = YES
25 Correct 5 ms 7380 KB answer = YES
26 Correct 4 ms 7384 KB answer = YES
27 Correct 4 ms 7380 KB answer = YES
28 Correct 4 ms 7380 KB answer = YES
29 Correct 4 ms 7380 KB answer = YES
30 Correct 4 ms 7380 KB answer = NO
31 Correct 4 ms 7372 KB answer = YES
32 Correct 4 ms 7380 KB answer = YES
33 Correct 4 ms 7380 KB answer = YES
34 Correct 4 ms 7508 KB answer = YES
35 Correct 4 ms 7380 KB answer = YES
36 Correct 6 ms 7380 KB answer = YES
37 Correct 4 ms 7372 KB answer = YES
38 Correct 4 ms 7380 KB answer = YES
39 Correct 6 ms 7380 KB answer = YES
40 Correct 5 ms 7520 KB answer = YES
41 Correct 4 ms 7508 KB answer = NO
42 Correct 5 ms 7508 KB answer = YES
43 Correct 5 ms 7508 KB answer = YES
44 Correct 4 ms 7508 KB answer = YES
45 Correct 4 ms 7516 KB answer = YES
46 Correct 4 ms 7380 KB answer = YES
47 Correct 4 ms 7508 KB answer = YES
48 Correct 4 ms 7516 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB answer = YES
2 Correct 4 ms 7380 KB answer = YES
3 Correct 5 ms 7380 KB answer = YES
4 Correct 3 ms 7380 KB answer = NO
5 Correct 4 ms 7380 KB answer = YES
6 Correct 4 ms 7380 KB answer = YES
7 Correct 4 ms 7380 KB answer = YES
8 Correct 4 ms 7380 KB answer = YES
9 Correct 4 ms 7388 KB answer = NO
10 Correct 4 ms 7380 KB answer = YES
11 Correct 4 ms 7380 KB answer = YES
12 Correct 4 ms 7380 KB answer = NO
13 Correct 4 ms 7380 KB answer = YES
14 Correct 5 ms 7364 KB answer = YES
15 Correct 4 ms 7380 KB answer = YES
16 Correct 4 ms 7380 KB answer = YES
17 Correct 4 ms 7380 KB answer = YES
18 Correct 4 ms 7324 KB answer = YES
19 Correct 3 ms 7380 KB answer = YES
20 Correct 4 ms 7380 KB answer = YES
21 Correct 4 ms 7380 KB answer = YES
22 Correct 4 ms 7340 KB answer = NO
23 Correct 5 ms 7380 KB answer = NO
24 Correct 4 ms 7380 KB answer = YES
25 Correct 5 ms 7380 KB answer = YES
26 Correct 4 ms 7384 KB answer = YES
27 Correct 4 ms 7380 KB answer = YES
28 Correct 4 ms 7380 KB answer = YES
29 Correct 4 ms 7380 KB answer = YES
30 Correct 4 ms 7380 KB answer = NO
31 Correct 4 ms 7372 KB answer = YES
32 Correct 4 ms 7380 KB answer = YES
33 Correct 4 ms 7380 KB answer = YES
34 Correct 4 ms 7508 KB answer = YES
35 Correct 4 ms 7380 KB answer = YES
36 Correct 6 ms 7380 KB answer = YES
37 Correct 4 ms 7372 KB answer = YES
38 Correct 4 ms 7380 KB answer = YES
39 Correct 6 ms 7380 KB answer = YES
40 Correct 5 ms 7520 KB answer = YES
41 Correct 4 ms 7508 KB answer = NO
42 Correct 5 ms 7508 KB answer = YES
43 Correct 5 ms 7508 KB answer = YES
44 Correct 4 ms 7508 KB answer = YES
45 Correct 4 ms 7516 KB answer = YES
46 Correct 4 ms 7380 KB answer = YES
47 Correct 4 ms 7508 KB answer = YES
48 Correct 4 ms 7516 KB answer = YES
49 Correct 13 ms 8800 KB answer = YES
50 Correct 12 ms 9180 KB answer = YES
51 Correct 13 ms 9296 KB answer = YES
52 Correct 9 ms 9296 KB answer = NO
53 Correct 5 ms 7452 KB answer = YES
54 Correct 5 ms 7764 KB answer = YES
55 Correct 8 ms 8076 KB answer = YES
56 Correct 12 ms 8804 KB answer = YES
57 Correct 12 ms 8396 KB answer = YES
58 Correct 12 ms 8460 KB answer = YES
59 Correct 10 ms 8404 KB answer = YES
60 Correct 11 ms 8592 KB answer = YES
61 Correct 7 ms 8020 KB answer = YES
62 Correct 73 ms 21564 KB answer = NO
63 Correct 63 ms 21576 KB answer = YES
64 Correct 61 ms 21516 KB answer = NO
65 Correct 66 ms 21628 KB answer = YES
66 Correct 5 ms 7640 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB answer = YES
2 Correct 4 ms 7380 KB answer = YES
3 Correct 5 ms 7380 KB answer = YES
4 Correct 3 ms 7380 KB answer = NO
5 Correct 4 ms 7380 KB answer = YES
6 Correct 4 ms 7380 KB answer = YES
7 Correct 4 ms 7380 KB answer = YES
8 Correct 4 ms 7380 KB answer = YES
9 Correct 4 ms 7388 KB answer = NO
10 Correct 4 ms 7380 KB answer = YES
11 Correct 4 ms 7380 KB answer = YES
12 Correct 4 ms 7380 KB answer = NO
13 Correct 4 ms 7380 KB answer = YES
14 Correct 5 ms 7364 KB answer = YES
15 Correct 4 ms 7380 KB answer = YES
16 Correct 4 ms 7380 KB answer = YES
17 Correct 4 ms 7380 KB answer = YES
18 Correct 4 ms 7324 KB answer = YES
19 Correct 3 ms 7380 KB answer = YES
20 Correct 4 ms 7380 KB answer = YES
21 Correct 4 ms 7380 KB answer = YES
22 Correct 4 ms 7340 KB answer = NO
23 Correct 5 ms 7380 KB answer = NO
24 Correct 4 ms 7380 KB answer = YES
25 Correct 5 ms 7380 KB answer = YES
26 Correct 4 ms 7384 KB answer = YES
27 Correct 4 ms 7380 KB answer = YES
28 Correct 4 ms 7380 KB answer = YES
29 Correct 4 ms 7380 KB answer = YES
30 Correct 4 ms 7380 KB answer = NO
31 Correct 4 ms 7372 KB answer = YES
32 Correct 4 ms 7380 KB answer = YES
33 Correct 4 ms 7380 KB answer = YES
34 Correct 4 ms 7508 KB answer = YES
35 Correct 4 ms 7380 KB answer = YES
36 Correct 6 ms 7380 KB answer = YES
37 Correct 4 ms 7372 KB answer = YES
38 Correct 4 ms 7380 KB answer = YES
39 Correct 6 ms 7380 KB answer = YES
40 Correct 5 ms 7520 KB answer = YES
41 Correct 4 ms 7508 KB answer = NO
42 Correct 5 ms 7508 KB answer = YES
43 Correct 5 ms 7508 KB answer = YES
44 Correct 4 ms 7508 KB answer = YES
45 Correct 4 ms 7516 KB answer = YES
46 Correct 4 ms 7380 KB answer = YES
47 Correct 4 ms 7508 KB answer = YES
48 Correct 4 ms 7516 KB answer = YES
49 Correct 13 ms 8800 KB answer = YES
50 Correct 12 ms 9180 KB answer = YES
51 Correct 13 ms 9296 KB answer = YES
52 Correct 9 ms 9296 KB answer = NO
53 Correct 5 ms 7452 KB answer = YES
54 Correct 5 ms 7764 KB answer = YES
55 Correct 8 ms 8076 KB answer = YES
56 Correct 12 ms 8804 KB answer = YES
57 Correct 12 ms 8396 KB answer = YES
58 Correct 12 ms 8460 KB answer = YES
59 Correct 10 ms 8404 KB answer = YES
60 Correct 11 ms 8592 KB answer = YES
61 Correct 7 ms 8020 KB answer = YES
62 Correct 73 ms 21564 KB answer = NO
63 Correct 63 ms 21576 KB answer = YES
64 Correct 61 ms 21516 KB answer = NO
65 Correct 66 ms 21628 KB answer = YES
66 Correct 5 ms 7640 KB answer = YES
67 Correct 91 ms 31288 KB answer = YES
68 Correct 80 ms 31292 KB answer = YES
69 Correct 73 ms 29376 KB answer = YES
70 Correct 112 ms 37852 KB answer = YES
71 Correct 79 ms 29624 KB answer = YES
72 Correct 111 ms 20832 KB answer = YES
73 Correct 87 ms 18800 KB answer = YES
74 Correct 58 ms 20436 KB answer = YES
75 Correct 39 ms 20944 KB answer = NO
76 Correct 13 ms 8912 KB answer = YES
77 Correct 26 ms 10720 KB answer = YES
78 Correct 40 ms 13336 KB answer = YES
79 Correct 88 ms 19312 KB answer = YES
80 Correct 70 ms 20932 KB answer = YES
81 Correct 48 ms 23880 KB answer = NO
82 Correct 91 ms 28784 KB answer = YES
83 Correct 110 ms 30092 KB answer = YES
84 Correct 120 ms 31868 KB answer = YES
85 Correct 91 ms 31292 KB answer = YES
86 Correct 78 ms 29536 KB answer = YES
87 Correct 77 ms 21992 KB answer = NO
88 Correct 109 ms 23040 KB answer = YES
89 Correct 94 ms 16996 KB answer = YES
90 Correct 82 ms 16732 KB answer = YES
91 Correct 94 ms 17408 KB answer = YES
92 Correct 52 ms 13216 KB answer = YES
93 Correct 41 ms 13180 KB answer = YES
94 Correct 79 ms 31932 KB answer = NO
95 Correct 44 ms 17100 KB answer = NO
96 Correct 172 ms 32860 KB answer = YES
97 Correct 50 ms 31344 KB answer = NO