Cod sursa(job #28592)

Utilizator UigresUigres Sergiu Uigres Data 8 martie 2007 08:06:06
Problema Balanta Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream.h>
int main()
{ifstream in("balanta.in");
ofstream out("balanta.out");
int n,m,x;
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; }
			  x=0;
			  for (i=1;i<=l;i++)  if (a[c[i]]) (a[c[i]]==1)?a[c[i]]=0:a[c[i]]=2;else x++;
			  if (x==2*l-1)   for (i=1;i<=l;i++)  if (!a[i]) {out<<i;return(0);}
			  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=0,g=0,g1=0,w=0,w1=0;
 for (i=1;i<=n;i++) {if (a[i]==1) {u++;u1=i; }; if (a[i]==2) {g++;g1=i; }; if (a[i]==-1) {w++;w1=i;}; }
 if ((g!=u)) {if (g==1) out<<g1; else if (u==1) out<<u1; else out<<0; }
 else if (n==1) out<<1; else if ((g==0)&&(w==1)) out<<w1; else out<<0;

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