Submission #726057

# Submission time Handle Problem Language Result Execution time Memory
726057 2023-04-18T11:59:40 Z Johann Graph (BOI20_graph) C++14
100 / 100
168 ms 20164 KB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

int N, M;
vvpii adj;
vi vis;
vpii poly;
vi newNodes;
vector<int> ans;
bool possible = true;
bool xdefined = false;
int x = 0;

void dfs(int v)
{
    vis[v] = true;
    newNodes.push_back(v);

    int u, c;
    for (pii e : adj[v])
    {
        tie(u, c) = e;
        if (vis[u])
        {
            // check whether cohrerent possible
            pii res = {poly[v].first + poly[u].first, poly[v].second + poly[u].second};
            if (res.first == c && res.second == 0)
            {
                // perfect, nothing to do
            }
            else if (xdefined)
            {
                if (res.first + res.second * x - c != 0)
                    possible = false;
            }
            else
            {
                // not x defined
                if (res.second == 0)
                    possible = false;
                else
                {
                    xdefined = true;
                    assert((c - res.first) % res.second == 0);
                    x = (c - res.first) / res.second;
                }
            }
        }
        else
        { // new Node
            poly[u] = {c - poly[v].first, -poly[v].second};
            dfs(u);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M;
    adj.resize(N);
    for (int i = 0, a, b, c; i < M; ++i)
    {
        cin >> a >> b >> c;
        --a, --b, c *= 2;
        adj[a].push_back({b, c}), adj[b].push_back({a, c});
    }

    vis.assign(N, false);
    ans.resize(N);
    poly.resize(N);
    for (int v = 0; v < N; ++v)
        if (!vis[v])
        {
            xdefined = false;
            x = 0;
            poly[v] = {0, 1};
            newNodes.clear();
            dfs(v);

            if (!xdefined)
            {
                ll m = 0, b = 0;
                priority_queue<int> A;
                for (int w : newNodes)
                {
                    if (poly[w].second != 0)
                    {
                        assert(abs(poly[w].second) == 1);
                        int nb, nm;
                        tie(nb, nm) = poly[w];
                        if (poly[w].second < 0)
                            nb *= -1, nm *= -1;
                        b += nb, m += nm;
                        A.push(-nb);
                    }
                }
                x = 0;
                ll minAns = 1LL << 60;
                while (!A.empty())
                {
                    ll res = m * A.top() + b;
                    if (res < minAns)
                        x = A.top(), minAns = res;
                    m -= 2;
                    b += (ll)2 * A.top();
                    A.pop();
                }
            }

            for (int u : newNodes)
                ans[u] = poly[u].first + poly[u].second * x;
        }

    if (possible)
    {
        cout << "YES\n";
        for (int v = 0; v < N; ++v)
            cout << ans[v] / (double)2 << " ";
        cout << "\n";
    }
    else
    {
        cout << "NO\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 0 ms 212 KB answer = YES
6 Correct 0 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 0 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 0 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 0 ms 212 KB answer = YES
15 Correct 0 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 212 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 0 ms 212 KB answer = YES
22 Correct 0 ms 212 KB answer = NO
23 Correct 0 ms 212 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 0 ms 212 KB answer = YES
6 Correct 0 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 0 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 0 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 0 ms 212 KB answer = YES
15 Correct 0 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 212 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 0 ms 212 KB answer = YES
22 Correct 0 ms 212 KB answer = NO
23 Correct 0 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 0 ms 212 KB answer = YES
27 Correct 0 ms 212 KB answer = YES
28 Correct 0 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 0 ms 212 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 212 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 0 ms 212 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 0 ms 212 KB answer = YES
6 Correct 0 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 0 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 0 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 0 ms 212 KB answer = YES
15 Correct 0 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 212 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 0 ms 212 KB answer = YES
22 Correct 0 ms 212 KB answer = NO
23 Correct 0 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 0 ms 212 KB answer = YES
27 Correct 0 ms 212 KB answer = YES
28 Correct 0 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 0 ms 212 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 212 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 0 ms 212 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
37 Correct 1 ms 212 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 320 KB answer = YES
40 Correct 2 ms 328 KB answer = YES
41 Correct 1 ms 340 KB answer = NO
42 Correct 2 ms 340 KB answer = YES
43 Correct 2 ms 340 KB answer = YES
44 Correct 2 ms 340 KB answer = YES
45 Correct 2 ms 340 KB answer = YES
46 Correct 1 ms 320 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 0 ms 212 KB answer = YES
6 Correct 0 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 0 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 0 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 0 ms 212 KB answer = YES
15 Correct 0 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 212 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 0 ms 212 KB answer = YES
22 Correct 0 ms 212 KB answer = NO
23 Correct 0 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 0 ms 212 KB answer = YES
27 Correct 0 ms 212 KB answer = YES
28 Correct 0 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 0 ms 212 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 212 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 0 ms 212 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
37 Correct 1 ms 212 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 320 KB answer = YES
40 Correct 2 ms 328 KB answer = YES
41 Correct 1 ms 340 KB answer = NO
42 Correct 2 ms 340 KB answer = YES
43 Correct 2 ms 340 KB answer = YES
44 Correct 2 ms 340 KB answer = YES
45 Correct 2 ms 340 KB answer = YES
46 Correct 1 ms 320 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
49 Correct 10 ms 1376 KB answer = YES
50 Correct 10 ms 1620 KB answer = YES
51 Correct 9 ms 1592 KB answer = YES
52 Correct 5 ms 1620 KB answer = NO
53 Correct 2 ms 324 KB answer = YES
54 Correct 3 ms 460 KB answer = YES
55 Correct 5 ms 724 KB answer = YES
56 Correct 9 ms 1364 KB answer = YES
57 Correct 8 ms 1236 KB answer = YES
58 Correct 8 ms 1108 KB answer = YES
59 Correct 8 ms 1108 KB answer = YES
60 Correct 9 ms 1236 KB answer = YES
61 Correct 4 ms 780 KB answer = YES
62 Correct 58 ms 7984 KB answer = NO
63 Correct 60 ms 8012 KB answer = YES
64 Correct 55 ms 8016 KB answer = NO
65 Correct 56 ms 8012 KB answer = YES
66 Correct 2 ms 456 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 1 ms 212 KB answer = YES
3 Correct 1 ms 212 KB answer = YES
4 Correct 1 ms 212 KB answer = NO
5 Correct 0 ms 212 KB answer = YES
6 Correct 0 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 0 ms 212 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 0 ms 212 KB answer = NO
13 Correct 0 ms 212 KB answer = YES
14 Correct 0 ms 212 KB answer = YES
15 Correct 0 ms 212 KB answer = YES
16 Correct 1 ms 212 KB answer = YES
17 Correct 1 ms 212 KB answer = YES
18 Correct 1 ms 212 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 212 KB answer = YES
21 Correct 0 ms 212 KB answer = YES
22 Correct 0 ms 212 KB answer = NO
23 Correct 0 ms 212 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 0 ms 212 KB answer = YES
27 Correct 0 ms 212 KB answer = YES
28 Correct 0 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 0 ms 212 KB answer = NO
31 Correct 1 ms 212 KB answer = YES
32 Correct 1 ms 212 KB answer = YES
33 Correct 1 ms 212 KB answer = YES
34 Correct 1 ms 212 KB answer = YES
35 Correct 0 ms 212 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
37 Correct 1 ms 212 KB answer = YES
38 Correct 1 ms 336 KB answer = YES
39 Correct 1 ms 320 KB answer = YES
40 Correct 2 ms 328 KB answer = YES
41 Correct 1 ms 340 KB answer = NO
42 Correct 2 ms 340 KB answer = YES
43 Correct 2 ms 340 KB answer = YES
44 Correct 2 ms 340 KB answer = YES
45 Correct 2 ms 340 KB answer = YES
46 Correct 1 ms 320 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
49 Correct 10 ms 1376 KB answer = YES
50 Correct 10 ms 1620 KB answer = YES
51 Correct 9 ms 1592 KB answer = YES
52 Correct 5 ms 1620 KB answer = NO
53 Correct 2 ms 324 KB answer = YES
54 Correct 3 ms 460 KB answer = YES
55 Correct 5 ms 724 KB answer = YES
56 Correct 9 ms 1364 KB answer = YES
57 Correct 8 ms 1236 KB answer = YES
58 Correct 8 ms 1108 KB answer = YES
59 Correct 8 ms 1108 KB answer = YES
60 Correct 9 ms 1236 KB answer = YES
61 Correct 4 ms 780 KB answer = YES
62 Correct 58 ms 7984 KB answer = NO
63 Correct 60 ms 8012 KB answer = YES
64 Correct 55 ms 8016 KB answer = NO
65 Correct 56 ms 8012 KB answer = YES
66 Correct 2 ms 456 KB answer = YES
67 Correct 91 ms 16056 KB answer = YES
68 Correct 75 ms 16092 KB answer = YES
69 Correct 75 ms 15616 KB answer = YES
70 Correct 108 ms 20164 KB answer = YES
71 Correct 78 ms 15900 KB answer = YES
72 Correct 107 ms 10476 KB answer = YES
73 Correct 95 ms 9944 KB answer = YES
74 Correct 55 ms 9584 KB answer = YES
75 Correct 38 ms 9556 KB answer = NO
76 Correct 10 ms 1568 KB answer = YES
77 Correct 20 ms 2788 KB answer = YES
78 Correct 42 ms 4688 KB answer = YES
79 Correct 82 ms 9076 KB answer = YES
80 Correct 59 ms 9580 KB answer = YES
81 Correct 44 ms 12436 KB answer = NO
82 Correct 88 ms 15392 KB answer = YES
83 Correct 105 ms 15812 KB answer = YES
84 Correct 117 ms 16056 KB answer = YES
85 Correct 87 ms 16024 KB answer = YES
86 Correct 74 ms 15780 KB answer = YES
87 Correct 75 ms 11096 KB answer = NO
88 Correct 101 ms 12168 KB answer = YES
89 Correct 83 ms 9040 KB answer = YES
90 Correct 84 ms 8896 KB answer = YES
91 Correct 94 ms 9000 KB answer = YES
92 Correct 39 ms 5344 KB answer = YES
93 Correct 38 ms 5236 KB answer = YES
94 Correct 65 ms 16084 KB answer = NO
95 Correct 41 ms 8712 KB answer = NO
96 Correct 168 ms 17832 KB answer = YES
97 Correct 44 ms 16060 KB answer = NO