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>
#include <time.h>

typedef enum { top, south, east, west, north, bottom, NUM_SIDES } sides;

int checkDice(const int dice0[], const int dice1[])
{
    int i, flag = 1;

    for( i = 0; i < NUM_SIDES && flag; ++i )
        if( dice0[i] != dice1[i] )
            flag = 0;

    return flag;
}

sides getOperator()
{
    return rand() % north + 1;
}

void roll(int *dice, const sides operator)
{
    int anti_op;
    switch( operator ){
    case south: anti_op = north; break;
    case east: anti_op = west; break;
    case west: anti_op = east; break;
    case north: anti_op = south; break;
    default: break;
    }

    int tmp = dice[operator];
    dice[operator] = dice[top];
    dice[top] = dice[anti_op];
    dice[anti_op] = dice[bottom];
    dice[bottom] = tmp;
}

int main()
{
    int i, j;

    srand(time(0));

    int n;
    scanf("%d", &n);
    int dice[n][NUM_SIDES];
    for( i = 0; i < n; ++i )
        for( j = 0; j < NUM_SIDES; ++j )
            scanf("%d", &dice[i][j]);

    sides operator = getOperator();
    for( i = 0; i < n; ++i )
        while( dice[0][top] != dice[i][top] || dice[0][south] != dice[i][south] ){
            roll(dice[i], operator);
            operator = getOperator();
        }

    int flag = 1;
    for( i = 0; i < n && flag; ++i )
        for( j = 0; j < n; ++j ){
            if( i == j ) continue;
            if( checkDice(dice[i], dice[j]) ){
                flag = 0;
                break;
            }
        }

    if( flag ) printf("Yes\n");
    else printf("No\n");

    return 0;
}