HDOJ 题目3364 Lanterns(高斯消元,开关灯问题)

Lanterns

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1174    Accepted Submission(s): 464


Problem Description
Alice has received a beautiful present from Bob. The present contains n lanterns and m switches. Each switch controls some lanterns and pushing the switch will change the state of all lanterns it controls from off to on or from on to off. A lantern may be controlled by many switches. At the beginning, all the lanterns are off.

Alice wants to change the state of the lanterns to some specific configurations and she knows that pushing a switch more than once is pointless. Help Alice to find out the number of ways she can achieve the goal. Two ways are different if and only if the sets (including the empty set) of the switches been pushed are different.
 

Input
The first line contains an integer T (T<=5) indicating the number of test cases.
The first line of each test case contains an integer n (1<=n<=50) and m (1<=m<=50).
Then m lines follow. Each line contains an integer k (k<=n) indicating the number of lanterns this switch controls.
Then k integers follow between 1 and n inclusive indicating the lantern controlled by this switch.
The next line contains an integer Q (1<=Q<=1000) represent the number of queries of this test case.
Q lines follows. Each line contains n integers and the i-th integer indicating that the state (1 for on and 0 for off) of the i-th lantern of this query.
 

Output
For each test case, print the case number in the first line. Then output one line containing the answer for each query.
Please follow the format of the sample output.
 

Sample Input
  
  
2 3 2 2 1 2 2 1 3 2 0 1 1 1 1 1 3 3 0 0 0 2 0 0 0 1 0 0
 

Sample Output
  
  
Case 1: 1 0 Case 2: 8 0
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3362  3363  3361  3365  3367 
 


ac代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct s
{
	int map[55][55];
};
int n,m;
int gauss(struct s a,int n,int m)
{
	int i,j,k,r,u;
	i=j=1;
	while(i<=n&&j<=m)
	{
		r=i;
		for(k=i;k<=n;k++)
		{
			if(a.map[k][j])
			{
				r=k;
				break;
			}
		}
		if(a.map[r][j])
		{
			if(r!=i)
			{
				for(k=0;k<=m+1;k++)
				{
					int t=a.map[r][k];
					a.map[r][k]=a.map[i][k];
					a.map[i][k]=t;
				}
			}
			for(u=i+1;u<=n;u++)
			{
				if(a.map[u][j])
				{
					for(k=i;k<=m+1;k++)
					{
						a.map[u][k]^=a.map[i][k];
					}
				}
			}
			i++;
		}
		j++;
	}
	for(u=i;u<=n;u++)
	{
		if(a.map[u][m+1])
			return -1;
	}
	return i-1;
}
int main()
{
	int t,c=0;
	scanf("%d",&t);
	while(t--)
	{
		struct s a;
		memset(a.map,0,sizeof(a.map));
		int i,j;
		scanf("%d%d",&n,&m);
		for(i=1;i<=m;i++)
		{
			int num;
			scanf("%d",&num);
			while(num--)
			{
				int x;
				scanf("%d",&x);
				a.map[x][i]=1;
			}
		}
		int q;
		//scanf("%d",&q);
		printf("Case %d:\n",++c);
		scanf("%d",&q);
		while(q--)
		{
			for(i=1;i<=n;i++)
			{
				int x;
				scanf("%d",&x);
				a.map[i][m+1]=x;
			}
			int ans=gauss(a,n,m);
			if(ans==-1)
				printf("0\n");
			else
				printf("%I64d\n",(__int64)1<<(m-ans));
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值