This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C"
#include <bits/stdc++.h>
#include "ugilib/base/constants.hpp"
#include "ugilib/base/definitions.hpp"
#include "ugilib/graph/bfs.hpp"
using namespace std;
// debug settings
// #define DEBUG
#ifdef DEBUG
// debug input
string _INPUT = R"(
5
1 2 3 4 5
)";
auto _cin = stringstream(_INPUT.substr(1)); // remove '\n' at _INPUT[0]
#else
// standard input
auto& _cin = cin;
#endif
// speed up
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// reader
struct rd {
static ll i() {ll i; _cin >> i; return i;} // long long
static ld d() {ld d; _cin >> d; return d;} // long double
static string s() {string s; _cin >> s; return s;} // string
static char c() {char c; _cin >> c; return c;} // char
static vector<ll> vi(int n) {vector<ll> v(n); rep(i, n) _cin >> v[i]; return v;} // vector<long long>
static vector<pair<ll, ll>> g(int n) {vector<pair<ll, ll>> v(n); rep(i, n) _cin >> v[i].first >> v[i].second; return v;} // vector<pair<long long, long long>>
// tuple
template<typename... Args> static tuple<Args...> t() {
tuple<Args...> values;
apply([](auto&... args) { ((_cin >> args), ...); }, values);
return values;
}
};
// debug print utility
namespace deb {
#include <cxxabi.h>
// demangle type name
string demangle(const char* name) {
int status = -4;
unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
free
};
return (status == 0) ? string(res.get()) : name ;
}
// meta functions for type traits
template<typename T>
constexpr bool isArithmeticContainer() { return is_arithmetic<typename T::value_type>::value; }
// for SFINAE
template<typename T, typename = void> struct has_key_and_mapped_type : false_type {};
template<typename T> struct has_key_and_mapped_type<T, void_t<typename T::key_type, typename T::mapped_type>> : true_type {};
// for map or unordered_map
template<typename T>
constexpr bool isMapLike() { return has_key_and_mapped_type<T>::value; }
// for values
template<typename T, typename enable_if<is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& x) { cout << x << " "; }
// for pairs
template <typename T, typename S>
void p(const pair<T, S>& _p){ p(_p.first); p(_p.second); cout << endl; }
// for containers
template<typename T, typename enable_if<!is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& container) {
// map and unordered_map
if constexpr (isMapLike<T>()) {
cout << demangle(typeid(T).name()) << ":" << endl;
for (const auto& kv : container) {
cout << "[" << kv.first << "] => ";
p(kv.second);
if constexpr (is_arithmetic_v<typename T::mapped_type>) cout << endl;
}
// vector or set or others
} else {
if constexpr (!isArithmeticContainer<T>()) cout << demangle(typeid(T).name()) << ":" << endl;
for (auto it = begin(container); it != end(container); ++it) {
p(*it);
}
if constexpr (isArithmeticContainer<T>()) cout << endl;
}
}
}; // namespace deb
int main() {
auto& cin = _cin;
// speed up io
cin.tie(nullptr);
ios::sync_with_stdio(false);
// code
ll n = rd::i();
vector<vector<pair<int, int>>> graph(n);
rep(i, n) {
auto [u, k] = rd::t<int, int>();
rep(j, k) {
auto v = rd::i();
graph[u - 1].emplace_back(v - 1, 1);
}
}
auto costs = ugilib::bfs01(n, 0, graph);
rep(i, n) {
cout << i + 1 << " " << (costs[i] == ugilib::constants::INF<int> ? -1 : costs[i]) << endl;
}
return 0;
}
#line 1 "tests/graph/bfs.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C"
#include <bits/stdc++.h>
#line 2 "ugilib/base/definitions.hpp"
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define rep(i, n) for(size_t i = 0; i < n; i++) // rep macro
#define all(v) begin(v), end(v) // all iterator
#line 3 "ugilib/base/constants.hpp"
namespace ugilib::constants {
template<typename T>
inline constexpr T INF = std::numeric_limits<T>::max() / 2;
} // namespace ugilib::constants
const ll INF = ugilib::constants::INF<ll>;
#line 4 "ugilib/graph/bfs.hpp"
using namespace std;
namespace ugilib {
/**
* @brief 0or1幅優先探索. 01BFS
* @param n グラフの頂点数
* @param start 始点
* @param graph グラフ. vector<pair<int, weight_type>> で隣接頂点とコストを表す. コストは0 or 1
* @param weight_inf 無限大の値. パスが存在しない場合のコスト
* @return 始点から各頂点までの最短距離
* @note O(E+V)
* @todo 01BFS問題でのテストを書く
*/
template <typename weight_type>
vector<weight_type> bfs01(int n, int start, const vector<vector<pair<int, weight_type>>> &graph, weight_type weight_inf = ugilib::constants::INF<weight_type>) {
vector<weight_type> costs(n, weight_inf);
costs[start] = 0;
deque<int> next_nodes;
next_nodes.push_back(start);
while (!next_nodes.empty()) {
auto node = next_nodes.front(); next_nodes.pop_front();
auto cost = costs[node];
for (const auto [next_node, next_cost] : graph[node]) {
auto new_cost = cost + next_cost;
if (new_cost < costs[next_node]) {
costs[next_node] = new_cost;
// 0or1幅優先探索の場合, コストが0の場合は前から入れ, 1の場合は後ろに入れる
if (next_cost == 0) next_nodes.push_front(next_node);
else next_nodes.push_back(next_node);
}
}
}
return costs;
}
} // namespace ugilib
#line 7 "tests/graph/bfs.test.cpp"
using namespace std;
// debug settings
// #define DEBUG
#ifdef DEBUG
// debug input
string _INPUT = R"(
5
1 2 3 4 5
)";
auto _cin = stringstream(_INPUT.substr(1)); // remove '\n' at _INPUT[0]
#else
// standard input
auto& _cin = cin;
#endif
// speed up
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// reader
struct rd {
static ll i() {ll i; _cin >> i; return i;} // long long
static ld d() {ld d; _cin >> d; return d;} // long double
static string s() {string s; _cin >> s; return s;} // string
static char c() {char c; _cin >> c; return c;} // char
static vector<ll> vi(int n) {vector<ll> v(n); rep(i, n) _cin >> v[i]; return v;} // vector<long long>
static vector<pair<ll, ll>> g(int n) {vector<pair<ll, ll>> v(n); rep(i, n) _cin >> v[i].first >> v[i].second; return v;} // vector<pair<long long, long long>>
// tuple
template<typename... Args> static tuple<Args...> t() {
tuple<Args...> values;
apply([](auto&... args) { ((_cin >> args), ...); }, values);
return values;
}
};
// debug print utility
namespace deb {
#include <cxxabi.h>
// demangle type name
string demangle(const char* name) {
int status = -4;
unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
free
};
return (status == 0) ? string(res.get()) : name ;
}
// meta functions for type traits
template<typename T>
constexpr bool isArithmeticContainer() { return is_arithmetic<typename T::value_type>::value; }
// for SFINAE
template<typename T, typename = void> struct has_key_and_mapped_type : false_type {};
template<typename T> struct has_key_and_mapped_type<T, void_t<typename T::key_type, typename T::mapped_type>> : true_type {};
// for map or unordered_map
template<typename T>
constexpr bool isMapLike() { return has_key_and_mapped_type<T>::value; }
// for values
template<typename T, typename enable_if<is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& x) { cout << x << " "; }
// for pairs
template <typename T, typename S>
void p(const pair<T, S>& _p){ p(_p.first); p(_p.second); cout << endl; }
// for containers
template<typename T, typename enable_if<!is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& container) {
// map and unordered_map
if constexpr (isMapLike<T>()) {
cout << demangle(typeid(T).name()) << ":" << endl;
for (const auto& kv : container) {
cout << "[" << kv.first << "] => ";
p(kv.second);
if constexpr (is_arithmetic_v<typename T::mapped_type>) cout << endl;
}
// vector or set or others
} else {
if constexpr (!isArithmeticContainer<T>()) cout << demangle(typeid(T).name()) << ":" << endl;
for (auto it = begin(container); it != end(container); ++it) {
p(*it);
}
if constexpr (isArithmeticContainer<T>()) cout << endl;
}
}
}; // namespace deb
int main() {
auto& cin = _cin;
// speed up io
cin.tie(nullptr);
ios::sync_with_stdio(false);
// code
ll n = rd::i();
vector<vector<pair<int, int>>> graph(n);
rep(i, n) {
auto [u, k] = rd::t<int, int>();
rep(j, k) {
auto v = rd::i();
graph[u - 1].emplace_back(v - 1, 1);
}
}
auto costs = ugilib::bfs01(n, 0, graph);
rep(i, n) {
cout << i + 1 << " " << (costs[i] == ugilib::constants::INF<int> ? -1 : costs[i]) << endl;
}
return 0;
}