Submission #651556

# Submission time Handle Problem Language Result Execution time Memory
651556 2022-10-19T10:10:54 Z 1zaid1 Graph (BOI20_graph) C++17
0 / 100
19 ms 35668 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n';

const int M = 5e5+5, MOD = 1e9+7;
vector<array<int, 3>> v;
int p[M], d[M], loop;
vector<int> node[M];
bitset<500005> vis;
map<int, int> e[M];
double cost[M];

void dfs(int s, int dp = 0) {
    vis[s] = 1;
    d[s] = dp++;

    for (int i:node[s]) {
        if (!vis[i]) {
            p[i] = s;
            dfs(i, dp);
        } else if (!loop&&(dp-d[i])%2) {
            p[i] = s;
            loop = s;
            return;
        }
    }
}

double eval(int s, double a) {
    double ans = a;
    cost[s] = a;
    vis[s] = 1;
    for (int i:node[s]) if (!vis[i]) ans += abs(eval(i, e[s][i]-a));
    return ans;
}

bool check() {
    for (auto [a, b, c]:v) if (abs(cost[a] + cost[b] - c) >= 1e-6) return 0;
    return true;
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    
    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        if (e[a][b] && e[a][b] != c) {
            cout << "NO" << endl;
            return 0;
        }

        e[a][b] = c;
        e[b][a] = c;
        node[a].push_back(b);
        node[b].push_back(a);
        v.push_back({a, b, c});
    }

    p[1] = 1;
    dfs(1);
    if (loop) {
        int x = loop;
        vector<int> l = {x};
        while (p[x] != loop) {
            l.push_back(x=p[x]);
        } l.push_back(l[0]);

        double sum = 0;
        for (int i = 0, f = 1; i < l.size()-1; i++, f^=1) {
            if (f) sum += e[l[i]][l[i+1]];
            else sum -= e[l[i]][l[i+1]];
        }

        vis = 0;
        eval(loop, sum/2);
        // cout << "LOOP " << loop <<' '<< sum << endl;
    } else {
        vis = 0;
        eval(1, 0);
        if (!check()) {
            cout << "NO" << endl;
            return 0;
        }

        double l = -1e10, r = 1e10, cnt = 0;
        while (abs(r-l) >= 1e-6 && cnt++<100) {
            double ll = (2*l+r)/3;
            double rr = (l+2*r)/3;
            vis = 0; double x = eval(1, ll);
            vis = 0; double y = eval(1, rr);
            if (x >= y) l = ll;
            else r = rr;
        } vis = 0, eval(1, (l+r)/2);
        // cout << "NO LOOP " << l << endl;
    }

    if (!check()) {
        cout << "NO" << endl;
        // for (auto [a, b, c]:v) if (abs(cost[a] + cost[b] - c) >= 1e-6) cout << cost[a] << ' ' << cost[b] << ' ' << c << endl;
        return 0;
    }

    cout << "YES" << fixed << setprecision(6) << endl;
    for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;

    return 0;
}

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

2 1
1 2 1

3 2
1 2 2
2 3 2

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

Compilation message

Graph.cpp: In function 'int main()':
Graph.cpp:74:34: 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]
   74 |         for (int i = 0, f = 1; i < l.size()-1; i++, f^=1) {
      |                                ~~^~~~~~~~~~~~
Graph.cpp:109:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  109 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |     ^~~
Graph.cpp:109:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  109 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |                                                          ^~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35668 KB answer = YES
2 Correct 19 ms 35624 KB answer = YES
3 Correct 18 ms 35668 KB answer = YES
4 Correct 19 ms 35540 KB answer = NO
5 Incorrect 18 ms 35668 KB jury has the better answer: jans = YES, pans = NO
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35668 KB answer = YES
2 Correct 19 ms 35624 KB answer = YES
3 Correct 18 ms 35668 KB answer = YES
4 Correct 19 ms 35540 KB answer = NO
5 Incorrect 18 ms 35668 KB jury has the better answer: jans = YES, pans = NO
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35668 KB answer = YES
2 Correct 19 ms 35624 KB answer = YES
3 Correct 18 ms 35668 KB answer = YES
4 Correct 19 ms 35540 KB answer = NO
5 Incorrect 18 ms 35668 KB jury has the better answer: jans = YES, pans = NO
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35668 KB answer = YES
2 Correct 19 ms 35624 KB answer = YES
3 Correct 18 ms 35668 KB answer = YES
4 Correct 19 ms 35540 KB answer = NO
5 Incorrect 18 ms 35668 KB jury has the better answer: jans = YES, pans = NO
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 35668 KB answer = YES
2 Correct 19 ms 35624 KB answer = YES
3 Correct 18 ms 35668 KB answer = YES
4 Correct 19 ms 35540 KB answer = NO
5 Incorrect 18 ms 35668 KB jury has the better answer: jans = YES, pans = NO
6 Halted 0 ms 0 KB -