Skip to the content.

:heavy_check_mark: test/AOJ/ALDS1_11_C.test.cpp

Depends on

Code

#define PROBLEM                                                                \
    "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C"
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (n); i++)

#include "library/graph/Graph.hpp"
#include "library/graph/shortest_path/BFS.hpp"

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;
    Graph g(n);
    REP (_, n) {
        int u, k;
        std::cin >> u >> k;
        u--;
        REP (_, k) {
            int v;
            std::cin >> v;
            v--;
            g.add_arc(u, v);
        }
    }
    g.build();
    auto [d, pre] = BFS(g);
    REP (i, n)
        std::cout << i + 1 << " " << d[i] << "\n";
}
#line 1 "test/AOJ/ALDS1_11_C.test.cpp"
#define PROBLEM                                                                \
    "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C"
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (n); i++)

#line 2 "library/graph/Graph.hpp"

#line 6 "library/graph/Graph.hpp"

struct Edge {
    int from, to;
    Edge() = default;
    Edge(int from, int to) : from(from), to(to) {}
    operator int() const { return to; }
};

struct Graph {
    int n;
    using edge_type = Edge;
    std::vector<edge_type> edges;

  protected:
    std::vector<int> in_deg;
    bool prepared;
    class OutgoingEdges {
        Graph *g;
        int l, r;

      public:
        OutgoingEdges(Graph *g, int l, int r) : g(g), l(l), r(r) {}
        edge_type *begin() { return &(g->edges[l]); }
        edge_type *end() { return &(g->edges[r]); }
        edge_type &operator[](int i) { return g->edges[l + i]; }
        int size() const { return r - l; }
    };
    class ConstOutgoingEdges {
        const Graph *g;
        int l, r;

      public:
        ConstOutgoingEdges(const Graph *g, int l, int r) : g(g), l(l), r(r) {}
        const edge_type *begin() const { return &(g->edges[l]); }
        const edge_type *end() const { return &(g->edges[r]); }
        const edge_type &operator[](int i) const { return g->edges[l + i]; }
        int size() const { return r - l; }
    };

  public:
    OutgoingEdges operator[](int v) {
        assert(prepared);
        return {this, in_deg[v], in_deg[v + 1]};
    }
    const ConstOutgoingEdges operator[](int v) const {
        assert(prepared);
        return {this, in_deg[v], in_deg[v + 1]};
    }

    bool is_prepared() const { return prepared; }

    Graph() : n(0), in_deg(1, 0), prepared(false) {}
    Graph(int n) : n(n), in_deg(n + 1, 0), prepared(false) {}
    Graph(int n, int m, bool directed = false, int indexed = 1)
        : n(n), in_deg(n + 1, 0), prepared(false) {
        scan(m, directed, indexed);
    }

    void resize(int n) { n = n; }

    void add_arc(int from, int to) {
        assert(!prepared);
        assert(0 <= from and from < n and 0 <= to and to < n);
        edges.emplace_back(from, to);
        in_deg[from + 1]++;
    }
    void add_edge(int u, int v) {
        add_arc(u, v);
        add_arc(v, u);
    }
    void add_arc(const edge_type &e) { add_arc(e.from, e.to); }
    void add_edge(const edge_type &e) { add_edge(e.from, e.to); }

    void scan(int m, bool directed = false, int indexed = 1) {
        edges.reserve(directed ? m : 2 * m);
        while (m--) {
            int u, v;
            std::cin >> u >> v;
            u -= indexed;
            v -= indexed;
            if (directed)
                add_arc(u, v);
            else
                add_edge(u, v);
        }
        build();
    }

    void build() {
        assert(!prepared);
        prepared = true;
        for (int v = 0; v < n; v++)
            in_deg[v + 1] += in_deg[v];
        std::vector<edge_type> new_edges(in_deg.back());
        auto counter = in_deg;
        for (auto &&e : edges)
            new_edges[counter[e.from]++] = e;
        edges = new_edges;
    }

    void graph_debug() const {
#ifndef __LOCAL
        return;
#endif
        assert(prepared);
        for (int from = 0; from < n; from++) {
            std::cerr << from << ";";
            for (int i = in_deg[from]; i < in_deg[from + 1]; i++)
                std::cerr << edges[i].to << " ";
            std::cerr << "\n";
        }
    }
};
#line 2 "library/graph/shortest_path/BFS.hpp"
template <typename GRAPH>
std::pair<std::vector<int>, std::vector<int>> BFS(const GRAPH &g, int s = 0) {
    assert(g.is_prepared());
    std::vector<int> d(g.n, -1), pre(g.n, -1);
    std::queue<int> que;
    d[s] = 0;
    que.push(s);
    while (que.size()) {
        int v = que.front();
        que.pop();
        for (int to : g[v])
            if (d[to] < 0) {
                d[to] = d[v] + 1;
                que.push(to);
            }
    }
    return {d, pre};
}
#line 9 "test/AOJ/ALDS1_11_C.test.cpp"

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;
    Graph g(n);
    REP (_, n) {
        int u, k;
        std::cin >> u >> k;
        u--;
        REP (_, k) {
            int v;
            std::cin >> v;
            v--;
            g.add_arc(u, v);
        }
    }
    g.build();
    auto [d, pre] = BFS(g);
    REP (i, n)
        std::cout << i + 1 << " " << d[i] << "\n";
}
Back to top page