Submission #652431

#TimeUsernameProblemLanguageResultExecution timeMemory
6524311zaid1Graph (BOI20_graph)C++17
58 / 100
1086 ms58868 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n';
 
const int M = 1e5+5, MOD = 1e9+7;
int p[M], d[M], loop, vis[M];
vector<pair<int, int>> node[M];
bitset<100005> is;
vector<int> tmp;
double cost[M];
 
void dfs(int s, int dp = 0) {
    vis[s] = 1;
    d[s] = dp++;
    tmp.push_back(s);
    for (auto [i, x]:node[s]) {
        if (!vis[i]) {
            p[i] = s;
            dfs(i, dp);
        } else if (!loop && (dp-d[i])%2) {
            loop = s;
            p[i] = s;
        }
    }
}
 
double eval(int s, double a, int cnt = 0) {
    double ans = abs(a);
    cost[s] = a;
    is[s] = 1;
    for (auto [i, x]:node[s]) {
        if (!is[i]) ans += abs(eval(i, x-a));
    } return ans;
}
 
bool check(vector<int> v) {
    for (int y:v) {
        for (auto [i, x]:node[y]) {
            if (abs(cost[y] + cost[i]-x) >= 1e-6)  return 0;
        }
    } return true;
}
 
signed main() {
    cin.tie(0)->sync_with_stdio(0);
    
    int n, m;
    cin >> n >> m;
 
    map<pair<int, int>, int> ed;
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        if (a > b) swap(a, b);
        if (ed[{a, b}] && ed[{a, b}] != c) {
            cout << "NO" << endl;
            return 0;
        } else if (ed[{a, b}]) continue;
 
        ed[{a, b}] = ed[{b, a}] = c;
        node[a].push_back({b, c});
        node[b].push_back({a, c});
    }
 
    for (int k = 1; k <= n; k++) {
        if (!vis[k] && node[k].size()) {
            dfs(k);
 
            if (loop) {
                vector<int> lp = {loop};
                for (int x = p[loop]; x != loop; x = p[x]) lp.push_back(x);
                lp.push_back(loop);
 
                double sum = 0;
                for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) {
                    if (f) sum += ed[{min(lp[i], lp[i+1]), max(lp[i], lp[i+1])}];
                    else   sum -= ed[{min(lp[i], lp[i+1]), max(lp[i], lp[i+1])}];
                }
 
                eval(loop, sum/2);
            } else {
                eval(k, 0);
                if (!check(tmp)) {
                    cout << "NO" << endl;
                    return 0;
                }
 
                int  l = -n-1, r = n+1, cnt = 0;
                while (cnt++<60) {
                    int ll = (2*l+r)/3;
                    int rr = (l+2*r)/3;
                    is = 0; double x = eval(k, ll);
                    is = 0; double y = eval(k, rr);
                    if (x >= y) l = ll;
                    else r = rr;
                } is = 0; eval(k, (l+r)/2);
            }
 
            if (!check(tmp)) {
                cout << "NO" << endl;
                return 0;
            }
 
            tmp = {};
            loop = 0;
        }
    }
 
    cout << "YES" << fixed << setprecision(6) << endl;
    for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
 
    return 0;
}
 
/*
3 3
1 1 2
2 2 1
2 3 2
 
4 5
1 4 2
3 2 1
2 1 2
3 4 1
1 3 2
 
 
4 4
1 2 1
2 3 2
1 3 2
3 4 1
 
*/

Compilation message (stderr)

Graph.cpp: In function 'int main()':
Graph.cpp:76:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |                 for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) {
      |                                        ~~^~~~~~~~~~~~~
Graph.cpp:111:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  111 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |     ^~~
Graph.cpp:111:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  111 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |                                                          ^~~~
#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...