結果

問題 No.2780 The Bottle Imp
ユーザー emthrm
提出日時 2024-06-07 21:49:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 2,884 bytes
コンパイル時間 4,835 ms
コンパイル使用メモリ 258,188 KB
実行使用メモリ 32,000 KB
最終ジャッジ日時 2024-12-27 13:41:09
合計ジャッジ時間 6,534 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <bool IS_FULL_VER = false>
struct StronglyConnectedComponents {
  std::vector<int> id;
  std::vector<std::vector<int>> vertices, g;

  explicit StronglyConnectedComponents(
      const std::vector<std::vector<int>>& graph)
      : n(graph.size()), is_used(n, false), graph(graph), rgraph(n) {
    order.reserve(n);
    for (int i = 0; i < n; ++i) {
      if (!is_used[i]) dfs(i);
    }
    id.assign(n, -1);
    for (int i = 0; i < n; ++i) {
      for (const int e : graph[i]) rgraph[e].emplace_back(i);
    }
    int m = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        if constexpr (IS_FULL_VER) vertices.emplace_back();
        rdfs(order[i], m++);
      }
    }
    g.resize(m);
    for (int i = 0; i < n; ++i) {
      for (const int e : graph[i]) {
        if (id[i] != id[e]) g[id[i]].emplace_back(id[e]);
      }
    }
    // if constexpr (IS_FULL_VER) {
    //   for (int i = 0; i < m; ++i) {
    //     std::sort(vertices[i].begin(), vertices[i].end());
    //   }
    // }
  }

 private:
  const int n;
  std::vector<bool> is_used;
  std::vector<int> order;
  std::vector<std::vector<int>> graph, rgraph;

  void dfs(const int ver) {
    is_used[ver] = true;
    for (const int e : graph[ver]) {
      if (!is_used[e]) dfs(e);
    }
    order.emplace_back(ver);
  }

  void rdfs(const int ver, const int m) {
    id[ver] = m;
    if constexpr (IS_FULL_VER) vertices.back().emplace_back(ver);
    for (const int e : rgraph[ver]) {
      if (id[e] == -1) rdfs(e, m);
    }
  }
};

int main() {
  int n; cin >> n;
  vector<vector<int>> graph(n);
  REP(i, n) {
    int m; cin >> m;
    while (m--) {
      int a; cin >> a; --a;
      graph[i].emplace_back(a);
    }
  }
  const StronglyConnectedComponents scc(graph);
  int num = 1;
  for (int i = scc.id[0]; ;) {
    int index = 0;
    while (index < scc.g[i].size() && scc.g[i][index] == i) ++index;
    if (index == scc.g[i].size()) break;
    i = scc.g[i][index];
    ++num;
  }
  cout << (num == scc.g.size() ? "Yes\n" : "No\n");
  return 0;
}
0