Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice IV

Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III.

Input

In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III.

Output

Print "Yes" if given dices are all different, otherwise "No" in a line.

Constraints

$2 \leq n \leq 100$

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1

Sample Output 1

No

Sample Input 2

3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6

Sample Output 2

Yes

Solution in C (judge verdict: Time Limit Exceeded)

#include<stdio.h>
#include<stdlib.h>
struct DICE {
        int data;
        struct DICE *N;
        struct DICE *E;
        struct DICE *S;
        struct DICE *W;
        struct DICE *B;
        };
 
struct DICE *turn(struct DICE *NOW,char com);
int dice_check(struct DICE *NOW,struct DICE *A);
 
int main(void){
struct DICE A[6],B[6],*NOW;
int i,j,k,c,up,front,data[200][7];
 
scanf("%d",&c);

for(i=0;i<c;i++){
   for(j=0;j<6;j++){
    scanf("%d",&data[i][j]);
   }
}

for(k=0;k<c-1;k++){
    A[0].data=data[k][0];
    A[1].data=data[k][1];
    A[2].data=data[k][2];
    A[3].data=data[k][3];
    A[4].data=data[k][4];
    A[5].data=data[k][5];
 
    A[0].N=&A[4];
    A[0].E=&A[2];
    A[0].S=&A[1];
    A[0].W=&A[3];
    A[0].B=&A[5];
 
    for(j=k+1;j<c;j++){
 
    B[0].data=data[j][0];
    B[1].data=data[j][1];
    B[2].data=data[j][2];
    B[3].data=data[j][3];
    B[4].data=data[j][4];
    B[5].data=data[j][5];
 
    B[0].N=&B[4];
    B[0].E=&B[2];
    B[0].S=&B[1];
    B[0].W=&B[3];
    B[0].B=&B[5];
 
NOW=&B[0];
up=A[0].data;
front=A[0].S->data;
 
while(1){
    for(i=0;i<4;i++){
        if(NOW->S->data==front){break;}
        NOW=turn(NOW,'S');
        }
    if(i==4){NOW=turn(NOW,'E');}else{break;}
    }
for(i=0;i<4;i++){
    if(NOW->data==up){break;}
    NOW=turn(NOW,'E');
    }
    if(dice_check(NOW,&A[0])!=0){printf("No\n");return 0;}
 
    }
}
printf("Yes\n");
return 0;
}
 
struct DICE *turn(struct DICE *NOW,char com){
    struct DICE *TEMP;
    TEMP=NOW;
    switch(com){
    case 'N':
        NOW=NOW->S;
        NOW->N=TEMP;
        NOW->S=TEMP->B;
        NOW->E=TEMP->E;
        NOW->W=TEMP->W;
        NOW->B=TEMP->N;
        break;
    case 'S':
        NOW=NOW->N;
        NOW->N=TEMP->B;
        NOW->S=TEMP;
        NOW->E=TEMP->E;
        NOW->W=TEMP->W;
        NOW->B=TEMP->S;
        break;
    case 'E':
        NOW=NOW->W;
        NOW->N=TEMP->N;
        NOW->S=TEMP->S;
        NOW->E=TEMP;
        NOW->W=TEMP->B;
        NOW->B=TEMP->E;
        break;
    case 'W':
        NOW=NOW->E;
        NOW->N=TEMP->N;
        NOW->S=TEMP->S;
        NOW->E=TEMP->B;
        NOW->W=TEMP;
        NOW->B=TEMP->W;
        break;
    }
    return(NOW);
}
int dice_check(struct DICE *NOW,struct DICE *A){
 
    if(NOW->data==A->data && NOW->S->data==A->S->data && NOW->N->data==A->N->data && NOW->E->data==A->E->data && NOW->W->data==A->W->data && NOW->B->data==A->B->data){return 1;}else{return 0;}
 
}