Submission #652539

# Submission time Handle Problem Language Result Execution time Memory
652539 2022-10-22T20:13:12 Z 1zaid1 Graph (BOI20_graph) C++17
100 / 100
601 ms 56172 KB
#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], f[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;
}

void dfs2(int s, int d = 0) {
    is[s] = true;
    for (auto [i, x]:node[s]) {
        if (!is[i]) {
            f[i] = f[s]+(d?x:-x);
            dfs2(i, !d);
        }
    }
}

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;
                }

                is = 0;
                dfs2(k);
                map<int, int> fr, ir;
                for (int i:tmp) fr[f[i]]++;
                for (int i:tmp) ir[f[i]] = i;
                int sum = 0;
                for (auto [a, b]:fr) sum += a*b;
                
                vector<int> nor; for (int i:tmp) nor.push_back(-f[i]);
                sort(nor.begin(), nor.end());

                double ans;
                if (nor.size()%2) ans = nor[nor.size()/2];
                else ans = (nor[nor.size()/2] + nor[nor.size()/2-1])/2;
                is = 0;
                eval(k, ans);
            }

            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

3 2
2 1 2
3 2 1

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

Compilation message

Graph.cpp: In function 'int main()':
Graph.cpp:86: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]
   86 |                 for (int i = 0, f = 1; i < lp.size()-1; i++, f ^= 1) {
      |                                        ~~^~~~~~~~~~~~~
Graph.cpp:128:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  128 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |     ^~~
Graph.cpp:128:58: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  128 |     for (int i = 1; i <= n; i++) cout << cost[i] << ' '; cout << endl;
      |                                                          ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 3 ms 2644 KB answer = YES
4 Correct 2 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2696 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 3 ms 2692 KB answer = YES
15 Correct 2 ms 2696 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 2 ms 2696 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2692 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 3 ms 2644 KB answer = YES
4 Correct 2 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2696 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 3 ms 2692 KB answer = YES
15 Correct 2 ms 2696 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 2 ms 2696 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2692 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2696 KB answer = YES
30 Correct 2 ms 2692 KB answer = NO
31 Correct 2 ms 2664 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2692 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 3 ms 2644 KB answer = YES
4 Correct 2 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2696 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 3 ms 2692 KB answer = YES
15 Correct 2 ms 2696 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 2 ms 2696 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2692 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2696 KB answer = YES
30 Correct 2 ms 2692 KB answer = NO
31 Correct 2 ms 2664 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2692 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2772 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 3 ms 2772 KB answer = YES
40 Correct 3 ms 2900 KB answer = YES
41 Correct 3 ms 2900 KB answer = NO
42 Correct 3 ms 2900 KB answer = YES
43 Correct 3 ms 2900 KB answer = YES
44 Correct 3 ms 2900 KB answer = YES
45 Correct 4 ms 2956 KB answer = YES
46 Correct 3 ms 2832 KB answer = YES
47 Correct 4 ms 2900 KB answer = YES
48 Correct 4 ms 2900 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 3 ms 2644 KB answer = YES
4 Correct 2 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2696 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 3 ms 2692 KB answer = YES
15 Correct 2 ms 2696 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 2 ms 2696 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2692 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2696 KB answer = YES
30 Correct 2 ms 2692 KB answer = NO
31 Correct 2 ms 2664 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2692 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2772 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 3 ms 2772 KB answer = YES
40 Correct 3 ms 2900 KB answer = YES
41 Correct 3 ms 2900 KB answer = NO
42 Correct 3 ms 2900 KB answer = YES
43 Correct 3 ms 2900 KB answer = YES
44 Correct 3 ms 2900 KB answer = YES
45 Correct 4 ms 2956 KB answer = YES
46 Correct 3 ms 2832 KB answer = YES
47 Correct 4 ms 2900 KB answer = YES
48 Correct 4 ms 2900 KB answer = YES
49 Correct 19 ms 5276 KB answer = YES
50 Correct 22 ms 5588 KB answer = YES
51 Correct 19 ms 5768 KB answer = YES
52 Correct 14 ms 5460 KB answer = NO
53 Correct 3 ms 2900 KB answer = YES
54 Correct 6 ms 3368 KB answer = YES
55 Correct 11 ms 4052 KB answer = YES
56 Correct 19 ms 5340 KB answer = YES
57 Correct 18 ms 4948 KB answer = YES
58 Correct 20 ms 4948 KB answer = YES
59 Correct 15 ms 4756 KB answer = YES
60 Correct 20 ms 5152 KB answer = YES
61 Correct 9 ms 3924 KB answer = YES
62 Correct 16 ms 4948 KB answer = NO
63 Correct 528 ms 37336 KB answer = YES
64 Correct 492 ms 37388 KB answer = NO
65 Correct 511 ms 37436 KB answer = YES
66 Correct 5 ms 3156 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 3 ms 2644 KB answer = YES
4 Correct 2 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2696 KB answer = YES
7 Correct 2 ms 2644 KB answer = YES
8 Correct 2 ms 2644 KB answer = YES
9 Correct 2 ms 2644 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 3 ms 2692 KB answer = YES
15 Correct 2 ms 2696 KB answer = YES
16 Correct 2 ms 2644 KB answer = YES
17 Correct 2 ms 2644 KB answer = YES
18 Correct 2 ms 2644 KB answer = YES
19 Correct 2 ms 2696 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 2 ms 2644 KB answer = YES
22 Correct 2 ms 2644 KB answer = NO
23 Correct 2 ms 2692 KB answer = NO
24 Correct 2 ms 2644 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2696 KB answer = YES
30 Correct 2 ms 2692 KB answer = NO
31 Correct 2 ms 2664 KB answer = YES
32 Correct 2 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2692 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2772 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 3 ms 2772 KB answer = YES
40 Correct 3 ms 2900 KB answer = YES
41 Correct 3 ms 2900 KB answer = NO
42 Correct 3 ms 2900 KB answer = YES
43 Correct 3 ms 2900 KB answer = YES
44 Correct 3 ms 2900 KB answer = YES
45 Correct 4 ms 2956 KB answer = YES
46 Correct 3 ms 2832 KB answer = YES
47 Correct 4 ms 2900 KB answer = YES
48 Correct 4 ms 2900 KB answer = YES
49 Correct 19 ms 5276 KB answer = YES
50 Correct 22 ms 5588 KB answer = YES
51 Correct 19 ms 5768 KB answer = YES
52 Correct 14 ms 5460 KB answer = NO
53 Correct 3 ms 2900 KB answer = YES
54 Correct 6 ms 3368 KB answer = YES
55 Correct 11 ms 4052 KB answer = YES
56 Correct 19 ms 5340 KB answer = YES
57 Correct 18 ms 4948 KB answer = YES
58 Correct 20 ms 4948 KB answer = YES
59 Correct 15 ms 4756 KB answer = YES
60 Correct 20 ms 5152 KB answer = YES
61 Correct 9 ms 3924 KB answer = YES
62 Correct 16 ms 4948 KB answer = NO
63 Correct 528 ms 37336 KB answer = YES
64 Correct 492 ms 37388 KB answer = NO
65 Correct 511 ms 37436 KB answer = YES
66 Correct 5 ms 3156 KB answer = YES
67 Correct 165 ms 34536 KB answer = YES
68 Correct 148 ms 34388 KB answer = YES
69 Correct 143 ms 33400 KB answer = YES
70 Correct 232 ms 56172 KB answer = YES
71 Correct 149 ms 33372 KB answer = YES
72 Correct 339 ms 27032 KB answer = YES
73 Correct 307 ms 25512 KB answer = YES
74 Correct 143 ms 21524 KB answer = YES
75 Correct 113 ms 21376 KB answer = NO
76 Correct 22 ms 5812 KB answer = YES
77 Correct 53 ms 8840 KB answer = YES
78 Correct 113 ms 13324 KB answer = YES
79 Correct 267 ms 23680 KB answer = YES
80 Correct 169 ms 22432 KB answer = YES
81 Correct 204 ms 28920 KB answer = NO
82 Correct 273 ms 32256 KB answer = YES
83 Correct 297 ms 33832 KB answer = YES
84 Correct 328 ms 34900 KB answer = YES
85 Correct 176 ms 34696 KB answer = YES
86 Correct 177 ms 33564 KB answer = YES
87 Correct 300 ms 26684 KB answer = NO
88 Correct 382 ms 29316 KB answer = YES
89 Correct 279 ms 24220 KB answer = YES
90 Correct 300 ms 24076 KB answer = YES
91 Correct 283 ms 24408 KB answer = YES
92 Correct 113 ms 14268 KB answer = YES
93 Correct 118 ms 14212 KB answer = YES
94 Correct 223 ms 32936 KB answer = NO
95 Correct 168 ms 23344 KB answer = NO
96 Correct 601 ms 47960 KB answer = YES
97 Correct 95 ms 32548 KB answer = NO