Cod sursa(job #28582)

Utilizator UigresUigres Sergiu Uigres Data 8 martie 2007 02:41:01
Problema Balanta Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream.h>
int main()
{ifstream in("balanta.in");
ofstream out("balanta.out");
int n,m;
in>>n>>m;
short int a[1025], c[1025];
int i,l,d,y;
for (i=1;i<=n;i++) {a[i]=-1;}
for (int j=1;j<=m;j++)
	{in>>l;
	for (i=1;i<=2*l;i++) in>>c[i];
	in>>d;
	if (d==0) for (i=1;i<=2*l;i++) a[c[i]]=0;
	if (d==1) { for (i=1;i<=n;i++) if (!a[i]) {y=1; while ((y<=2*l)&&(i!=c[y])) y++;if (y==2*l+1) a[i]=0; }
			  for (i=1;i<=l;i++)  if (a[c[i]]) (a[c[i]]==1)?a[c[i]]=0:a[c[i]]=2;
			  for (i=l+1;i<=2*l;i++) if (a[c[i]]) (a[c[i]]==2)?a[c[i]]=0:a[c[i]]=1;
			 }
 if (d==2) { for (i=1;i<=n;i++) if (!a[i]) {y=1; while ((y<=2*l)&&(i!=c[y])) y++;if (y==2*l+1) a[i]=0;  }
			  for (i=1;i<=l;i++)  if (a[c[i]]) (a[c[i]]==2)?a[c[i]]=0:a[c[i]]=1;
			  for (i=l+1;i<=2*l;i++) if (a[c[i]]) (a[c[i]]==1)?a[c[i]]=0:a[c[i]]=2;
			 }
}
 int u=0,u1,g=0,g1;
 for (i=1;i<=n;i++) {if (a[i]==1) {u++;u1=i; }; if (a[i]==2) {g++;g1=i; }; }
 if ((g!=u)) {if (g==1) out<<g1; else if (u==1) out<<u1; else out<<0; }
 else if (u!=0) out<<0; else out<<u1;

 in.close();out.close();
 return(0);}