Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/aoj-0275.test.cpp

Depends on

Code

// clang-format off
// competitive-verifier: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0275
// clang-format on

#include <iostream>
#include <utility>
#include <vector>

#include "../../graph/others/offline-dag-reachability.hpp"
#include "../../graph/shortest-path/dijkstra.hpp"

using namespace std;

int main() {
  int S, R, A, B, Q;
  cin >> S >> R;
  Graph<int> g(S);
  vector<int> U(R), V(R), C(R);
  for (int i = 0; i < R; i++) {
    cin >> U[i] >> V[i] >> C[i];
    --U[i], --V[i];
    g.add_edge(U[i], V[i], C[i]);
  }
  cin >> A >> B >> Q;
  --A, --B;
  auto pre = dijkstra(g, A).dist;
  auto suf = dijkstra(g, B).dist;

  Graph<int> dag(S);
  for (int i = 0; i < R; i++) {
    if (pre[U[i]] + C[i] + suf[V[i]] == pre[B])
      dag.add_directed_edge(U[i], V[i]);
    if (pre[V[i]] + C[i] + suf[U[i]] == pre[B])
      dag.add_directed_edge(V[i], U[i]);
  }
  vector<pair<int, int> > qs(Q);
  for (auto& p : qs) {
    cin >> p.first >> p.second;
    --p.first, --p.second;
  }
  auto ans = offline_dag_reachability(dag, qs);
  for (auto& p : ans) cout << (p ? "Yes\n" : "No\n");
}
#line 1 "test/verify/aoj-0275.test.cpp"
// clang-format off
// competitive-verifier: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0275
// clang-format on

#include <iostream>
#include <utility>
#include <vector>

#line 2 "graph/others/offline-dag-reachability.hpp"

#include <algorithm>
#include <cstdint>
#line 7 "graph/others/offline-dag-reachability.hpp"

#line 2 "graph/graph-template.hpp"

#include <cstddef>
#line 6 "graph/graph-template.hpp"

template <typename T = int>
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1)
      : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template <typename T = int>
struct Graph {
  std::vector<std::vector<Edge<T> > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  std::size_t size() const { return g.size(); }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(to, from, cost, es++);
  }

  void read(int M, int padding = -1, bool weighted = false,
            bool directed = false) {
    for (int i = 0; i < M; i++) {
      int a, b;
      std::cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if (weighted) std::cin >> c;
      if (directed)
        add_directed_edge(a, b, c);
      else
        add_edge(a, b, c);
    }
  }

  inline std::vector<Edge<T> >& operator[](const int& k) { return g[k]; }

  inline const std::vector<Edge<T> >& operator[](const int& k) const {
    return g[k];
  }
};

template <typename T = int>
using Edges = std::vector<Edge<T> >;
#line 2 "graph/others/topological-sort.hpp"

#include <stack>
#line 5 "graph/others/topological-sort.hpp"

#line 7 "graph/others/topological-sort.hpp"

/**
 * @brief Topological Sort(トポロジカルソート)
 *
 */
template <typename T>
std::vector<int> topological_sort(const Graph<T>& g) {
  const int N = (int)g.size();
  std::vector<int> deg(N);
  for (int i = 0; i < N; i++) {
    for (auto& to : g[i]) ++deg[to];
  }
  std::stack<int> st;
  for (int i = 0; i < N; i++) {
    if (deg[i] == 0) st.emplace(i);
  }
  std::vector<int> ord;
  while (!st.empty()) {
    auto p = st.top();
    st.pop();
    ord.emplace_back(p);
    for (auto& to : g[p]) {
      if (--deg[to] == 0) st.emplace(to);
    }
  }
  return ord;
}
#line 10 "graph/others/offline-dag-reachability.hpp"

/**
 * @brief Offline Dag Reachability(DAGの到達可能性クエリ)
 *
 */

template <typename T>
std::vector<int> offline_dag_reachability(
    const Graph<T>& g, std::vector<std::pair<int, int> >& qs) {
  const int N = (int)g.size();
  const int Q = (int)qs.size();
  auto ord = topological_sort(g);
  std::vector<int> ans(Q);
  for (int l = 0; l < Q; l += 64) {
    int r = std::min(Q, l + 64);
    std::vector<std::int64_t> dp(N);
    for (int k = l; k < r; k++) {
      dp[qs[k].first] |= std::int64_t(1) << (k - l);
    }
    for (auto& idx : ord) {
      for (auto& to : g[idx]) dp[to] |= dp[idx];
    }
    for (int k = l; k < r; k++) {
      ans[k] = (dp[qs[k].second] >> (k - l)) & 1;
    }
  }
  return ans;
}
#line 2 "graph/shortest-path/dijkstra.hpp"

#include <functional>
#include <limits>
#include <queue>
#include <tuple>
#line 9 "graph/shortest-path/dijkstra.hpp"

#line 11 "graph/shortest-path/dijkstra.hpp"

/**
 * @brief Dijkstra(単一始点最短路)
 *
 */
template <typename T>
struct ShortestPath {
  std::vector<T> dist;
  std::vector<int> from, id;
};

template <typename T>
ShortestPath<T> dijkstra(const Graph<T>& g, int s) {
  const auto INF = std::numeric_limits<T>::max();
  std::vector<T> dist(g.size(), INF);
  std::vector<int> from(g.size(), -1), id(g.size(), -1);
  using Pi = std::pair<T, int>;
  std::priority_queue<Pi, std::vector<Pi>, std::greater<> > que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while (!que.empty()) {
    T cost;
    int idx;
    std::tie(cost, idx) = que.top();
    que.pop();
    if (dist[idx] < cost) continue;
    for (auto& e : g[idx]) {
      auto next_cost = cost + e.cost;
      if (dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      from[e.to] = idx;
      id[e.to] = e.idx;
      que.emplace(dist[e.to], e.to);
    }
  }
  return {dist, from, id};
}
#line 11 "test/verify/aoj-0275.test.cpp"

using namespace std;

int main() {
  int S, R, A, B, Q;
  cin >> S >> R;
  Graph<int> g(S);
  vector<int> U(R), V(R), C(R);
  for (int i = 0; i < R; i++) {
    cin >> U[i] >> V[i] >> C[i];
    --U[i], --V[i];
    g.add_edge(U[i], V[i], C[i]);
  }
  cin >> A >> B >> Q;
  --A, --B;
  auto pre = dijkstra(g, A).dist;
  auto suf = dijkstra(g, B).dist;

  Graph<int> dag(S);
  for (int i = 0; i < R; i++) {
    if (pre[U[i]] + C[i] + suf[V[i]] == pre[B])
      dag.add_directed_edge(U[i], V[i]);
    if (pre[V[i]] + C[i] + suf[U[i]] == pre[B])
      dag.add_directed_edge(V[i], U[i]);
  }
  vector<pair<int, int> > qs(Q);
  for (auto& p : qs) {
    cin >> p.first >> p.second;
    --p.first, --p.second;
  }
  auto ans = offline_dag_reachability(dag, qs);
  for (auto& p : ans) cout << (p ? "Yes\n" : "No\n");
}

Test cases

Env Name Status Elapsed Memory
g++ testcase_00 :heavy_check_mark: AC 2 ms 4 MB
g++ testcase_01 :heavy_check_mark: AC 2 ms 4 MB
g++ testcase_02 :heavy_check_mark: AC 1 ms 4 MB
g++ testcase_03 :heavy_check_mark: AC 110 ms 20 MB
g++ testcase_04 :heavy_check_mark: AC 2 ms 4 MB
g++ testcase_05 :heavy_check_mark: AC 2 ms 4 MB
g++ testcase_06 :heavy_check_mark: AC 108 ms 20 MB
g++ testcase_07 :heavy_check_mark: AC 290 ms 27 MB
g++ testcase_08 :heavy_check_mark: AC 161 ms 23 MB
clang++ testcase_00 :heavy_check_mark: AC 2 ms 4 MB
clang++ testcase_01 :heavy_check_mark: AC 1 ms 4 MB
clang++ testcase_02 :heavy_check_mark: AC 1 ms 4 MB
clang++ testcase_03 :heavy_check_mark: AC 111 ms 20 MB
clang++ testcase_04 :heavy_check_mark: AC 2 ms 4 MB
clang++ testcase_05 :heavy_check_mark: AC 1 ms 4 MB
clang++ testcase_06 :heavy_check_mark: AC 113 ms 20 MB
clang++ testcase_07 :heavy_check_mark: AC 292 ms 27 MB
clang++ testcase_08 :heavy_check_mark: AC 163 ms 23 MB
Back to top page