Submission #1303439

#TimeUsernameProblemLanguageResultExecution timeMemory
1303439hypersphereGraph (BOI20_graph)C++20
5 / 100
2 ms348 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r) {
    return uniform_int_distribution<ll>(l, r)(rng);
}

const ll INF = 1e18;
const ll mod = 1e9 + 22071997;

ll binpow(ll base, ll exp, ll mod) {
    ll ans = 1;
    ll mult = base;
    while(exp) {
        if(exp & 1) ans = (ans * mult) % mod;
        mult = (mult * mult) % mod;
        exp >>= 1;
    }
    return ans;
} 

ll gcd(ll a, ll b) {
    if(b == 0) return a;
    return gcd(b, a%b);
}

const int N = 1e5 + 5;
int n, m;
vector<pair<int, double>> adj[N];
bool vis[N];
double val[N];

void gather_nodes(vector<int>& nodes, int node) {
    vis[node] = true;
    nodes.push_back(node);

    for(pair<int, int> v : adj[node]) {
        if(vis[v.first]) continue;
        gather_nodes(nodes, v.first);
    }
}

bool valid = true;
void spread_val(int node) {
    vis[node] = true;
    for(pair<int, int> v : adj[node]) {
        if(!vis[v.first]) {
            val[v.first] = v.second - val[node];
            spread_val(v.first);
        } else if(val[v.first] + val[node] != v.second) valid = false;
    }
}

bool valid_for_whole_graph = true;
void solve_component(int node) {
    vector<int> nodes;
    gather_nodes(nodes, node);

    for(int i : nodes) {
        vis[i] = false;
        val[i] = -69;
    }

    double max_ans = 1e9;
    double optimal_value = -69;
    static vector<double> candidates = {-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2};

    for(double start_val : candidates) {
        //cout << start_val << "\n";
        val[node] = start_val;
        valid = true;
        spread_val(node);

        // for(int i = 1; i <= n; i++) {
        //     cout << val[i] << " ";
        // }
        // cout << "\n";

        if(!valid) {
            for(int i : nodes) {
                vis[i] = false;
                val[i] = -69;
            }
            continue;
        }
        double ans = 0;
        for(int i : nodes) ans += abs(val[i]);
        if(ans < max_ans) {
            max_ans = ans;
            optimal_value = start_val;
        }

        for(int i : nodes) {
            vis[i] = false;
            val[i] = -69;
        }
    }

    if(max_ans >= 1e8) {
        valid_for_whole_graph = false;
        return;
    } 

    val[node] = optimal_value;
    spread_val(node);
}


void solve() {
    memset(vis, 0, sizeof(vis));
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        int a, b, t;
        cin >> a >> b >> t;
        adj[a].push_back({b, t});
        adj[b].push_back({a, t});
    }

    for(int i = 1; i <= n; i++) {
        if(vis[i]) continue;
        solve_component(i);
        if(!valid_for_whole_graph) break;
    }

    if(!valid_for_whole_graph) {
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
    for(int i = 1; i <= n; i++) cout << val[i] << " ";
}

int main() {
    //freopen("COLLECT.INP", "r", stdin);
    //freopen("COLLECT.OUT", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int tests = 1;
    //cin >> tests;

    while(tests--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...