library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub sash2104/library

:heavy_check_mark: test/aoj/ALDS_1_11_C.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C"

#include "../../graph/bin-bfs.hpp"
#include <iostream>
using namespace std; 

int main() {
  int n; cin >> n;
  Graph<int> g(n);
  for (int i = 0; i < n; ++i) {
    int u, k; cin >> u >> k;
    --u;
    for (int j = 0; j < k; ++j) {
      int v; cin >> v; --v;
      g[u].emplace_back(u, v, 1);
    }
  }
  BinBFS<int> bfs(g);
  bfs.init(0);
  for (int i = 0; i < n; ++i) {
    int d = bfs.dist[i];
    if (d == bfs.INF) d = -1;
    cout << i+1 << " " << d << endl;
  }
}
#line 1 "test/aoj/ALDS_1_11_C.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C"

#line 1 "graph/bin-bfs.hpp"


// @title 01-BFS
#line 1 "graph/template.hpp"



#include <iostream>
#include <vector>

template< typename T >
struct Edge {
  int from, to;
  T cost;
  Edge() {}
  Edge(int f, int t) : from(f), to(t), cost(1) {}
  Edge(int f, int t, T c) : from(f), to(t), cost(c) {}
  friend bool operator < (const Edge& lhs, const Edge& rhs) { return lhs.cost < rhs.cost; };
  friend bool operator > (const Edge& lhs, const Edge& rhs) { return rhs < lhs; };
  friend bool operator <= (const Edge& lhs, const Edge& rhs) { return !(lhs > rhs); };
  friend bool operator >= (const Edge& lhs, const Edge& rhs) { return !(lhs < rhs); };
};

template< typename T >
using Edges = std::vector< Edge< T > >;
template< typename T >
using Graph = std::vector< Edges< T > >;

template< typename T >
void debug(Graph<T> &g, int n = -1) {
    if (n == -1) n = g.size();
    for (int i = 0; i < n; ++i) {
        std::cerr << i  << "\t";
        for (auto &e: g[i]) {
            std::cerr << e.to << ",";
        }
        std::cerr << std::endl;
    }
}


#line 5 "graph/bin-bfs.hpp"

#include <algorithm>
#include <functional>
#include <limits>
#include <queue>
#include <utility>
#line 12 "graph/bin-bfs.hpp"

template < typename T >
struct BinBFS {
  using P = std::pair<T, int>; // <始点からの距離, 点のid>
  const T INF; // 十分に大きいがoverflowしない値として、型Tの最大値の半分を使う
  const int V; // 頂点数
  Graph<T> g;
  std::vector<T> dist; // 始点からの距離
  std::vector<bool> visit; // すでに探索済みの点か
  BinBFS(Graph<T> &g): INF(std::numeric_limits<T>::max()/2), V(g.size()), g(g), dist(V), visit(V) {}
  BinBFS(Graph<T> &g, int V): INF(std::numeric_limits<T>::max()/2), V(V), g(g), dist(V), visit(V) {}
  void init(int s) {
    std::fill(dist.begin(), dist.end(), INF);
    std::fill(visit.begin(), visit.end(), false);
    dist[s] = T(); // 始点の距離を0で初期化
    std::deque<P> q;
    q.push_front({dist[s], s});
    while (!q.empty()) {
      P node = q.front(); q.pop_front();
      int from = node.second;
      if (visit[from]) continue;
      visit[from] = true;
      for (auto &e : g[from]) {
        if (visit[e.to]) continue;
        T d = node.first + e.cost;
        if (d >= dist[e.to]) continue;
        if (e.cost == 0) {
          q.push_front({d, e.to});
        }
        else {
          q.push_back({d, e.to});
        }
        dist[e.to] = d;
      }
    }
  }
};


#line 5 "test/aoj/ALDS_1_11_C.test.cpp"
using namespace std; 

int main() {
  int n; cin >> n;
  Graph<int> g(n);
  for (int i = 0; i < n; ++i) {
    int u, k; cin >> u >> k;
    --u;
    for (int j = 0; j < k; ++j) {
      int v; cin >> v; --v;
      g[u].emplace_back(u, v, 1);
    }
  }
  BinBFS<int> bfs(g);
  bfs.init(0);
  for (int i = 0; i < n; ++i) {
    int d = bfs.dist[i];
    if (d == bfs.INF) d = -1;
    cout << i+1 << " " << d << endl;
  }
}
Back to top page