HDOJ 3364 Lanterns

4101 ワード

ガウス消元,2^自由変元個数,しかし行列の行と列は等しくない... 
Lanterns
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 932    Accepted Submission(s): 373
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
「光庭杯」第5回華中北区プログラム設計招待試合及びWHU第8回プログラム設計コンテスト
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=110;

int ta[maxn][maxn],a[maxn][maxn],n,m;
int equ,val;

void Gauss()
{
    int r,c;
    for(r=0,c=0;r<equ&&c<val;r++,c++)
    {
        int mr=r;
        for(int i=r+1;i<equ;i++)
        {
            if(abs(a[i][c])>abs(a[mr][c]))
                mr=i;
        }
        if(a[mr][c]==0)
        {
            r--; continue;
        }
        if(mr!=r)
        {
            for(int i=c;i<=val;i++)
                swap(a[r][i],a[mr][i]);
        }
        for(int i=r+1;i<equ;i++)
        {
            if(a[i][c])
            {
                for(int j=c;j<=val;j++)
                {
                    a[i][j]^=a[r][j];
                }
            }
        }
    }
    for(int i=r;i<equ;i++)
    {
        if(a[i][val])
        {
            puts("0"); return ;
        }
    }
    printf("%I64d
",1LL<<(m-r)); } int main() { int T_T,cas=1; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&n,&m); memset(ta,0,sizeof(ta)); for(int i=0;i<m;i++) { int k; scanf("%d",&k); while(k--) { int x; scanf("%d",&x); ta[x-1][i]=1; } } equ=n,val=m; int Q; scanf("%d",&Q); printf("Case %d:
",cas++); while(Q--) { memcpy(a,ta,sizeof(ta)); for(int i=0;i<n;i++) scanf("%d",&a[i][m]); Gauss(); } } return 0; }