POJ 2345

View Code
  1 //Result:wizmann    2345    Accepted    984K    250MS    G++    1302B

  2 #include <cstdio>

  3 #include <cstring>

  4 #include <cstdlib>

  5 #include <iostream>

  6 #include <algorithm>

  7 

  8 using namespace std;

  9 

 10 #define print(x) cout<<x<<endl

 11 #define input(x) cin>>x

 12 #define SIZE 256

 13 

 14 int mat[SIZE][SIZE];

 15 int ans[SIZE];

 16 int n;

 17 

 18 int main()

 19 {

 20     while(input(n))

 21     {

 22         memset(mat,0,sizeof(mat));

 23         memset(ans,0,sizeof(ans));

 24         int t;

 25         for(int i=1;i<=n;i++)

 26         {

 27             while(input(t) && t!=-1)

 28             {

 29                 mat[t][i]=1;

 30             }

 31             mat[i][n+1]=1;

 32         }

 33         /*

 34          * Sample Input

 35          * 4

 36          * 1 2 -1

 37          * 2 3 4 -1

 38          * 2 -1

 39          * 4 -1

 40          * 

 41          * =>

 42          *  Light  | Status

 43          * 1 0 0 0 |    1

 44          * 1 1 1 0 |    1

 45          * 0 0 1 0 |    1

 46          * 0 0 0 1 |    1

 47          */

 48          

 49         for(int i=1;i<=n;i++)

 50         {

 51             int pos=-1;

 52             for(int j=i;j<=n;j++)

 53             {

 54                 if(mat[j][i]){pos=j;break;}

 55             }

 56             for(int j=i;j<=n+1;j++)

 57             {

 58                 swap(mat[i][j],mat[pos][j]);

 59             }

 60 

 61             

 62             for(int j=i+1;j<=n;j++)

 63             {

 64                 if(mat[j][i]==1)

 65                 {

 66                     for(int k=i;k<=n+1;k++)

 67                     {

 68                         mat[j][k]^=mat[i][k];

 69                     }

 70                 }

 71             }

 72         }

 73         

 74         for(int i=n;i>=1;i--)

 75         {

 76             ans[i]=mat[i][n+1];

 77             for(int j=i-1;j>=1;j--)

 78             {

 79                 mat[j][n+1]^=(ans[i]*mat[j][i]);

 80             }

 81         }

 82         bool flag=true;

 83         for(int i=1;i<=n;i++)

 84         {

 85             if(ans[i])

 86             {

 87                 if(flag)

 88                 {

 89                     printf("%d",i);

 90                     flag=false;

 91                 }

 92                 else printf(" %d",i);

 93             }

 94         }

 95         if(flag) puts("No solution");

 96         else puts("");

 97     }

 98     return 0;

 99 }

100             

你可能感兴趣的:(poj)