#line 2 "/home/cocojapanpan/Procon_CPP/proconLibrary/lib/template/procon.hpp" #ifndef DEBUG // 提出時にassertはオフ #ifndef NDEBUG #define NDEBUG #endif // 定数倍高速化 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif #include using namespace std; using ll = long long; #define ALL(x) (x).begin(), (x).end() template using vec = vector; template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } template constexpr T INF = 1'000'000'000; template <> constexpr int INF = 1'000'000'000; template <> constexpr ll INF = ll(INF) * INF * 2; #line 2 "main.cpp" int N; // 結合律の確認 bool associative(const vec> &group) { for(int a = 0; a < N; a++) { for(int b = 0; b < N; b++) { for(int c = 0; c < N; c++) { // (a x b) x c int formerComp = group[group[a][b]][c]; // a x (b x c) int latterComp = group[a][group[b][c]]; if(formerComp != latterComp) return false; } } } return true; } // 単位元を返す vec getUnit(const vec> &group) { // 単位元の候補達 vec unitCandidate(N, true); for(int a = 0; a < N; a++) { for(int e = 0; e < N; e++) { // a x e = a かつ e x a = a if(group[a][e] == a && group[e][a] == a) continue; unitCandidate[e] = false; } } vec ret; for(int e = 0; e < N; e++) { if(unitCandidate[e]) ret.push_back(e); } return ret; } // 単位元eについて、逆元が存在するか bool haveInverse(const vec> &group, int e) { for(int a = 0; a < N; a++) { bool ok = false; for(int i = 0; i < N; i++) { // a x i = i x a = e なるiが存在 if(group[a][i] == e && group[i][a] == e) { ok = true; break; } } if(!ok) return false; } return true; } bool solve(const vec> &group) { if(!associative(group)) return false; vec units = getUnit(group); for(int e : units) { if(haveInverse(group, e)) return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N; vec> group(N, vec(N)); for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { cin >> group[i][j]; } } cout << (solve(group) ? "Yes" : "No") << "\n"; }