Cod sursa(job #2491063)

Utilizator DordeDorde Matei Dorde Data 11 noiembrie 2019 18:59:18
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.06 kb
#include <iostream>
#include <map>

using namespace std;
typedef long long ll;
map <int , bool> fq;
int const NM = 1 + 1e5;
struct code {
    int x , y;
    bool operator < (code r) const {
        if (x == r . x)
            return y < r . y;
        return x < r . x;
    }
};
map <code , int> mp;
int out (int c){
    while (c --)
        cout << "NOPE :/\n";
    return 0;
}
int t [NM] ;
ll label [NM];
int main()
{
    int i , n , m , cnt = 0 , from , to , comp = 1;
    from = to = 0;
    cin >> n >> m;
    for(i = 1 ; i <= m ; ++ i){
        int x , y , z;
        cin >> x >> y >> z;
        if (t [x] > t [y]){
            swap (x , y);
            z = -z;
        }
        code a = {from , to};
        if (mp [a])
            z -= mp [a];
        if (t [x] != t [y] && t [x] && t [y]){
            a = {from , to};
            mp [a] = z;
        }
        t [x] = t [y] = comp;
        if (! fq [x]){
            fq [x] = ++ cnt;
            x = cnt;
        }
        else
            x = fq [x];
        if (! fq [y]){
            fq [y] = ++ cnt;
            y = cnt;
        }
        else
            y = fq [y];
        if (x == y && z){
            return out (m - i + 1);
        }
        if (i == 1)
            label [x] = 0;
        if (label [x]){
            ll bx = label [x] , by = label [x] - z;
            if (label [y] && label [y] != by)
                return out (m - i + 1);
            if (! label [y])
                label [y] = by;
        }
        else{
            if (label [y]){
                ll by = label [y] , bx = label [y] - z;
                if (label [x] && label [x] != bx)
                    return out (m - i + 1);
                if (! label [x])
                    label [x] = bx;
            }
            else{
                from = x , to = y;
                label [x] = 0;
                label [y] = label [x] - z;
                ++ t [x] , ++ t [y];
                ++ comp;
            }
        }
        cout << "YEP :)\n";
    }
    return 0;
}