답안 #902439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
902439 2024-01-10T12:03:06 Z GrindMachine Graph (BOI20_graph) C++17
0 / 100
2 ms 5000 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;
            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);
                pll px = {s1-s2,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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 5000 KB answer = YES
4 Correct 2 ms 4956 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Incorrect 2 ms 4956 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 5000 KB answer = YES
4 Correct 2 ms 4956 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Incorrect 2 ms 4956 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 5000 KB answer = YES
4 Correct 2 ms 4956 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Incorrect 2 ms 4956 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 5000 KB answer = YES
4 Correct 2 ms 4956 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Incorrect 2 ms 4956 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4956 KB answer = YES
3 Correct 2 ms 5000 KB answer = YES
4 Correct 2 ms 4956 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Incorrect 2 ms 4956 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -