Submission #447163

# Submission time Handle Problem Language Result Execution time Memory
447163 2021-07-24T19:57:25 Z Ozy Graph (BOI20_graph) C++17
0 / 100
2 ms 2636 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "

#define MAX 100000
#define des first
#define color second
#define INF 100000000000.0

lli n,m,a,b,c,cont, nx, ny;
vector<pair<lli,lli> > hijos[MAX+2], inicios;
lli visitados[MAX+2],prof[MAX+2];
long double val[2][MAX+2], exnodo[MAX+2], valx[MAX+2], valy[MAX+2];
long double x, y, extra, suma;
bool posible = true;

void dfs(lli pos,lli padre){
    visitados[pos] = cont;
    b = padre;
    a = pos;
    for(auto h : hijos[pos]) if (!visitados[h.des]) dfs(h.des,pos);
}

void DFS(lli pos, lli p) {

}

bool calcx(lli pos, lli p, lli v){
    bool res = true;
    visitados[pos] = -v;
    prof[pos] = p;
    p ^= 1;

    for (auto h : hijos[pos]){
        if (visitados[h.des] == v){
            if (h.color == 1) {
                exnodo[h.des] = -exnodo[pos];
            }
            else {
                exnodo[h.des] = -exnodo[pos] + 1;
            }
            extra += exnodo[h.des];
            res &= calcx(h.des, p, v);
        }
        else if (prof[pos] == prof[h.des]) {
            long double xx = exnodo[h.des] + exnodo[pos];
            long double r = h.color;
            if (x == INF){
                if (!p){
                    y = (r - xx) / 2;
                    x = 1 - y;
                }
                else {
                    x = (r - xx) / 2;
                    y = 1 - x;
                }
            }
            else {
                if (!p && (y + y + xx) != r) return false;
                if (p && (x + x + xx) != r) return false;
            }
        }
        else {
            long double xx = exnodo[h.des] + exnodo[pos];
            long double r = h.color;
            if (x == INF && xx + 1 != r) return false;
            if (x != INF && xx + x + y != r) return false;
        }
    }
    return res;
}

void solve(pair<lli, lli> inicio) {
    cont = visitados[inicio.first];


    x = y = INF;
    nx = ny = 0;
    extra = 0;
    long double sa;
    if (calcx(inicio.first, 0, cont)){
        if (x == INF){
            sa = min(nx, ny) + extra;
            if (nx <= ny){ x = 0; y = 1; }
            else { x = 1; y = 0; }
        }
        else sa = (nx * x) + (ny * y) + extra;
    }
    else sa = INF;

    long double xa = x;
    long double ya = y;

    extra = 0;
    x = y = INF;
    nx = ny = 0;
    long double sb;
    if (inicio.second > 0 && calcx(inicio.second, 0, -cont)){
        if (x == INF){
            sb = min(nx, ny) + extra;
            if (nx <= ny) { x = 0; y = 1; }
            else { x = 1; y = 0; }
        }
        else sb = (nx * x) + (ny * y) + extra;
    }
    else sb = INF;

    if (sa == INF && sb == INF){
        posible = false;
        return;
    }
    else if (sa < sb) {
        x = y = INF;
        nx = ny = 0;
        extra = 0;
        calcx(inicio.first, 0, cont);

        x = xa;
        y = ya;

        suma += sa;
    }
    else{
        suma += sb;
    }

    valx[cont] = x;
    valy[cont] = y;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    rep(i,1,m) {
        cin >> a >> b >> c;
        hijos[a].push_back({b,c});
        hijos[b].push_back({a,c});
    }

    posible = true;
    cont = 1;
    rep(i,1,n) if (visitados[i] == 0) {
        a = b = 0;
        dfs(i, 0);
        cont++;
        inicios.emplace_back(a, b);
    }

    for(auto arbol : inicios) solve(arbol);

    if (!posible) cout << "NO\n";
    else {
        cout << "YES\n";
        cout << setprecision(6) << fixed;
        rep (i,1,n){
            if (prof[i]) cout << valy[abs(visitados[i])] + exnodo[i] << ' ';
            else cout << valx[abs(visitados[i])] + exnodo[i] << ' ';
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Incorrect 2 ms 2636 KB participant answer is larger than the answer of jury
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Incorrect 2 ms 2636 KB participant answer is larger than the answer of jury
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Incorrect 2 ms 2636 KB participant answer is larger than the answer of jury
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Incorrect 2 ms 2636 KB participant answer is larger than the answer of jury
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB answer = YES
2 Correct 2 ms 2636 KB answer = YES
3 Incorrect 2 ms 2636 KB participant answer is larger than the answer of jury
4 Halted 0 ms 0 KB -