結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー 👑 emthrmemthrm
提出日時 2022-06-17 21:53:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 4,000 ms
コード長 3,070 bytes
コンパイル時間 2,391 ms
コンパイル使用メモリ 207,236 KB
実行使用メモリ 51,816 KB
最終ジャッジ日時 2024-04-17 13:41:07
合計ジャッジ時間 9,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 8 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 92 ms
17,280 KB
testcase_14 AC 116 ms
20,424 KB
testcase_15 AC 123 ms
22,940 KB
testcase_16 AC 52 ms
16,128 KB
testcase_17 AC 118 ms
21,632 KB
testcase_18 AC 108 ms
23,208 KB
testcase_19 AC 157 ms
29,508 KB
testcase_20 AC 112 ms
21,704 KB
testcase_21 AC 133 ms
23,460 KB
testcase_22 AC 152 ms
26,624 KB
testcase_23 AC 216 ms
32,600 KB
testcase_24 AC 212 ms
32,448 KB
testcase_25 AC 216 ms
32,776 KB
testcase_26 AC 204 ms
32,692 KB
testcase_27 AC 208 ms
32,784 KB
testcase_28 AC 204 ms
32,440 KB
testcase_29 AC 224 ms
32,496 KB
testcase_30 AC 211 ms
32,584 KB
testcase_31 AC 213 ms
32,588 KB
testcase_32 AC 208 ms
32,584 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 57 ms
14,848 KB
testcase_35 AC 151 ms
49,408 KB
testcase_36 AC 129 ms
27,392 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 29 ms
5,376 KB
testcase_39 AC 153 ms
51,816 KB
testcase_40 AC 130 ms
30,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#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)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
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;

struct Lowlink {
  std::vector<int> order, lowlink, articulation_points;
  std::vector<std::pair<int, int>> bridges;
  const std::vector<std::vector<int>> graph;

  explicit Lowlink(const std::vector<std::vector<int>>& graph)
      : graph(graph) {
    const int n = graph.size();
    order.assign(n, -1);
    lowlink.resize(n);
    int t = 0;
    for (int i = 0; i < n; ++i) {
      if (order[i] == -1) dfs(-1, i, &t);
    }
  }

 private:
  void dfs(const int par, const int ver, int* t) {
    order[ver] = lowlink[ver] = (*t)++;
    int num = 0;
    bool is_articulation_point = false;
    for (const int e : graph[ver]) {
      if (order[e] == -1) {
        ++num;
        dfs(ver, e, t);
        lowlink[ver] = std::min(lowlink[ver], lowlink[e]);
        if (order[ver] <= lowlink[e]) {
          is_articulation_point = true;
          if (order[ver] < lowlink[e]) {
            bridges.emplace_back(std::minmax(ver, e));
          }
        }
      } else if (e != par) {
        lowlink[ver] = std::min(lowlink[ver], order[e]);
      }
    }
    if ((par == -1 && num >= 2) || (par != -1 && is_articulation_point)) {
      articulation_points.emplace_back(ver);
    }
  }
};

struct UnionFind {
  explicit UnionFind(const int n) : data(n, -1) {}

  int root(const int ver) {
    return data[ver] < 0 ? ver : data[ver] = root(data[ver]);
  }

  bool unite(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    return true;
  }

  bool is_same(const int u, const int v) { return root(u) == root(v); }

  int size(const int ver) { return -data[root(ver)]; }

 private:
  std::vector<int> data;
};

int main() {
  int n, m, q; cin >> n >> m >> q;
  vector<vector<int>> graph(n);
  while (m--) {
    int u, v; cin >> u >> v; --u; --v;
    graph[u].emplace_back(v);
    graph[v].emplace_back(u);
  }
  Lowlink lowlink(graph);
  UnionFind union_find(n);
  for (const auto [u, v] : lowlink.bridges) union_find.unite(u, v);
  while (q--) {
    int x, y; cin >> x >> y; --x; --y;
    cout << (union_find.is_same(x, y) ? "Yes\n" : "No\n");
  }
  return 0;
}
0