Cod sursa(job #1424919)

Utilizator preda.andreiPreda Andrei preda.andrei Data 25 aprilie 2015 21:07:53
Problema Balanta Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <iostream>
#include <fstream>

using namespace std;

int h[1025];
int l[1025];
int v1[513];
int v2[513];

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

    int n, m, x, r;
    fin >> n >> m;
    h[0]=l[0]=n;
    for(int z=1; z<=m; z++){
        fin >> x;
        for(int j=1; j<=x; j++)
            fin >> v1[j];
        for(int j=1; j<=x; j++)
            fin >> v2[j];
        fin >> r;
        if(r==1 || r==0){
            for(int i=1; i<=x; i++){
                if(l[v1[i]]==0)
                    l[0]--;
                l[v1[i]]--;
                if(h[v2[i]]==0)
                    h[0]--;
                h[v2[i]]--;
            }
        }
        if(r==2 || r==0){
            for(int i=1; i<=x; i++){
                if(l[v2[i]]==0)
                    l[0]--;
                l[v2[i]]--;
                if(h[v1[i]]==0)
                    h[0]--;
                h[v1[i]]--;
            }
        }
    }

    if(h[0]==1)
        for(int i=1; i<=n; i++)
            if(h[i]==0){
                fout << i;
                break;
            }
    else if(l[0]==1)
        for(int i=1; i<=n; i++)
            if(l[i]==0){
                fout << i;
                break;
            }
    else if(h[0]+l[0]==1){
        r=0;
        for(int i=1; i<=n; i++)
            if(h[i]==l[i] && h[i]==0){
                    r=i;
                    break;
            }
        fout << r;
    }
    else fout << 0;
    return 0;
}