Submission #1092823

#TimeUsernameProblemLanguageResultExecution timeMemory
1092823andrei_iorgulescuGraph (BOI20_graph)C++14
100 / 100
100 ms25028 KiB
#include <bits/stdc++.h>

using namespace std;

using ld = long double;

int inf = 1e9 + 123;

int n, m;
vector<pair<int,int>> g[100005];
vector<vector<int>> cc;
vector<int> cur_cc;
vector<ld> vls;
bool viz[100005];

ld coef[100005], ofs[100005];
ld ans[100005];

bool possible = true;
ld trb_x = inf;

void dfs(int nod)
{
    viz[nod] = true;
    cur_cc.push_back(nod);
    for (auto vecin : g[nod])
    {
        if (!viz[vecin.first])
        {
            coef[vecin.first] = -coef[nod];
            ofs[vecin.first] = vecin.second - ofs[nod];
            dfs(vecin.first);
        }
        else
        {
            ld ncf = -coef[nod];
            ld nofs = vecin.second - ofs[nod];
            if (ncf == coef[vecin.first] and nofs != ofs[vecin.first])
                possible = false;
            else if (ncf != coef[vecin.first])
            {
                //cout << nod << ' ' << vecin.first << ' ' << ncf << ' ' << nofs << ' ' << coef[vecin.first] << ' ' << ofs[vecin.first] << endl;
                ld ex_x = trb_x;
                if (ncf == -1)
                    trb_x = (ld)(nofs - ofs[vecin.first]) / 2.0d;
                else
                    trb_x = (ld)(ofs[vecin.first] - nofs) / 2.0d;
                //cout << ex_x << ' ' << trb_x << endl;
                if (ex_x != trb_x and ex_x != inf)
                    possible = false;
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        cin >> x >> y >> z;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            trb_x = inf;
            cur_cc.clear();
            coef[i] = 1, ofs[i] = 0;
            dfs(i);
            cc.push_back(cur_cc);
            vls.push_back(trb_x);
        }
    }
    if (!possible)
    {
        cout << "NO";
        return 0;
    }
    for (int i = 0; i < cc.size(); i++)
    {
        if (vls[i] != inf)
        {
            for (auto it : cc[i])
                ans[it] = coef[it] * vls[i] + ofs[it];
        }
        else
        {
            vector<ld> pct;
            for (auto it : cc[i])
                pct.push_back(-ofs[it] / coef[it]);
            sort(pct.begin(), pct.end());
            int pz = pct.size() / 2;
            vls[i] = pct[pz];
            for (auto it : cc[i])
                ans[it] = coef[it] * vls[i] + ofs[it];
        }
    }
    cout << "YES\n";
    cout << setprecision(10) << fixed;
    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';
    return 0;
}

/*
3 3
1 2 1
2 3 2
1 3 2
*/

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i = 0; i < cc.size(); 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...