Submission #902442

# Submission time Handle Problem Language Result Execution time Memory
902442 2024-01-10T12:11:28 Z GrindMachine Graph (BOI20_graph) C++17
100 / 100
159 ms 27308 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
 
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
 
template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}
 
template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
 
/*
 
 
 
*/
 
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
 
vector<pll> adj[N];
vector<bool> vis(N);
vector<pll> val(N);
vector<ll> curr_nodes;
pll fix = {-1,-1};
bool ok = true;
 
void dfs1(ll u){
    if(fix.ff != -1) return;
 
    curr_nodes.pb(u);
    vis[u] = 1;
 
    for(auto [v,w] : adj[u]){
        if(fix.ff != -1) return;
        pll want = {-val[u].ff,w-val[u].ss};
        if(vis[v]){
            if(want != val[v]){
                if(want.ff == val[v].ff){
                    ok = false;
                }
                else{
                    auto p1 = want, p2 = val[v];
                    if(p1.ff != 1) swap(p1,p2);
                    assert(p1.ff == 1);
                    assert(p2.ff == -1);
                    ll x = p2.ss-p1.ss;
                    fix = {v,x};
                    return;
                }
            }
        }
        else{
            val[v] = want;
            dfs1(v);
        }
    }
}
 
vector<ll> ans(N);
 
void dfs2(ll u){
    vis[u] = 1;
    for(auto [v,w] : adj[u]){
        ll want = w*2-ans[u];
        if(vis[v]){
            if(ans[v] != want){
                ok = false;
            }
        }
        else{
            ans[v] = want;
            dfs2(v);
        }
    }
}
 
void solve(int test_case)
{
    ll n,m; cin >> n >> m;
    rep1(i,m){
        ll u,v,w; cin >> u >> v >> w;
        adj[u].pb({v,w}), adj[v].pb({u,w});
    }
 
    rep1(i,n){
        if(vis[i]) conts;
        curr_nodes.clear();
        fix = {-1,-1};
        val[i] = {1,0};
        dfs1(i);
 
        if(fix.ff != -1){
            trav(u,curr_nodes){
                vis[u] = 0;
            }
            curr_nodes.clear();
            ans[i] = fix.ss;
            dfs2(i);
            conts;
        }
 
        vector<pll> vals;
        trav(u,curr_nodes){
            vis[u] = 0;
            vals.pb(val[u]);
        }
    
        vector<ll> here[3];
        for(auto [m,c] : vals){
            here[m+1].pb(c);
        }
        rep(j,3){
            sort(all(here[j]));
        }

        vector<ll> pref[3];
        for(int m : {-1,1}){
            pref[m+1].pb(0);
            trav(x,here[m+1]){
                pref[m+1].pb(pref[m+1].back()+x);
            }
        }

        pll best = {inf2,inf2};
 
        for(auto [k1,k2] : vals){
            ll x = -k2/k1;
            ll sum = 0;

            for(int m : {-1,1}){
                ll pos = lower_bound(all(here[m+1]),-m*x)-here[m+1].begin()+1;
                ll siz = sz(here[m+1]);
                ll s1 = pref[m+1].back()-pref[m+1][pos-1]+m*x*(siz-pos+1);
                ll s2 = pref[m+1][pos-1]+m*x*(pos-1);
                sum += s1-s2;
            }

            pll px = {sum,x};
            amin(best,px);
        }

        ans[i] = best.ss*2;
        dfs2(i);
    }
 
    if(!ok){
        no;
        return;
    }
    
    cout << fixed << setprecision(11);

    yes;
    rep1(i,n) cout << (ld)ans[i]/2 << " ";
    cout << endl;    
}
 
int main()
{
    fastio;
 
    int t = 1;
    // cin >> t;
 
    rep1(i, t) {
        solve(i);
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4964 KB answer = YES
3 Correct 2 ms 4968 KB answer = YES
4 Correct 2 ms 4968 KB answer = NO
5 Correct 2 ms 4968 KB answer = YES
6 Correct 2 ms 4980 KB answer = YES
7 Correct 2 ms 5196 KB answer = YES
8 Correct 2 ms 4968 KB answer = YES
9 Correct 3 ms 5200 KB answer = NO
10 Correct 3 ms 4968 KB answer = YES
11 Correct 3 ms 4968 KB answer = YES
12 Correct 3 ms 4972 KB answer = NO
13 Correct 2 ms 4968 KB answer = YES
14 Correct 2 ms 4968 KB answer = YES
15 Correct 2 ms 4968 KB answer = YES
16 Correct 2 ms 4968 KB answer = YES
17 Correct 2 ms 4968 KB answer = YES
18 Correct 2 ms 4968 KB answer = YES
19 Correct 2 ms 4968 KB answer = YES
20 Correct 2 ms 4968 KB answer = YES
21 Correct 3 ms 4968 KB answer = YES
22 Correct 2 ms 4968 KB answer = NO
23 Correct 2 ms 4968 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4964 KB answer = YES
3 Correct 2 ms 4968 KB answer = YES
4 Correct 2 ms 4968 KB answer = NO
5 Correct 2 ms 4968 KB answer = YES
6 Correct 2 ms 4980 KB answer = YES
7 Correct 2 ms 5196 KB answer = YES
8 Correct 2 ms 4968 KB answer = YES
9 Correct 3 ms 5200 KB answer = NO
10 Correct 3 ms 4968 KB answer = YES
11 Correct 3 ms 4968 KB answer = YES
12 Correct 3 ms 4972 KB answer = NO
13 Correct 2 ms 4968 KB answer = YES
14 Correct 2 ms 4968 KB answer = YES
15 Correct 2 ms 4968 KB answer = YES
16 Correct 2 ms 4968 KB answer = YES
17 Correct 2 ms 4968 KB answer = YES
18 Correct 2 ms 4968 KB answer = YES
19 Correct 2 ms 4968 KB answer = YES
20 Correct 2 ms 4968 KB answer = YES
21 Correct 3 ms 4968 KB answer = YES
22 Correct 2 ms 4968 KB answer = NO
23 Correct 2 ms 4968 KB answer = NO
24 Correct 2 ms 4968 KB answer = YES
25 Correct 2 ms 4968 KB answer = YES
26 Correct 2 ms 4968 KB answer = YES
27 Correct 2 ms 4956 KB answer = YES
28 Correct 2 ms 4960 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4968 KB answer = YES
33 Correct 2 ms 4960 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5000 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4964 KB answer = YES
3 Correct 2 ms 4968 KB answer = YES
4 Correct 2 ms 4968 KB answer = NO
5 Correct 2 ms 4968 KB answer = YES
6 Correct 2 ms 4980 KB answer = YES
7 Correct 2 ms 5196 KB answer = YES
8 Correct 2 ms 4968 KB answer = YES
9 Correct 3 ms 5200 KB answer = NO
10 Correct 3 ms 4968 KB answer = YES
11 Correct 3 ms 4968 KB answer = YES
12 Correct 3 ms 4972 KB answer = NO
13 Correct 2 ms 4968 KB answer = YES
14 Correct 2 ms 4968 KB answer = YES
15 Correct 2 ms 4968 KB answer = YES
16 Correct 2 ms 4968 KB answer = YES
17 Correct 2 ms 4968 KB answer = YES
18 Correct 2 ms 4968 KB answer = YES
19 Correct 2 ms 4968 KB answer = YES
20 Correct 2 ms 4968 KB answer = YES
21 Correct 3 ms 4968 KB answer = YES
22 Correct 2 ms 4968 KB answer = NO
23 Correct 2 ms 4968 KB answer = NO
24 Correct 2 ms 4968 KB answer = YES
25 Correct 2 ms 4968 KB answer = YES
26 Correct 2 ms 4968 KB answer = YES
27 Correct 2 ms 4956 KB answer = YES
28 Correct 2 ms 4960 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4968 KB answer = YES
33 Correct 2 ms 4960 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5000 KB answer = YES
37 Correct 3 ms 5208 KB answer = YES
38 Correct 2 ms 4968 KB answer = YES
39 Correct 3 ms 5224 KB answer = YES
40 Correct 2 ms 5224 KB answer = YES
41 Correct 3 ms 5224 KB answer = NO
42 Correct 2 ms 5224 KB answer = YES
43 Correct 4 ms 5224 KB answer = YES
44 Correct 3 ms 5224 KB answer = YES
45 Correct 3 ms 5224 KB answer = YES
46 Correct 2 ms 5224 KB answer = YES
47 Correct 2 ms 5224 KB answer = YES
48 Correct 3 ms 5224 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4964 KB answer = YES
3 Correct 2 ms 4968 KB answer = YES
4 Correct 2 ms 4968 KB answer = NO
5 Correct 2 ms 4968 KB answer = YES
6 Correct 2 ms 4980 KB answer = YES
7 Correct 2 ms 5196 KB answer = YES
8 Correct 2 ms 4968 KB answer = YES
9 Correct 3 ms 5200 KB answer = NO
10 Correct 3 ms 4968 KB answer = YES
11 Correct 3 ms 4968 KB answer = YES
12 Correct 3 ms 4972 KB answer = NO
13 Correct 2 ms 4968 KB answer = YES
14 Correct 2 ms 4968 KB answer = YES
15 Correct 2 ms 4968 KB answer = YES
16 Correct 2 ms 4968 KB answer = YES
17 Correct 2 ms 4968 KB answer = YES
18 Correct 2 ms 4968 KB answer = YES
19 Correct 2 ms 4968 KB answer = YES
20 Correct 2 ms 4968 KB answer = YES
21 Correct 3 ms 4968 KB answer = YES
22 Correct 2 ms 4968 KB answer = NO
23 Correct 2 ms 4968 KB answer = NO
24 Correct 2 ms 4968 KB answer = YES
25 Correct 2 ms 4968 KB answer = YES
26 Correct 2 ms 4968 KB answer = YES
27 Correct 2 ms 4956 KB answer = YES
28 Correct 2 ms 4960 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4968 KB answer = YES
33 Correct 2 ms 4960 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5000 KB answer = YES
37 Correct 3 ms 5208 KB answer = YES
38 Correct 2 ms 4968 KB answer = YES
39 Correct 3 ms 5224 KB answer = YES
40 Correct 2 ms 5224 KB answer = YES
41 Correct 3 ms 5224 KB answer = NO
42 Correct 2 ms 5224 KB answer = YES
43 Correct 4 ms 5224 KB answer = YES
44 Correct 3 ms 5224 KB answer = YES
45 Correct 3 ms 5224 KB answer = YES
46 Correct 2 ms 5224 KB answer = YES
47 Correct 2 ms 5224 KB answer = YES
48 Correct 3 ms 5224 KB answer = YES
49 Correct 12 ms 6372 KB answer = YES
50 Correct 14 ms 6280 KB answer = YES
51 Correct 11 ms 6600 KB answer = YES
52 Correct 8 ms 6616 KB answer = NO
53 Correct 3 ms 5208 KB answer = YES
54 Correct 4 ms 5476 KB answer = YES
55 Correct 6 ms 5724 KB answer = YES
56 Correct 14 ms 6276 KB answer = YES
57 Correct 9 ms 5992 KB answer = YES
58 Correct 13 ms 5992 KB answer = YES
59 Correct 8 ms 5920 KB answer = YES
60 Correct 9 ms 6136 KB answer = YES
61 Correct 5 ms 5468 KB answer = YES
62 Correct 44 ms 16736 KB answer = NO
63 Correct 46 ms 16976 KB answer = YES
64 Correct 59 ms 16948 KB answer = NO
65 Correct 54 ms 16936 KB answer = YES
66 Correct 4 ms 5212 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4964 KB answer = YES
3 Correct 2 ms 4968 KB answer = YES
4 Correct 2 ms 4968 KB answer = NO
5 Correct 2 ms 4968 KB answer = YES
6 Correct 2 ms 4980 KB answer = YES
7 Correct 2 ms 5196 KB answer = YES
8 Correct 2 ms 4968 KB answer = YES
9 Correct 3 ms 5200 KB answer = NO
10 Correct 3 ms 4968 KB answer = YES
11 Correct 3 ms 4968 KB answer = YES
12 Correct 3 ms 4972 KB answer = NO
13 Correct 2 ms 4968 KB answer = YES
14 Correct 2 ms 4968 KB answer = YES
15 Correct 2 ms 4968 KB answer = YES
16 Correct 2 ms 4968 KB answer = YES
17 Correct 2 ms 4968 KB answer = YES
18 Correct 2 ms 4968 KB answer = YES
19 Correct 2 ms 4968 KB answer = YES
20 Correct 2 ms 4968 KB answer = YES
21 Correct 3 ms 4968 KB answer = YES
22 Correct 2 ms 4968 KB answer = NO
23 Correct 2 ms 4968 KB answer = NO
24 Correct 2 ms 4968 KB answer = YES
25 Correct 2 ms 4968 KB answer = YES
26 Correct 2 ms 4968 KB answer = YES
27 Correct 2 ms 4956 KB answer = YES
28 Correct 2 ms 4960 KB answer = YES
29 Correct 2 ms 4956 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 2 ms 4952 KB answer = YES
32 Correct 2 ms 4968 KB answer = YES
33 Correct 2 ms 4960 KB answer = YES
34 Correct 2 ms 4956 KB answer = YES
35 Correct 2 ms 4956 KB answer = YES
36 Correct 2 ms 5000 KB answer = YES
37 Correct 3 ms 5208 KB answer = YES
38 Correct 2 ms 4968 KB answer = YES
39 Correct 3 ms 5224 KB answer = YES
40 Correct 2 ms 5224 KB answer = YES
41 Correct 3 ms 5224 KB answer = NO
42 Correct 2 ms 5224 KB answer = YES
43 Correct 4 ms 5224 KB answer = YES
44 Correct 3 ms 5224 KB answer = YES
45 Correct 3 ms 5224 KB answer = YES
46 Correct 2 ms 5224 KB answer = YES
47 Correct 2 ms 5224 KB answer = YES
48 Correct 3 ms 5224 KB answer = YES
49 Correct 12 ms 6372 KB answer = YES
50 Correct 14 ms 6280 KB answer = YES
51 Correct 11 ms 6600 KB answer = YES
52 Correct 8 ms 6616 KB answer = NO
53 Correct 3 ms 5208 KB answer = YES
54 Correct 4 ms 5476 KB answer = YES
55 Correct 6 ms 5724 KB answer = YES
56 Correct 14 ms 6276 KB answer = YES
57 Correct 9 ms 5992 KB answer = YES
58 Correct 13 ms 5992 KB answer = YES
59 Correct 8 ms 5920 KB answer = YES
60 Correct 9 ms 6136 KB answer = YES
61 Correct 5 ms 5468 KB answer = YES
62 Correct 44 ms 16736 KB answer = NO
63 Correct 46 ms 16976 KB answer = YES
64 Correct 59 ms 16948 KB answer = NO
65 Correct 54 ms 16936 KB answer = YES
66 Correct 4 ms 5212 KB answer = YES
67 Correct 102 ms 21960 KB answer = YES
68 Correct 77 ms 22084 KB answer = YES
69 Correct 81 ms 19732 KB answer = YES
70 Correct 132 ms 27308 KB answer = YES
71 Correct 69 ms 19652 KB answer = YES
72 Correct 93 ms 16036 KB answer = YES
73 Correct 81 ms 13536 KB answer = YES
74 Correct 66 ms 12852 KB answer = YES
75 Correct 38 ms 14988 KB answer = NO
76 Correct 11 ms 6360 KB answer = YES
77 Correct 25 ms 7764 KB answer = YES
78 Correct 42 ms 9828 KB answer = YES
79 Correct 75 ms 14660 KB answer = YES
80 Correct 63 ms 15184 KB answer = YES
81 Correct 34 ms 14024 KB answer = NO
82 Correct 99 ms 17668 KB answer = YES
83 Correct 142 ms 20452 KB answer = YES
84 Correct 139 ms 22572 KB answer = YES
85 Correct 86 ms 22080 KB answer = YES
86 Correct 75 ms 19908 KB answer = YES
87 Correct 76 ms 14668 KB answer = NO
88 Correct 159 ms 16540 KB answer = YES
89 Correct 89 ms 12556 KB answer = YES
90 Correct 87 ms 12528 KB answer = YES
91 Correct 78 ms 12900 KB answer = YES
92 Correct 37 ms 9420 KB answer = YES
93 Correct 57 ms 9420 KB answer = YES
94 Correct 72 ms 22596 KB answer = NO
95 Correct 51 ms 11512 KB answer = NO
96 Correct 159 ms 22420 KB answer = YES
97 Correct 39 ms 21932 KB answer = NO