답안 #725927

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
725927 2023-04-18T08:57:22 Z Johann Graph (BOI20_graph) C++14
58 / 100
700 ms 15436 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 minAns = 1LL << 60;
                x = 0;
                for (int w : newNodes)
                {
                    if (poly[w].second != 0)
                    {
                        int xcand = -poly[w].first / poly[w].second;
                        ll tmpAns = 0;
                        for (int u : newNodes)
                            tmpAns += (ll)abs(poly[u].first + poly[u].second * xcand);
                        if (tmpAns < minAns)
                            x = xcand, minAns = tmpAns;
                    }
                }
            }

            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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 292 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 340 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 1 ms 316 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 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 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 320 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 292 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 340 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 1 ms 316 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 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 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 320 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 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 1 ms 316 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 292 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 340 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 1 ms 316 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 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 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 320 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 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 1 ms 316 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
37 Correct 1 ms 340 KB answer = YES
38 Correct 1 ms 212 KB answer = YES
39 Correct 2 ms 324 KB answer = YES
40 Correct 2 ms 340 KB answer = YES
41 Correct 3 ms 340 KB answer = NO
42 Correct 3 ms 340 KB answer = YES
43 Correct 2 ms 428 KB answer = YES
44 Correct 3 ms 340 KB answer = YES
45 Correct 2 ms 340 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 292 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 340 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 1 ms 316 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 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 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 320 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 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 1 ms 316 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
37 Correct 1 ms 340 KB answer = YES
38 Correct 1 ms 212 KB answer = YES
39 Correct 2 ms 324 KB answer = YES
40 Correct 2 ms 340 KB answer = YES
41 Correct 3 ms 340 KB answer = NO
42 Correct 3 ms 340 KB answer = YES
43 Correct 2 ms 428 KB answer = YES
44 Correct 3 ms 340 KB answer = YES
45 Correct 2 ms 340 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
49 Correct 187 ms 1304 KB answer = YES
50 Correct 9 ms 1612 KB answer = YES
51 Correct 215 ms 1592 KB answer = YES
52 Correct 192 ms 1592 KB answer = NO
53 Correct 3 ms 340 KB answer = YES
54 Correct 11 ms 564 KB answer = YES
55 Correct 61 ms 812 KB answer = YES
56 Correct 199 ms 1288 KB answer = YES
57 Correct 39 ms 1200 KB answer = YES
58 Correct 63 ms 1164 KB answer = YES
59 Correct 15 ms 1108 KB answer = YES
60 Correct 96 ms 1264 KB answer = YES
61 Correct 7 ms 724 KB answer = YES
62 Correct 76 ms 7992 KB answer = NO
63 Correct 92 ms 8112 KB answer = YES
64 Correct 83 ms 8048 KB answer = NO
65 Correct 95 ms 8064 KB answer = YES
66 Correct 8 ms 460 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 1 ms 212 KB answer = YES
8 Correct 1 ms 292 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 340 KB answer = YES
11 Correct 1 ms 212 KB answer = YES
12 Correct 1 ms 212 KB answer = NO
13 Correct 1 ms 316 KB answer = YES
14 Correct 1 ms 212 KB answer = YES
15 Correct 1 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 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 320 KB answer = NO
24 Correct 1 ms 212 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Correct 1 ms 212 KB answer = YES
27 Correct 1 ms 212 KB answer = YES
28 Correct 1 ms 212 KB answer = YES
29 Correct 1 ms 212 KB answer = YES
30 Correct 1 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 1 ms 316 KB answer = YES
36 Correct 1 ms 212 KB answer = YES
37 Correct 1 ms 340 KB answer = YES
38 Correct 1 ms 212 KB answer = YES
39 Correct 2 ms 324 KB answer = YES
40 Correct 2 ms 340 KB answer = YES
41 Correct 3 ms 340 KB answer = NO
42 Correct 3 ms 340 KB answer = YES
43 Correct 2 ms 428 KB answer = YES
44 Correct 3 ms 340 KB answer = YES
45 Correct 2 ms 340 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
49 Correct 187 ms 1304 KB answer = YES
50 Correct 9 ms 1612 KB answer = YES
51 Correct 215 ms 1592 KB answer = YES
52 Correct 192 ms 1592 KB answer = NO
53 Correct 3 ms 340 KB answer = YES
54 Correct 11 ms 564 KB answer = YES
55 Correct 61 ms 812 KB answer = YES
56 Correct 199 ms 1288 KB answer = YES
57 Correct 39 ms 1200 KB answer = YES
58 Correct 63 ms 1164 KB answer = YES
59 Correct 15 ms 1108 KB answer = YES
60 Correct 96 ms 1264 KB answer = YES
61 Correct 7 ms 724 KB answer = YES
62 Correct 76 ms 7992 KB answer = NO
63 Correct 92 ms 8112 KB answer = YES
64 Correct 83 ms 8048 KB answer = NO
65 Correct 95 ms 8064 KB answer = YES
66 Correct 8 ms 460 KB answer = YES
67 Execution timed out 1055 ms 15436 KB Time limit exceeded
68 Halted 0 ms 0 KB -