Cod sursa(job #1593524)

Utilizator AeroHHorea Stefan AeroH Data 8 februarie 2016 17:50:26
Problema Balanta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

ifstream f("balanta.in");
ofstream g("balanta.out");

int n,m,k,i,r,a;

set<int> H,L;
vector<int> st,dr,re={0};

void notL(vector<int> v)
{
    for(int i:v)
        L.erase(i);
}
void notH(vector<int> v)
{
    for(int i:v)
        H.erase(i);
}
void nope()
{
    notH(st),notL(st);
    notH(dr),notL(dr);
}
void reL()
{
    vector<int> t=re;
    for(auto i:L)
        if (find(st.begin(),st.end(),i)==st.end())
            t.push_back(i);
    for(auto i:t)L.erase(i);

    t=re;
    for(auto i:H)
        if (find(dr.begin(),dr.end(),i)==dr.end())
            t.push_back(i);
    for(auto i:t)H.erase(i);
}
void reH()
{
    vector<int> t=re;
    for(auto i:H)
        if (find(st.begin(),st.end(),i)==st.end())
            t.push_back(i);
    for(auto i:t)H.erase(t[i]);

    t=re;
    for(auto i:L)
        if (find(dr.begin(),dr.end(),i)==dr.end())
            t.push_back(i);
    for(auto i:t)L.erase(t[i]);
}

void show(set<int> s)
{
    for (auto i:s)
        cerr<<i<<" ";
    cerr<<'\n';
}

int main()
{
    f>>n>>m;
    for (i=1;i<=n;++i)
    {
        H.insert(i);
        L.insert(i);
    }
    while(m--)
    {
        f>>k;
        st=dr=re;
        for(i=1;i<=k;++i)
            f>>a,st.push_back(a);
        for(i=1;i<=k;++i)
            f>>a,dr.push_back(a);
        f>>r;
        if (r == 0)nope();
        else if (r == 1)notL(st),notH(dr),reH();
        else if (r == 2)notL(dr),notH(st),reL();
    }
    if (L.size()+H.size()>1)
        g<<0;
    else if (L.size())
        g<<*L.begin();
    else g<<*H.begin();

    return 0;
}