답안 #573310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
573310 2022-06-06T11:55:50 Z tengiz05 Graph (BOI20_graph) C++17
0 / 100
1 ms 212 KB
#include <bits/stdc++.h>

using i64 = long long;

constexpr int inf = 1E9;

void gg() {
    std::cout << "NO\n";
    std::exit(0);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    
    std::vector<std::vector<std::pair<int, int>>> adj(n);
      
    for (int i = 0; i < m; i++) {
        int u, v, c;
        std::cin >> u >> v >> c;
        u--;
        v--;
        adj[u].emplace_back(v, c);
        adj[v].emplace_back(u, c);
    }
    
    std::vector<bool> vis(n);
    std::vector<int> col(n), a(n);
    std::vector<double> ans(n);
    
    for (int u = 0; u < n; u++) {
        if (!vis[u]) {
            int res = inf;
            std::vector<int> b;
            
            std::function<void(int)> dfs = [&](int u) {
                vis[u] = true;
                for (auto [v, c] : adj[u]) {
                    if (!vis[v]) {
                        col[v] = col[u] ^ 1;
                        a[v] = c - a[u];
                        dfs(v);
                    } else {
                        if (col[u] != col[v]) {
                            if (a[u] + a[v] != c) {
                                gg();
                            }
                        } else {
                            if (res == inf) {
                                res = c - a[u] - a[v];
                            } else if (res != c - a[u] - a[v]) {
                                gg();
                            }
                        }
                    }
                }
                b.push_back(u);
            };
            
            dfs(u);
            
            if (res != inf) {
                for (int v : b) {
                    ans[v] = a[v] + (col[v] == 0 ? res / 2. : -res / 2.);
                }
            } else {
                std::pair<int, int> best(inf, -1);
                for (int i = 0; i <= 2; i++) {
                    int sum = 0;
                    for (int v : b) {
                        sum += std::abs(a[v] + (col[v] == 0 ? i : -i));
                    }
                    best = std::min(best, std::pair(sum, i));
                }
                
                for (int v : b) {
                    ans[v] = a[v] + (col[v] == 0 ? best.second : -best.second);
                }
            }
        }
    }
    
    std::cout << "YES\n";
    for (int i = 0; i < n; i++) {
        std::cout << ans[i] << " \n"[i == n - 1];
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 0 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 0 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Incorrect 0 ms 212 KB Sum of endpoints for edge (4; 2) differs from the expected value 1.
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 0 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 0 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Incorrect 0 ms 212 KB Sum of endpoints for edge (4; 2) differs from the expected value 1.
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 0 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 0 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Incorrect 0 ms 212 KB Sum of endpoints for edge (4; 2) differs from the expected value 1.
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 0 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 0 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Incorrect 0 ms 212 KB Sum of endpoints for edge (4; 2) differs from the expected value 1.
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB answer = YES
2 Correct 0 ms 212 KB answer = YES
3 Correct 0 ms 212 KB answer = YES
4 Correct 0 ms 212 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 212 KB answer = YES
7 Correct 0 ms 212 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 212 KB answer = NO
10 Correct 1 ms 212 KB answer = YES
11 Incorrect 0 ms 212 KB Sum of endpoints for edge (4; 2) differs from the expected value 1.
12 Halted 0 ms 0 KB -