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 JAVA (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
  /*1-11-d n????????????????????¨?????°???????????????*/

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    //?????????????????°??\???
    int allDice = Integer.parseInt(br.readLine());

    //??????????????????????????¨???????????£?¨?
    dice[] dice = new dice[allDice];

    //?????????????????°??????????????????????????????
    for(int i = 0; i < allDice ; i++){
      dice[i] = new dice();
    }

    //????????¨ ??¢??¨??°???????????´????????????+1
    int judge = 0;

    //??????????????¢?????°?????\???(num[1]???????????????1)
    for (int diceCount = 0; diceCount < allDice; diceCount++) {
      String num[] = br.readLine().split("[\\s]+");
      for (int i = 0; i < num.length; i++) {
        dice[diceCount].diceNum[i + 1] = Integer.parseInt(num[i]);
      }
    }

    //????????¨????????????2?¬???????????????£?¨?
    int diceCopy1[] = new int[7];
    int diceCopy2[] = new int[7];
    //??????????????¨
    int cnt;
    int onaji[] = new int[6];

    //????????°????????????????????????????????¨??????????????????????????????
    for(int i1 = 0; i1 < allDice ; i1++){
      //????????????????????????????????????????????????
      diceCopy1 = dice[i1].diceNum.clone();
      Arrays.sort(diceCopy1);

      for (int i2 = i1+1; i2 < allDice ; i2++) {
        //????????????????????????????????????????????????
        diceCopy2 = dice[i2].diceNum.clone();
        Arrays.sort(diceCopy2);

          //??????????????°????????¨????????????
          if(Arrays.equals(diceCopy1,diceCopy2)){
            //?????????????????????
            cnt = 0;
            //1???????????????????????°???????????????????????°???????????????
            for (int i3 = 1; i3 <= 6 - 1; i3++) {
              //?¬??????°?????¨????????????onaji?????????????´????+1
              if (diceCopy1[i3] == diceCopy1[i3 + 1]) {
                onaji[cnt]++;
              } else {
                //?????£??????onaji???????????????????????????+1
                cnt++;
              }
            }

            //onaji??°???????????§???(112222???4)???5??\???????????????????????????????????§??????
            Arrays.sort(onaji);
            if(onaji[onaji.length-1] + 1 >= 5){
//              System.out.println("dice" + i1 + " ??¨ " + "dice" + i2 + " ??????(??????5??\???)" );
              System.out.println("No");
              return;
            }

          //judge????????????
            judge = 0;

            //??°??????????????????????????§????????????????????????????????????
            //N???????????¢??????
            for (int j = 0; j < 3; j++) {
              //?????????2?????¢1??¨?????????1?????????????????¢???????????°?????????
              if (dice[i2].diceNum[1] == dice[i1].diceNum[1]) {
                judge = 1;
                break;
              }
              dice[i1].oderN();
            }

            //W???????????¢??????
            for (int j = 0; j < 3; j++) {
              //?????????2?????¢1??¨?????????1?????????????????¢???????????°?????????
              if (dice[i2].diceNum[1] == dice[i1].diceNum[1]) {
                break;
              }
              dice[i1].oderW();
            }
            //??£??¢?????°????¢????

            //?°´??????????????????
            for (int j = 0; j < 3; j++) {
              //?????????2?????¢2??¨?????????1?????¢2???????????°?????????
              if (dice[i2].diceNum[2] == dice[i1].diceNum[2]) {
                break;
              }
              dice[i1].oderRoll();
            }
            //?????¢?????°????¢????

            //??¢?????§?????????????????°???????????????
            for (int k = 1; k <= 6; k++) {
              if (dice[i1].diceNum[k] == dice[i2].diceNum[k]) {
                judge += 1;
              }
            }

            //??????(?????????????????¢?????????????????§???)
            //?????????????????????????????????No
            if (judge >= 6) {
 //             System.out.println("jucge3 = " + judge );////////////////
 //             System.out.println("dice" + i1 + " ??¨ " + "dice" + i2 + " ??????(?????¢)" );////////////////
              System.out.println("No");
              return;
            }

          }

 //         System.out.println("dice" + i1 + " ??¨ " + "dice" + i2 + " ?????\" );

      }

    }

    //??¨?????????????????????????????¨??¨?????????????????????Yes
    System.out.println("Yes");

  }

}

//??????????????????
class dice {
  //??????????????¢?????????0???6,0???????????????
  int diceNum[] = new int[7];
  //????????¨?????°
  int stoc;

  //N???????????¢??????
  public void oderN() {
    //1?????¢?????°????????????
    stoc = diceNum[1];
    //1????????£?????¢???2?????????
    diceNum[1] = diceNum[2];
    //2????????£?????¢???6?????????
    diceNum[2] = diceNum[6];
    //6????????£?????¢???5?????????
    diceNum[6] = diceNum[5];
    //5????????£?????¢???1?????????
    diceNum[5] = stoc;

  }

  //W???????????¢??????
  public void oderW() {

    stoc = diceNum[1];
    diceNum[1] = diceNum[3];
    diceNum[3] = diceNum[6];
    diceNum[6] = diceNum[4];
    diceNum[4] = stoc;

  }

  //?°´???????¨?????????§90?°?????¢
  public void oderRoll() {
    stoc = diceNum[2];
    diceNum[2] = diceNum[3];
    diceNum[3] = diceNum[5];
    diceNum[5] = diceNum[4];
    diceNum[4] = stoc;

  }

}