Submission #254803

#TimeUsernameProblemLanguageResultExecution timeMemory
2548032fat2codeGraph (BOI20_graph)C++17
100 / 100
375 ms23956 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define sz() size()
#define fr first
#define sc second
#define int long long
#define mp make_pair
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
using namespace std;
 
const int nmax = 100005;
const long double eps = 1e-12;
 
long double ans[nmax];
int n,m;
 
struct varf{
    int coef,cons;
}arr[nmax];
 
int AAA = 0;
long double tz = 0;
 
vector<pair<int,int>>nod[nmax];
vector<int>vecc;
bitset<nmax>viz;
bitset<nmax>viz2;
 
void DFS(int s){
    vecc.push_back(s);
    viz[s] = 1;
    for(auto it : nod[s]){
        if(!viz[it.fr]){
            arr[it.fr].coef = -arr[s].coef;
            if(it.sc == 1LL){
                arr[it.fr].cons = 1LL - arr[s].cons;
            }
            else{
                arr[it.fr].cons = 2LL - arr[s].cons;
            }
            DFS(it.fr);
        }
        else{
            int currcons = 0LL;
            if(it.sc == 1LL){
                currcons = 1LL - arr[s].cons;
            }
            else{
                currcons = 2LL - arr[s].cons;
            }
            int currcoef = -arr[s].coef;
            if(arr[it.fr].coef == currcoef && arr[it.fr].cons != currcons){
                rcc("NO");
            }
            else if(currcoef != arr[it.fr].coef){
                AAA = 1;
                tz = (long double)(arr[it.fr].cons - currcons)/(currcoef - arr[it.fr].coef);
            }
        }
    }
}
 
void check(int l,int r,int val){
    long double lmao = (long double) (arr[l].coef * tz + arr[l].cons);
    lmao += (long double) (arr[r].coef * tz + arr[r].cons);
    lmao = (long double) (lmao - val);
    if(abs(lmao) >= eps){
        rcc("NO");
    }
}
 
map<int,int>q;
 
void CHECK12(int s){
    viz2[s] = 1;
    for(auto it : nod[s]){
        check(it.fr,s,it.sc);
        if(!viz2[it.fr]){
            CHECK12(it.fr);
        }
    }
}
 
int32_t main(){
    cout << fixed << setprecision(13);
    cin >> n >> m;
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin >> x >> y >> z;
        nod[x].push_back({y,z});
        nod[y].push_back({x,z});
    }
    for(int i=1;i<=n;i++){
        if(viz[i] == 0){
            AAA = 0;
            arr[i].cons = 0;
            arr[i].coef = 1;
            vecc.clear();
            DFS(i);
            if(AAA == 1){
                CHECK12(i);
                for(auto it : vecc){
                    ans[it] = (long double)arr[it].coef * tz + arr[it].cons;
                }
            }
            else{
                q.clear();
                for(auto it : vecc){
                    if(arr[it].coef < 0LL){
                        q[arr[it].cons]++;
                    }
                    else{
                        q[-arr[it].cons]++;
                    }
                }
                auto it = q.begin();
                int nec = ((int)vecc.size() + 1)/2;
                int curr = (it->sc);
                int anslmao = 0;
                while(true){
                    if(curr >= nec){
                        anslmao = (it->fr);
                        break;
                    }
                    it++;
                    if(it == q.end()){
                        break;
                    }
                    else{
                        curr += (it->sc);
                    }
                }
                for(auto it : vecc){
                    ans[it] = anslmao * arr[it].coef + arr[it].cons;
                }
            }
        }
    }
    cout << "YES" << '\n';
    for(int i=1;i<=n;i++) cout << ans[i] << ' ';
}
#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...