답안 #1030542

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030542 2024-07-22T06:36:51 Z thinknoexit Graph (BOI20_graph) C++17
5 / 100
2 ms 3932 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100100;
struct A {
    int v, w;
};
vector<A> adj[N];
double ans[N];
bool ch[N], vis[N];
int col[N], bp = 0;
vector<pair<int, int>> cycle;
void dfs(int v, vector<int>& node) {
    node.push_back(v);
    vis[v] = 1;
    for (auto& x : adj[v]) {
        if (!vis[x.v]) dfs(x.v, node);
    }
}
bool dfs_ans(int v) {
    for (auto& x : adj[v]) {
        if (!ch[x.v]) {
            ch[x.v] = 1;
            ans[x.v] = x.w - ans[v];
            dfs_ans(x.v);
        }
        else if (ans[v] + ans[x.v] != x.w) return false;
    }
    return true;
}
bool bipartite(int v, int c) {
    if (col[v]) {
        if (col[v] != c) return bp = v, false;
        else return true;
    }
    col[v] = c;
    for (auto& x : adj[v]) {
        if (!bipartite(x.v, 3 - c)) {
            if (bp != 0) cycle.push_back({ v, x.w });
            if (v == bp) bp = 0;
            return false;
        }
    }
    return true;
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for (int i = 1;i <= m;i++) {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({ v, w });
        adj[v].push_back({ u, w });
    }
    for (int i = 1;i <= n;i++) {
        if (vis[i]) continue;
        vector<int> node;
        dfs(i, node);
        bool bipar = bipartite(i, 1);
        if (!bipar) {
            vector<pair<int, int>> c = cycle;
            cycle.clear();
            int root = c.back().first;
            int val = 0;
            for (int i = 0;i < (int)c.size();i++) {
                if (i & 1) val -= c[i].second;
                else val += c[i].second;
            }
            // value at node (root) = val / 2
            ch[root] = 1;
            ans[root] = (double)val / 2;
            if (!dfs_ans(root)) {
                cout << "NO";
                return 0;
            }
            continue;
        }
        int sz = node.size();
        if (sz == 1) {
            continue;
        }
        double mn = 1e9;
        int idx = -100;
        for (int V = -80;V <= 80;V++) {
            ch[i] = 1;
            ans[i] = (double)V;
            double sum = 0;
            bool can = dfs_ans(i);
            for (auto& i : node) {
                ch[i] = 0;
                sum += abs(ans[i]);
            }
            if (can && sum < mn) {
                mn = sum;
                idx = V;
            }
        }
        if (mn == 1e9) {
            cout << "NO";
            return 0;
        }
        ch[i] = 1;
        ans[i] = (double)idx;
        dfs_ans(i);
    }
    cout << "YES\n";
    for (int i = 1;i <= n;i++) {
        cout << ans[i] << ' ';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 2 ms 2652 KB answer = YES
7 Correct 2 ms 2648 KB answer = YES
8 Correct 2 ms 2652 KB answer = YES
9 Correct 1 ms 2648 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2908 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 2 ms 2648 KB answer = YES
16 Correct 2 ms 2648 KB answer = YES
17 Correct 1 ms 2652 KB answer = YES
18 Correct 2 ms 2652 KB answer = YES
19 Correct 2 ms 2648 KB answer = YES
20 Correct 2 ms 2652 KB answer = YES
21 Correct 1 ms 2652 KB answer = YES
22 Correct 2 ms 2652 KB answer = NO
23 Correct 1 ms 3932 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 2 ms 2652 KB answer = YES
7 Correct 2 ms 2648 KB answer = YES
8 Correct 2 ms 2652 KB answer = YES
9 Correct 1 ms 2648 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2908 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 2 ms 2648 KB answer = YES
16 Correct 2 ms 2648 KB answer = YES
17 Correct 1 ms 2652 KB answer = YES
18 Correct 2 ms 2652 KB answer = YES
19 Correct 2 ms 2648 KB answer = YES
20 Correct 2 ms 2652 KB answer = YES
21 Correct 1 ms 2652 KB answer = YES
22 Correct 2 ms 2652 KB answer = NO
23 Correct 1 ms 3932 KB answer = NO
24 Correct 1 ms 2652 KB answer = YES
25 Correct 1 ms 2816 KB answer = YES
26 Correct 2 ms 2648 KB answer = YES
27 Correct 1 ms 2648 KB answer = YES
28 Correct 2 ms 2696 KB answer = YES
29 Correct 2 ms 2652 KB answer = YES
30 Incorrect 1 ms 2652 KB Sum of endpoints for edge (75; 88) differs from the expected value 1.
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 2 ms 2652 KB answer = YES
7 Correct 2 ms 2648 KB answer = YES
8 Correct 2 ms 2652 KB answer = YES
9 Correct 1 ms 2648 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2908 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 2 ms 2648 KB answer = YES
16 Correct 2 ms 2648 KB answer = YES
17 Correct 1 ms 2652 KB answer = YES
18 Correct 2 ms 2652 KB answer = YES
19 Correct 2 ms 2648 KB answer = YES
20 Correct 2 ms 2652 KB answer = YES
21 Correct 1 ms 2652 KB answer = YES
22 Correct 2 ms 2652 KB answer = NO
23 Correct 1 ms 3932 KB answer = NO
24 Correct 1 ms 2652 KB answer = YES
25 Correct 1 ms 2816 KB answer = YES
26 Correct 2 ms 2648 KB answer = YES
27 Correct 1 ms 2648 KB answer = YES
28 Correct 2 ms 2696 KB answer = YES
29 Correct 2 ms 2652 KB answer = YES
30 Incorrect 1 ms 2652 KB Sum of endpoints for edge (75; 88) differs from the expected value 1.
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 2 ms 2652 KB answer = YES
7 Correct 2 ms 2648 KB answer = YES
8 Correct 2 ms 2652 KB answer = YES
9 Correct 1 ms 2648 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2908 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 2 ms 2648 KB answer = YES
16 Correct 2 ms 2648 KB answer = YES
17 Correct 1 ms 2652 KB answer = YES
18 Correct 2 ms 2652 KB answer = YES
19 Correct 2 ms 2648 KB answer = YES
20 Correct 2 ms 2652 KB answer = YES
21 Correct 1 ms 2652 KB answer = YES
22 Correct 2 ms 2652 KB answer = NO
23 Correct 1 ms 3932 KB answer = NO
24 Correct 1 ms 2652 KB answer = YES
25 Correct 1 ms 2816 KB answer = YES
26 Correct 2 ms 2648 KB answer = YES
27 Correct 1 ms 2648 KB answer = YES
28 Correct 2 ms 2696 KB answer = YES
29 Correct 2 ms 2652 KB answer = YES
30 Incorrect 1 ms 2652 KB Sum of endpoints for edge (75; 88) differs from the expected value 1.
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2648 KB answer = YES
2 Correct 1 ms 2652 KB answer = YES
3 Correct 2 ms 2652 KB answer = YES
4 Correct 1 ms 2652 KB answer = NO
5 Correct 1 ms 2652 KB answer = YES
6 Correct 2 ms 2652 KB answer = YES
7 Correct 2 ms 2648 KB answer = YES
8 Correct 2 ms 2652 KB answer = YES
9 Correct 1 ms 2648 KB answer = NO
10 Correct 1 ms 2652 KB answer = YES
11 Correct 1 ms 2652 KB answer = YES
12 Correct 1 ms 2908 KB answer = NO
13 Correct 1 ms 2652 KB answer = YES
14 Correct 1 ms 2652 KB answer = YES
15 Correct 2 ms 2648 KB answer = YES
16 Correct 2 ms 2648 KB answer = YES
17 Correct 1 ms 2652 KB answer = YES
18 Correct 2 ms 2652 KB answer = YES
19 Correct 2 ms 2648 KB answer = YES
20 Correct 2 ms 2652 KB answer = YES
21 Correct 1 ms 2652 KB answer = YES
22 Correct 2 ms 2652 KB answer = NO
23 Correct 1 ms 3932 KB answer = NO
24 Correct 1 ms 2652 KB answer = YES
25 Correct 1 ms 2816 KB answer = YES
26 Correct 2 ms 2648 KB answer = YES
27 Correct 1 ms 2648 KB answer = YES
28 Correct 2 ms 2696 KB answer = YES
29 Correct 2 ms 2652 KB answer = YES
30 Incorrect 1 ms 2652 KB Sum of endpoints for edge (75; 88) differs from the expected value 1.
31 Halted 0 ms 0 KB -