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>

void north (int *d) {
    int tmp;

    tmp=d[0];
    d[0]=d[1];
    d[1]=d[5];
    d[5]=d[4];
    d[4]=tmp;
}

void east (int *d) {
    int tmp;

    tmp=d[0];
    d[0]=d[3];
    d[3]=d[5];
    d[5]=d[2];
    d[2]=tmp;
}

void curl (int *d) {
    int tmp;

    tmp=d[1];
    d[1]=d[2];
    d[2]=d[4];
    d[4]=d[3];
    d[3]=tmp;
}

int compare(int *d1, int *d2) {
    int i=0, n=0;

    for (i=0;i<6;i++) {
        if (d1[i]==d2[i]) n++;
    }
    return n;
}

int process(int *d1, int *d2) {
    int i, j, n;

    for (i=0;i<6;i++) {
        for (j=0;j<6;j++) {
            if (d1[i]==d2[j]) n++;
        }
    }
    if (n<6) {
        return 0;
    }
    while (d1[0]!=d2[0]) {
        north(d2);
        if (d1[0]==d2[0]) {
            break;
        } else {
            east(d2);
        }
    }
    n=0;
    i=0;
    j=1;
    while (d1[j]!=d2[j]) {
        if (i>4) j++, i=0;
        curl(d2);
        i++;
    }
    for (i=0;i<7;i++) {
        n=compare(d1, d2);
        if (n==6) break;
        north(d2);
        n=compare(d1, d2);
        if (n==6) break;
        east(d2);
        n=compare(d1, d2);
        if (n==6) break;
    }

    if (n==6) {
        return 1;
    } else {
        return 0;
    }
}



int main(void) {
    int dice[100][6];
    int i, j, n=0, num;

    scanf("%d", &num);
    for (i=0; i<num; i++) {
        scanf("%d %d %d %d %d %d",
            &dice[i][0], &dice[i][1], &dice[i][2],
            &dice[i][3], &dice[i][4], &dice[i][5]);
    }

    for (i=0;i<num;i++) {
        for (j=i+1;j<num;j++) {
            n=process(dice[i], dice[j]);
            if (n==1) {
                printf("No\n");
                return 0;
            }
        }
    }

    printf("Yes\n");

    return 0;
}