Cod sursa(job #3188828)

Utilizator Linca_AmaliaLinca Mihaela Amalia Linca_Amalia Data 3 ianuarie 2024 21:34:44
Problema Balanta Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.36 kb
#include <fstream>
#include <cstring>
using namespace std;

ifstream fin("balanta.in");
ofstream fout("balanta.out");

int n, m, x, r, sol;
int G[1040], U[1040], A[1040], B[1040], ok[1040];

int main(){

    fin >> n >> m;
    for (int i = 1; i <= n; i++){
        G[i] = 1;
        U[i] = 1;
    }
    for(int i = 1; i <= m; i++){
        fin >> x;
        for(int j = 1; j <= x; j++)
            fin >> A[j];
        for(int j = 1; j <= x; j++)
            fin >> B[j];
        fin >> r;

        if ( r == 0 ){
            for(int j = 1; j <= x; j++){
                G[ A[j] ] = 0;
                G[ B[j] ] = 0;
                U[ A[j] ] = 0;
                U[ B[j] ] = 0;
                /// G - A - B  /// U - A - B
            }
            continue;
        }

        if ( r == 1 ){
            memset(ok, 0, sizeof(ok));
            for(int j = 1; j <= x; j++)
                ok[ A[j] ] = 1;
            for(int j = 1; j <= n; j++){
                if ( ok[j] == 1 && G[j] == 1 )
                    G[j] = 1;
                else
                    G[j] = 0;
            }
            memset(ok, 0, sizeof(ok));
            for(int j = 1; j <= x; j++)
                ok[ B[j] ] = 1;
            for(int j = 1; j <= n; j++){
                if ( ok[j] == 1 && U[j] == 1 )
                    U[j] = 1;
                else
                    U[j] = 0;
            }
            continue;
        }

        if ( r == 2 ){
            memset(ok, 0, sizeof(ok));
            for(int j = 1; j <= x; j++)
                ok[ B[j] ] = 1;
            for(int j = 1; j <= n; j++){
                if ( ok[j] == 1 && G[j] == 1 )
                    G[j] = 1;
                else
                    G[j] = 0;
            }
            memset(ok, 0, sizeof(ok));
            for(int j = 1; j <= x; j++)
                ok[ A[j] ] = 1;
            for(int j = 1; j <= n; j++){
                if ( ok[j] == 1 && U[j] == 1 )
                    U[j] = 1;
                else
                    U[j] = 0;
            }
            continue;
        }

    }
    sol = -1;
    for(int i = 1; i <= n; i++)
        if ( G[i] == 1 )
            sol = i;
    for(int i = 1; i <= n; i++)
        if ( U[i] == 1 )
            sol = i;
    if ( sol != -1 )
        fout << sol;
    else
        fout << 0;

    return 0;
}