提出 #7314254


ソースコード 拡げる

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

map<PLL, vector<PLL>> nexts; // edges[v]: {u1, u2, ..}
map<PLL, ll> prevs;
vector<ll> a[2000];

PLL ver(ll i, ll j) {
  if (i > j)
    swap(i, j);
  return PLL(i, j);
}

signed main() {
  ll N;
  cin >> N;
  rep(i,N) {
    rep(j,N-1) {
      ll x;
      cin >> x;
      x--;
      a[i].push_back(x);
    }
  }
  rep(i,N) {
    rep(j,N-2) {
      auto cur = ver(i, a[i][j]);
      auto nex = ver(i, a[i][j+1]);
      nexts[cur].push_back(nex);
      prevs[nex]++;
      // printf("cur: (%d, %d) nex: (%d, %d)\n", cur.first, cur.second, nex.first, nex.second);
    }
  }

  // deleted candinates
  vector<PLL> can;
  rep(i,N) rep(j,i) {
    auto v = ver(i,j);
    if (prevs[v] == 0){
      can.push_back(v);
    }
  }
  //for (auto e : can) {
  //  printf("(%d, %d)\n", e.first, e.second);
  //}

  ll ans = 0;
  ll done = 0;
  while (done < N*(N-1)/2) {
    if (can.size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    vector<PLL> t;
    for (auto v : can) {
      done++;
      for (auto u: nexts[v]) {
        prevs[u]--;
        if (prevs[u] == 0) {
          t.push_back(u);
        }
      }
    }
    can = t;
    ans++;
  }

  cout << ans << endl;
  return 0;
}

提出情報

提出日時
問題 E - League
ユーザ bobuhiro11
言語 C++14 (GCC 5.4.1)
得点 500
コード長 1481 Byte
結果 AC
実行時間 1990 ms
メモリ 111744 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 23
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
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 1733 ms 109696 KiB
b09 AC 1165 ms 111744 KiB
b10 AC 1989 ms 109696 KiB
b11 AC 1990 ms 109568 KiB
b12 AC 460 ms 33280 KiB
b13 AC 375 ms 26496 KiB
b14 AC 137 ms 13312 KiB
b15 AC 101 ms 9984 KiB
b16 AC 43 ms 6144 KiB
b17 AC 30 ms 3584 KiB
b18 AC 299 ms 20864 KiB
b19 AC 234 ms 17152 KiB
b20 AC 48 ms 6528 KiB
b21 AC 42 ms 4352 KiB
b22 AC 12 ms 2304 KiB
b23 AC 5 ms 896 KiB