Cod sursa(job #2230690)

Utilizator giotoPopescu Ioan gioto Data 11 august 2018 00:04:43
Problema Balanta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>
using namespace std;

int n, m;
int x[1025], y[1025];
set <int> a;
set <int> b;
set <int> c;
set <int> d;
int main()
{
    freopen("balanta.in", "r", stdin);
    freopen("balanta.out", "w", stdout);

    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n ; ++i) a.insert(i), b.insert(i);

    for(int i = 1; i <= m ; ++i){
        int k, t;
        scanf("%d", &k);
        for(int i = 1; i <= k ; ++i) scanf("%d", &x[i]);
        for(int i = 1; i <= k ; ++i) scanf("%d", &y[i]);
        scanf("%d", &t);

        if(t == 0){
            for(int i = 1; i <= k ; ++i){
                a.erase(x[i]); a.erase(y[i]);
                b.erase(x[i]); b.erase(y[i]);
            }
        }
        else if(t == 1){
            c.clear(); d.clear();
            for(int i = 1; i <= k ; ++i){
                if(a.count(x[i])) c.insert(x[i]);
                if(b.count(y[i])) d.insert(y[i]);
            }
            a.swap(c);
            b.swap(d);
        }
        else{
            c.clear(); d.clear();
            for(int i = 1; i <= k ; ++i){
                if(a.count(y[i])) c.insert(y[i]);
                if(b.count(x[i])) d.insert(x[i]);
            }
            a.swap(c);
            b.swap(d);
        }
    }

    if(a.size() == 1 && b.size() == 0) printf("%d", *a.begin());
    else if(b.size() == 1 && a.size() == 0) printf("%d", *b.begin());
    else printf("0");
    return 0;
}