Submission #711417

# Submission time Handle Problem Language Result Execution time Memory
711417 2023-03-16T21:41:06 Z sysia Graph (BOI20_graph) C++17
5 / 100
1 ms 340 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e9+7;

void solve(){
    int n, m; cin >> n >> m;
    vector<vector<T>>g(n+1);
    for (int i = 0; i<m; i++) {
        int a, b, c; cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    vector<T>poly(n+1);
    vector<bool>vis(n+1);
    long double ans = (long double)oo;
    long double eps = 1e-7;
    vector<int>valid;
    function<void(int)>dfs = [&](int v){
        vis[v] = 1;
        valid.emplace_back(v);
        for (auto [x, c]: g[v]){
            if (!vis[x]){
                poly[x].first = -poly[v].first;
                poly[x].second = c - poly[v].second;
                dfs(x);
            } else {
                //solve the equation
                //curr.first * x + curr.second = poly[x].first * x + poly[x].second
                //x (curr.first - poly[x].first) = poly[x].second - curr.second;
                T curr = {-poly[v].first, c - poly[v].second};
                debug(v, x, curr, poly[x], poly[v]);
                if (curr.first == poly[x].first) {
                    if (curr.second != poly[x].second){
                        cout << "NO\n";
                        exit(0);
                    }
                    continue;
                }
                long double now = (long double)(poly[x].second - curr.second)/(long double)(curr.first - poly[x].first);
                debug(x, v, now);
                if (ans == oo) ans = now;
                else {
                    if (abs(now - ans) > eps){
                        cout << "NO\n";
                        exit(0);
                    }
                }
            }
        }
    };
    
    cout << fixed << setprecision(10);
    vector<long double>ret(n+1);
    vector<bool>vis2(n+1, 0);
    auto solve = [&](long double curr, int from){
        function<void(int)>restore = [&](int v){
            vis2[v] = 1;
            for (auto [x, c]: g[v]){
                if (!vis2[x]){
                    ret[x] = (long double)c - ret[v];
                    restore(x);
                }
            }
        };
        ret[from] = curr;
        restore(from);
        for (auto u: valid) vis2[u] = 0;
    };
    vector<long double>check = {-2.0, -1.0, 0.0, -0.5, 0.5, 1.0, 2.0};
    vector<long double>out(n+1);
    for (int i = 1; i<=n; i++){
        if (!vis[i]){
            ans = (long double)oo;
            poly[i].first = 1;
            dfs(i);
            debug(valid);
            if (ans == oo){ 
                long double mn = oo;
                for (auto x: check){
                    solve(x, i);
                    long double s = 0.0;
                    for (auto u: valid){
                        s += (long double)abs(ret[u]);
                    }
                    if (s <= mn){
                        mn = s;
                        ans = x;
                    }
                }
            }
            out[i] = ans;
            valid.clear();
        }
    }
    vis.assign(n+1, 0);
    function<void(int)>dfessa = [&](int v){
        vis[v] = 1;
        for (auto [x, c]: g[v]){
            if (!vis[x]){
                dfessa(x);
            }
        }
    };
    for (int i = 1; i<=n; i++) {
        if (!vis[i]){
            solve(out[i], i);
            dfessa(i);
        }
    } 
    cout << "YES\n";
    for (int i = 1; i<=n; i++) cout << ret[i] << " ";
    cout << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while (t--) solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 212 KB answer = YES
9 Correct 0 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 212 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 316 KB answer = YES
18 Correct 1 ms 320 KB answer = YES
19 Correct 1 ms 324 KB answer = YES
20 Correct 1 ms 320 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 212 KB answer = YES
9 Correct 0 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 212 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 316 KB answer = YES
18 Correct 1 ms 320 KB answer = YES
19 Correct 1 ms 324 KB answer = YES
20 Correct 1 ms 320 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 320 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Incorrect 1 ms 212 KB participant answer is larger than the answer of jury
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 212 KB answer = YES
9 Correct 0 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 212 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 316 KB answer = YES
18 Correct 1 ms 320 KB answer = YES
19 Correct 1 ms 324 KB answer = YES
20 Correct 1 ms 320 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 320 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Incorrect 1 ms 212 KB participant answer is larger than the answer of jury
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 212 KB answer = YES
9 Correct 0 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 212 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 316 KB answer = YES
18 Correct 1 ms 320 KB answer = YES
19 Correct 1 ms 324 KB answer = YES
20 Correct 1 ms 320 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 320 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Incorrect 1 ms 212 KB participant answer is larger than the answer of jury
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 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 212 KB answer = YES
9 Correct 0 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 212 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 316 KB answer = YES
18 Correct 1 ms 320 KB answer = YES
19 Correct 1 ms 324 KB answer = YES
20 Correct 1 ms 320 KB answer = YES
21 Correct 1 ms 212 KB answer = YES
22 Correct 1 ms 212 KB answer = NO
23 Correct 1 ms 212 KB answer = NO
24 Correct 1 ms 320 KB answer = YES
25 Correct 1 ms 212 KB answer = YES
26 Incorrect 1 ms 212 KB participant answer is larger than the answer of jury
27 Halted 0 ms 0 KB -