結果
| 問題 |
No.1983 [Cherry 4th Tune C] 南の島のマーメイド
|
| コンテスト | |
| ユーザー |
sotanishy
|
| 提出日時 | 2022-06-17 22:28:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 281 ms / 4,000 ms |
| コード長 | 3,106 bytes |
| コンパイル時間 | 2,179 ms |
| コンパイル使用メモリ | 205,524 KB |
| 最終ジャッジ日時 | 2025-01-29 22:19:36 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }
class Lowlink {
public:
Lowlink() = default;
explicit Lowlink(const std::vector<std::vector<int>>& G) : G(G), ord(G.size(), -1), low(G.size()) {
for (int i = 0; i < (int) G.size(); ++i) {
if (ord[i] == -1) dfs(i, -1);
}
}
std::vector<std::pair<int, int>> get_bridges() const {
return bridge;
}
std::vector<int> get_articulation_points() const {
return articulation;
}
bool is_bridge(int u, int v) const {
if (ord[u] > ord[v]) std::swap(u, v);
return ord[u] < low[v];
}
protected:
std::vector<std::vector<int>> G;
std::vector<int> ord, low;
std::vector<std::pair<int, int>> bridge;
std::vector<int> articulation;
private:
int k = 0;
void dfs(int v, int p) {
ord[v] = k++;
low[v] = ord[v];
bool is_articulation = false, checked = false;
int cnt = 0;
for (int c : G[v]) {
if (c == p && !checked) {
checked = true;
continue;
}
if (ord[c] == -1) {
++cnt;
dfs(c, v);
low[v] = std::min(low[v], low[c]);
if (p != -1 && ord[v] <= low[c]) is_articulation = true;
if (ord[v] < low[c]) bridge.push_back(std::minmax(v, c));
} else {
low[v] = std::min(low[v], ord[c]);
}
}
if (p == -1 && cnt > 1) is_articulation = true;
if (is_articulation) articulation.push_back(v);
}
};
class UnionFind {
public:
UnionFind() = default;
explicit UnionFind(int n) : data(n, -1) {}
int find(int x) {
if (data[x] < 0) return x;
return data[x] = find(data[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (data[x] > data[y]) std::swap(x, y);
data[x] += data[y];
data[y] = x;
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return -data[find(x)];
}
private:
std::vector<int> data;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int N, M, Q;
cin >> N >> M >> Q;
vector<vector<int>> G(N);
rep(_,0,M) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v);
G[v].push_back(u);
}
Lowlink low(G);
UnionFind uf(N);
rep(i,0,N) for (int j : G[i]) if (low.is_bridge(i, j)) uf.unite(i, j);
while (Q--) {
int x, y;
cin >> x >> y;
--x, --y;
cout << (uf.same(x, y) ? "Yes" : "No") << "\n";
}
}
sotanishy