Cod sursa(job #968563)
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream f("balanta.in");
ofstream f2("balanta.out");
int n, nr_c, nr_t, res;
int a[1026];
int h[1026];
int l[1026];
f>>n;
for(int i = 1; i <= n; ++i) h[i] = l[i] = 1;
f>>nr_c;
while(nr_c--)
{
f>>nr_t;
for(int i = 0; i < nr_t*2; ++i) f>>a[i];
f>>res;
if(!res)
{
for(int i = 0; i < nr_t*2; ++i) h[a[i]] = l[a[i]] = 0;
}
else if(res == 1)
{
for(int i = 0; i < nr_t; ++i) h[a[i]] = l[a[2*nr_t-i-1]] = 0;
for(int i = 1; i <= n; ++i)
{
bool fnd = 0;
for(int j = 0; !fnd && j < nr_t*2; ++j)
if(a[j] == i) fnd = 1;
if(!fnd) l[i] = h[i] = 0;
}
}
else if(res == 2)
{
for(int i = 0; i < nr_t; ++i) l[a[i]] = h[a[2*nr_t-i-1]] = 0;
for(int i = 1; i <= n; ++i)
{
bool fnd = 0;
for(int j = 0; !fnd && j < nr_t*2; ++j)
if(a[j] == i) fnd = 1;
if(!fnd) l[i] = h[i] = 0;
}
}
}
int found = 0;
for(int i = 1; i <= n; ++i)
{
if(l[i])
{
if(!found) found = i;
else { f2<<"0"; return 0; }
}
if(h[i])
{
if(!found) found = i;
else { f2<<"0"; return 0; }
}
}
f2<<found;
}