結果

問題 No.2780 The Bottle Imp
ユーザー tnakao0123
提出日時 2024-06-08 15:36:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 2,634 bytes
コンパイル時間 1,002 ms
コンパイル使用メモリ 76,704 KB
最終ジャッジ日時 2025-02-21 20:56:58
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:79:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   79 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:83:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   83 |     scanf("%d", &mi);
      |     ~~~~~^~~~~~~~~~~
main.cpp:86:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   86 |       scanf("%d", &aj), aj--;
      |       ~~~~~^~~~~~~~~~~

ソースコード

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