Submission #1168798

#TimeUsernameProblemLanguageResultExecution timeMemory
1168798steveonalexGraph (BOI20_graph)C++20
100 / 100
148 ms21932 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}

ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 1e5 + 69;
int n, m;
vector<pair<int, int>> graph[N];
bool visited[N];
vector<int> ver;
vector<int> candidate;
pair<int, int> dis[N];

void dfs(int u){
    visited[u] = true;
    ver.push_back(u);

    for(pair<int,int> v: graph[u]) {
        if (!visited[v.first]){
            dis[v.first] = {-dis[u].first, v.second - dis[u].second};
            dfs(v.first);
        }
        else{
            pair<int, int> expected = {-dis[u].first, v.second - dis[u].second};
            if (expected != dis[v.first]){
                if (dis[v.first].first == expected.first){
                    cout << "NO\n";
                    exit(0);
                }
                candidate.push_back(2 * (dis[v.first].second - expected.second) / (expected.first - dis[v.first].first));
            }
        }
    }
}

int find_optimal_point(vector<pair<int, int>> dih){
    vector<int> points;
    for(auto &i: dih){
        if (i.first < 0) {
            i.first *= -1;
            i.second *= -1;
        }
        points.push_back(-i.second);
    }

    sort(ALL(points));
    int mid = points.size() / 2;
    return points[mid];
}

void solve(){
    cin >> n >> m;
    for(int i = 0; i < m; ++i){
        int u, v, c; cin >> u >> v >> c;
        graph[u].push_back({v, c});
        graph[v].push_back({u, c});
    }
    cout << fixed << setprecision(6);

    vector<double> ans(n+1);
    for(int i = 1; i <= n; ++i) if (visited[i] == 0){
        dis[i] = {1, 0};
        ver.clear(); candidate.clear();
        dfs(i);

        remove_dup(candidate);

        if (candidate.size() > 1) {
            cout << "NO\n";
            return;
        }
        if (candidate.size() == 0){
            vector<pair<int,int>> dih;
            for(int j: ver) dih.push_back(dis[j]);
            candidate.push_back(2 * find_optimal_point(dih));
        }
        if (candidate.size() == 1){
            double ret = (double)candidate[0] / 2;
            for(int i: ver){
                ans[i] = ret * dis[i].first + dis[i].second;
            }
        }
    }

    cout << "YES\n";
    for(int i = 1; i <= n; ++i) cout << ans[i] << " ";
    cout << "\n";
}

int main(void){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    clock_t start = clock();

    solve();

    cerr << "Time elapsed: " << clock() - start << " ms\n";
    return 0;
}
#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...