結果

問題 No.2780 The Bottle Imp
ユーザー tnakao0123tnakao0123
提出日時 2024-06-08 15:36:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 2,634 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 78,536 KB
実行使用メモリ 38,372 KB
最終ジャッジ日時 2024-06-08 15:36:40
合計ジャッジ時間 3,490 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,984 KB
testcase_01 AC 5 ms
10,112 KB
testcase_02 AC 5 ms
10,112 KB
testcase_03 AC 5 ms
10,112 KB
testcase_04 AC 5 ms
10,112 KB
testcase_05 AC 5 ms
9,984 KB
testcase_06 AC 5 ms
10,112 KB
testcase_07 AC 38 ms
16,396 KB
testcase_08 AC 38 ms
16,428 KB
testcase_09 AC 38 ms
16,432 KB
testcase_10 AC 38 ms
16,400 KB
testcase_11 AC 37 ms
16,408 KB
testcase_12 AC 67 ms
23,288 KB
testcase_13 AC 65 ms
23,264 KB
testcase_14 AC 20 ms
12,288 KB
testcase_15 AC 19 ms
12,288 KB
testcase_16 AC 20 ms
12,416 KB
testcase_17 AC 25 ms
12,288 KB
testcase_18 AC 19 ms
12,160 KB
testcase_19 AC 21 ms
12,288 KB
testcase_20 AC 20 ms
12,288 KB
testcase_21 AC 20 ms
12,288 KB
testcase_22 AC 15 ms
12,160 KB
testcase_23 AC 19 ms
12,416 KB
testcase_24 AC 32 ms
15,288 KB
testcase_25 AC 49 ms
19,556 KB
testcase_26 AC 25 ms
13,820 KB
testcase_27 AC 19 ms
12,288 KB
testcase_28 AC 18 ms
12,160 KB
testcase_29 AC 34 ms
17,424 KB
testcase_30 AC 22 ms
15,668 KB
testcase_31 AC 29 ms
14,336 KB
testcase_32 AC 13 ms
10,624 KB
testcase_33 AC 44 ms
28,800 KB
testcase_34 AC 62 ms
38,372 KB
testcase_35 AC 14 ms
10,496 KB
testcase_36 AC 6 ms
10,112 KB
testcase_37 AC 6 ms
10,112 KB
testcase_38 AC 12 ms
10,496 KB
testcase_39 AC 30 ms
18,048 KB
testcase_40 AC 29 ms
18,048 KB
testcase_41 AC 29 ms
18,048 KB
testcase_42 AC 6 ms
10,112 KB
testcase_43 AC 5 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2780.cc:  No.2780 The Bottle Imp - yukicoder
 */

#include<cstdio>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef stack<int> sti;
typedef set<int> si;

/* global variables */

vi nbrs[MAX_N];
int gids[MAX_N], pns[MAX_N], vs[MAX_N];
si gnbrs[MAX_N];

/* subroutines */

void scc_visit(const vi *nbrs, int v, vvi& scc,
	       sti& S, vb &inS, vi& low, vi& num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;

  const vi& nbrv = nbrs[v];
  for (vi::const_iterator vit = nbrv.begin(); vit != nbrv.end(); vit++) {
    const int& w = *vit;
    if (num[w] == 0) {
      scc_visit(nbrs, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    }
    else if (inS[w])
      low[v] = min(low[v], num[w]);
  }

  if (low[v] == num[v]) {
    scc.push_back(vi());
    for (;;) {
      int w = S.top(); S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}

void calc_scc(const int n, const vi *nbrs, vvi& scc) {
  vi num(n), low(n);
  sti S;
  vb inS(n);
  int time = 0;

  for (int u = 0; u < n; u++)
    if (num[u] == 0)
      scc_visit(nbrs, u, scc, S, inS, low, num, time);
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    int mi;
    scanf("%d", &mi);
    for (int j = 0; j < mi; j++) {
      int aj;
      scanf("%d", &aj), aj--;
      nbrs[i].push_back(aj);
    }
  }

  vvi scc;
  calc_scc(n, nbrs, scc);

  int gn = scc.size();
  for (int i = 0; i < gn; i++)
    for (auto u: scc[i]) gids[u] = i;
  if (false) {
    printf(" gn=%d\n", gn);
    for (int i = 0; i < gn; i++) {
      for (auto u: scc[i]) printf(" %d", u); putchar('\n');
    }
  }

  for (int u = 0; u < n; u++) {
    int gu = gids[u];
    for (auto v: nbrs[u]) {
      int gv = gids[v];
      if (gu != gv) gnbrs[gu].insert(gv);
    }
  }

  for (int u = 0; u < gn; u++)
    for (auto v: gnbrs[u]) pns[v]++;

  qi q;
  for (int u = 0; u < gn; u++)
    if (pns[u] == 0) q.push(u);

  int k = 0;
  while (! q.empty()) {
    int u = q.front(); q.pop();
    vs[k++] = u;
    for (auto v: gnbrs[u])
      if (--pns[v] == 0) q.push(v);
  }

  if (k == gn && gids[0] == vs[0]) {
    for (int i = 0; i + 1 < gn; i++) {
      int u = vs[i], v = vs[i + 1];
      if (gnbrs[u].find(v) == gnbrs[u].end()) {
	puts("No"); return 0;
      }
    }
    puts("Yes");
  }
  else
    puts("No");

  return 0;
}
0