Submission #1105878

#TimeUsernameProblemLanguageResultExecution timeMemory
1105878DangKhoizzzzGraph (BOI20_graph)C++14
100 / 100
122 ms30168 KiB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

typedef pair <int , int> pii;

const int maxn = 2e5 + 7;

int n , m;
double ans[maxn];
vector <pii> g[maxn];
bool vis[maxn];
pair <double , double> val[maxn];

vector <int> node;
bool conf;
double found;


void dfs(int u , int p)
{
    vis[u] = 1;
    node.push_back(u);
    for(pii tmp: g[u])
    {

        int v = tmp.fi;
        int w = tmp.se;

        if(v == p) continue;

        if(!vis[v])
        {
            val[v].fi = -1*val[u].fi;
            val[v].se = w - val[u].se;
            dfs(v , u);
        }
        else if(val[v].fi == val[u].fi)
        {
            conf = 1; 
            found = (1.0*(w - val[v].se - val[u].se)/2.0)*val[v].fi;
        }
    }
}

void solve()
{
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int u , v , w; 
        cin >> u >> v >> w;
        g[u].push_back({v , w});
        g[v].push_back({u , w});
    }


    for(int i = 1; i <= n; i++)
    {
        if(!vis[i])
        {
            node.clear();
            conf = 0; 

            val[i].fi = 1.0;
            val[i].se = 0.0;

            dfs(i , 0);

            if(conf == 0)
            {
                vector <int> v;
                for(int u: node) 
                {
                    if(val[u].fi < 0) v.push_back(val[u].se);
                    else v.push_back(val[u].se*-1.0);
                }
                sort(v.begin() , v.end());

                found = v[((int)(v.size()) - 1)/2];

                //cout << found << '\n';
            }

            for(int u: node) 
            {
                ans[u] = found*val[u].fi + val[u].se;
            }
        }
    }

    for(int u = 1; u <= n; u++)
    {
        for(pii tmp: g[u])
        {
            int v = tmp.fi;
            int w = tmp.se;

            if(ans[u] + ans[v] != w)
            {
                cout << "NO" << '\n';
                return;
            }
        }
    }

    cout << "YES" << '\n';

    for(int u = 1; u <= n; u++) cout << ans[u] << ' ';
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //freopen("sol.inp" ,"r" , stdin);
    //freopen("sol.out" , "w" , stdout);
    solve();
    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...