Submission #8568580


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.first<<","<<p.second<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
#else
#define dbg(...) {}
#endif

class SCC {
private:
  vector<vector<int>> &G;
  vector<vector<int>> &rG;
  vector<bool> used;
  vector<int> vs;
  int n;
  void dfs(int v){
    used[v] = true;
    for(auto to : G[v]) if(!used[to]) dfs(to);
    vs.pb(v);
  }
  void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    groups.back().pb(v);
    for(auto to : rG[v]) if(!used[to]) rdfs(to, k);
  }
public:
  vector<int> cmp; //頂点iが属するSCCのID
  vector<vector<int>> groups; // SCCのトポロジカル順i番目に属する頂点番号群
  int gc = 0; // group count
  SCC(vector<vector<int>> &g, vector<vector<int>> &r) : G(g), rG(r){
    n = G.size();
    used = vector<bool>(n, false);
    cmp.resize(n);
    rep(i,n) if(!used[i]) dfs(i);
    fill(all(used), false);
    for(int i=vs.size()-1; i>=0; i--) if(!used[vs[i]]){
      groups.pb(vector<int>());
      rdfs(vs[i], gc++);
    }
  }
  inline bool same(int i, int j){ return cmp[i]==cmp[j]; }
  inline int operator[](int i){ return cmp[i]; }
};

int main(){
  int n;
  cin>>n;
  map<pair<int,int>, int> num;
  rep(i,n) rep(j,i) {
    int x = num.size();
    num[{j,i}] = x;
  }

  vector<vector<int>> to(num.size());
  vector<vector<int>> from(num.size());

  rep(i,n){
    vector<int> tmp;
    rep(j,n-1){
      int k; cin>>k;
      k--;
      tmp.pb(num[{min(i,k), max(i,k)}]);
    }
    rep(j,n-2) {
      to[tmp[j]].pb(tmp[j+1]);
      from[tmp[j+1]].pb(tmp[j]);
    }
  }

  // SCC scc(to, from);

  // for(auto &v : scc.groups) if(v.size() > 1) {
  //   cout << -1 << endl;
  //   return 0;
  // }

  vector<int> nxt;
  vector<int> d(num.size());
  rep(i,num.size()) {
    d[i] = from[i].size();
    if(d[i]==0) nxt.pb(i);
  }

  int ans = 0;
  int visited = num.size();
  while(visited > 0) {
    ans++;
    vector<int> tmp;
    if(nxt.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    for(auto v : nxt) {
      visited--;
      for(auto i : to[v]) {
        d[i]--;
        if(d[i]==0) tmp.pb(i);
      }
    }
    swap(tmp, nxt);
  }

  cout << ans << endl;

  return 0;
}

Submission Info

Submission Time
Task E - League
User tossy
Language C++14 (GCC 5.4.1)
Score 500
Code Size 3023 Byte
Status AC
Exec Time 1032 ms
Memory 88064 KiB

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 KiB
a02 AC 1 ms 256 KiB
a03 AC 1 ms 256 KiB
b04 AC 1 ms 256 KiB
b05 AC 1 ms 256 KiB
b06 AC 1 ms 256 KiB
b07 AC 1 ms 256 KiB
b08 AC 1032 ms 88064 KiB
b09 AC 955 ms 88064 KiB
b10 AC 1003 ms 88064 KiB
b11 AC 1002 ms 87808 KiB
b12 AC 239 ms 25088 KiB
b13 AC 208 ms 21248 KiB
b14 AC 79 ms 10368 KiB
b15 AC 55 ms 7680 KiB
b16 AC 32 ms 4992 KiB
b17 AC 18 ms 2816 KiB
b18 AC 152 ms 16640 KiB
b19 AC 120 ms 13568 KiB
b20 AC 34 ms 5248 KiB
b21 AC 21 ms 3456 KiB
b22 AC 10 ms 1792 KiB
b23 AC 4 ms 640 KiB