답안 #604950

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
604950 2022-07-25T11:06:26 Z OttoTheDino Graph (BOI20_graph) C++17
0 / 100
2 ms 2644 KB
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i,s,e)                  for (int i = s; i <= e; ++i)
#define rrep(i,s,e)                 for (int i = s; i >= e; --i)
#define pb                          push_back
#define pf                          push_front
#define all(a)                      a.begin(), a.end()
#define len(a)                      (int)a.size()
#define fi                          first
#define se                          second
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<pll> vpll;
 
const int mx = 1e5;
const double eps = 1e-9;
vii adj[mx+1];
int b1[mx+1], b2[mx+1], b3[mx+1], c[mx+1];
double ans[mx+1];
vi cur;
 
double eq (double x, double y) {
    return abs(x-y)<eps;
}
 
pair<int,double> dfs1 (int u, int p) {
    b1[u] = 1;
    for (ii ve : adj[u]) {
        int v = ve.fi, e = ve.se;
        if (e==p) continue;
        if (b1[v]) {
            if (u==v) return {u,{c[e]/2.0}};
            return {v,c[e]}; 
        }
        pair<int,double> res = dfs1 (v,e);
        if (res.fi) {
            res.se = c[e]-res.se;
            if (u==res.fi) return {u,res.se/2.0}; 
            return {res.fi,res.se};
        }
    }
    return {0,0};
}
 
void dfs_help (int u) {
    b3[u] = b1[u] = 1;
    cur.pb(u);
    for (ii ve : adj[u]) {
        int v = ve.fi;
        if (b3[v]) continue;
        dfs_help (v);
    }
}
 
double dfs2 (int u, int p, double val) {
    double s = abs(val);
    b2[u] = 1;
    ans[u] = val;
    for (ii ve : adj[u]) {
        int v = ve.fi, e = ve.se;
        if (e==p) continue;
        if (b2[v]) {
            if (!eq(c[e],val+ans[v])) return 1e9;
            continue;
        }
        double res = dfs2 (v, e, c[e]-val);
        if (res==1e9) return 1e9;
        s += res;
    }
    return s;
}  
 
int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int n, m; cin >> n >> m;
 
    ii edges[m+1];
    rep (i,1,m) {
        int u, v; cin >> u >> v;
        adj[u].pb({v,i});
        adj[v].pb({u,i});
        cin >> c[i];
        edges[i] = {u,v};
    }
 
    bool suc = 1;
    rep (i,1,n) {
        if (!b1[i]) {
            pair<int,double> res = dfs1 (i,0);
            dfs_help(i);
            if (res.fi) {
                if (dfs2 (i,-1,res.se)==1e9) {
                    suc = 0;
                    break;
                }
            }
            else {
                int best = cur[0];
                double val = 1e18;
                for (int el : cur) {
                    double r = dfs2(el,-1,0);
                    if (r<val) {
                        best = el;
                        val = r;
                    }
                    for (int el2 : cur) b2[el2] = 0;
                }
                dfs2(best,-1,0);
            }
            cur.clear();
        }
    }
 
    if (suc) {
        cout << "YES\n";
        rep (i,1,n) cout << ans[i] << " \n"[i==n];
    }
    else cout << "NO\n";
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Incorrect 2 ms 2644 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Incorrect 2 ms 2644 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Incorrect 2 ms 2644 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Incorrect 2 ms 2644 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 1 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Incorrect 2 ms 2644 KB participant answer is larger than the answer of jury
9 Halted 0 ms 0 KB -