Submission #7271818


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;;
#define ll long long
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define INF 1<<30
#define LINF 1LL<<62
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
const int MOD = 1000000007;
 
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;cin >> n;
  queue<ll> a[n];
  rep(i,n){
      rep(j,n-1){
          ll tmp ;cin >> tmp;
          tmp--;
          a[i].push(tmp);
      }
  }
  ll ans = 0;
  set<ll> s;
  rep(i,n){
      if(a[a[i].front()].front()==i){
          if(!s.count(i)){
              s.insert(i);
          }
          if(!s.count(a[i].front())){
              s.insert(a[i].front());
          }
      }
  }
  while(s.size()){
      set<ll> ss = s;
      s.clear();
      auto it = ss.begin();
      while(it!=ss.end()){
          a[*it].pop();
          it++;
      }
      it = ss.begin();
      while(it!=ss.end()){
        ll i = *it;
        if(a[i].size() && a[a[i].front()].size() &&  a[a[i].front()].front()==i){
          if(!s.count(i)){
              s.insert(i);
          }
          if(!s.count(a[i].front())){
              s.insert(a[i].front());
          }
        }
          it++;
      }
      ans++;
  }
  rep(i,n){
      if(a[i].size()){
          cout << -1 << endl;
          return 0;
      }
  }
  cout << ans << endl;
}

Submission Info

Submission Time
Task E - League
User mathda1
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1612 Byte
Status AC
Exec Time 416 ms
Memory 9088 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 23
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 256 KB
b08 AC 173 ms 8960 KB
b09 AC 72 ms 8960 KB
b10 AC 416 ms 9088 KB
b11 AC 415 ms 8960 KB
b12 AC 104 ms 2944 KB
b13 AC 87 ms 2432 KB
b14 AC 31 ms 1408 KB
b15 AC 27 ms 1024 KB
b16 AC 7 ms 768 KB
b17 AC 8 ms 512 KB
b18 AC 55 ms 1920 KB
b19 AC 45 ms 1792 KB
b20 AC 7 ms 768 KB
b21 AC 11 ms 640 KB
b22 AC 3 ms 512 KB
b23 AC 2 ms 384 KB