結果
| 問題 |
No.1326 ふたりのDominator
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-05-08 14:37:56 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 132 ms / 2,000 ms |
| コード長 | 9,433 bytes |
| コンパイル時間 | 3,963 ms |
| コンパイル使用メモリ | 296,572 KB |
| 実行使用メモリ | 55,584 KB |
| 最終ジャッジ日時 | 2025-05-08 14:38:03 |
| 合計ジャッジ時間 | 7,051 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 24 |
コンパイルメッセージ
tree/euler-tour.hpp: In instantiation of ‘struct EulerTour<Graph<bool> >’: /home/y_midori/cp/test/test.test.cpp:11:23: required from here tree/euler-tour.hpp:7:27: warning: ‘EulerTour<Graph<bool> >’ has a field ‘SparseTable<std::pair<int, int>, <lambda closure object>EulerTour<Graph<bool> >::<lambda(EulerTour<Graph<bool> >::pii, EulerTour<Graph<bool> >::pii)>()> EulerTour<Graph<bool> >::rmq’ whose type has internal linkage [-Wsubobject-linkage]
ソースコード
#line 1 "/home/y_midori/cp/test/test.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1326"
#line 2 "template.hpp"
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
template <class T>
concept Streamable = requires(ostream os, T &x) { os << x; };
template <class mint>
concept is_modint = requires(mint &x) {
{ x.val() } -> std::convertible_to<int>;
};
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif
template <Streamable T> void print_one(const T &value) { cout << value; }
template <is_modint T> void print_one(const T &value) { cout << value.val(); }
void print() { cout << '\n'; }
template <class T, class... Ts> void print(const T &a, const Ts &...b) {
print_one(a);
((cout << ' ', print_one(b)), ...);
cout << '\n';
}
template <ranges::range Iterable>
requires(!Streamable<Iterable>)
void print(const Iterable &v) {
for(auto it = v.begin(); it != v.end(); ++it) {
if(it != v.begin())
cout << " ";
print_one(*it);
}
cout << '\n';
}
using vi = vector<int>;
using vii = vector<vector<int>>;
using pii = pair<int, int>;
using ll = long long;
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;
#define all(v) begin(v), end(v)
#define UNIQUE(v) ranges::sort(v), v.erase(unique(all(v)), end(v))
template <typename T> inline bool chmax(T &a, T b) {
return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
return ((a > b) ? (a = b, true) : (false));
}
// https://trap.jp/post/1224/
template <class... T> constexpr auto min(T... a) {
return min(initializer_list<common_type_t<T...>>{a...});
}
template <class... T> constexpr auto max(T... a) {
return max(initializer_list<common_type_t<T...>>{a...});
}
template <class... T> void input(T &...a) { (cin >> ... >> a); }
template <class T> void input(vector<T> &a) {
for(T &x : a)
cin >> x;
}
#define INT(...) \
int __VA_ARGS__; \
input(__VA_ARGS__)
#define LL(...) \
long long __VA_ARGS__; \
input(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
input(__VA_ARGS__)
#define REP1(a) for(ll i = 0; i < a; i++)
#define REP2(i, a) for(ll i = 0; i < a; i++)
#define REP3(i, a, b) for(ll i = a; i < b; i++)
#define REP4(i, a, b, c) for(ll i = a; i < b; i += c)
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define rep1(i, n) for(ll i = 1; i <= ((ll)n); ++i)
ll inf = 3e18;
vl dx = {1, -1, 0, 0};
vl dy = {0, 0, 1, -1};
template <class T> T floor(T x, T y) {
return x / y - ((x ^ y) < 0 and x % y);
}
template <class T> T ceil(T x, T y) {
return y < 0 ? floor(-x + -y - 1, -y) : floor(x + y - 1, y);
}
// yの符号に関わらず非負で定義 \bmod:texコマンド
template <class T> T bmod(T x, T y) {
T m = x % y;
return (m < 0) ? m + (y > 0 ? y : -y) : m;
}
#line 3 "graph/graph-template.hpp"
/**
* @brief Graph Template(グラフテンプレート)
* @see https://ei1333.github.io/library/graph/graph-template.hpp
*/
template <class T = ll> 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) {}
Edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <class T = ll> struct Graph {
using cost_type = T;
vector<vector<Edge<T>>> g;
int es; // edge_size
Graph() = default;
explicit Graph(int n) : g(n), es(0) {};
int size() const { return ssize(g); }
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++);
}
vector<Edge<T>> &operator[](const int &k) { return g[k]; }
const vector<Edge<T>> &operator[](const int &k) const { return g[k]; }
void read(int m, int padding = -1, bool weighted = false,
bool directed = false) {
for(int i = 0; i < m; ++i) {
int a, b;
T c(1);
cin >> a >> b;
a += padding;
b += padding;
if(weighted)
cin >> c;
if(directed)
add_directed_edge(a, b, c);
else
add_edge(a, b, c);
}
}
};
#line 4 "graph/block-cut-tree.hpp"
/**
* @brief ブロックカット木的な
* https://maspypy.github.io/library/graph/block_cut.hpp
* https://twitter.com/noshi91/status/1529858538650374144
* N頂点の無向グラフがC個の二重連結成分を持つとき(N+C)頂点の森を作る
* [0,N)のidxは元のグラフの頂点を表し[N,N+C)のidxは二重連結成分を表す
* 辺(u,N+v)は頂点uがv番目の二重連結成分に属することを表す
* 関節点<->次数2以上
*/
template <class G> Graph<bool> block_cut_tree(const G &g) {
int n = g.size();
Graph<bool> tree(n);
vector<int> low(n, -1), ord(n, -1);
int id = 0;
vector<int> tmp;
tree.g.reserve(2 * n);
tmp.reserve(n);
auto dfs = [&](auto &dfs, int cur, int p) -> void {
ord[cur] = low[cur] = id++;
tmp.emplace_back(cur);
int cnt = 0;
for(auto &to : g[cur]) {
if(to == p)
continue;
if(ord[to] == -1) {
int sz = ssize(tmp);
++cnt;
dfs(dfs, to, cur);
chmin(low[cur], low[to]);
if((p == -1 and cnt > 1) or (p != -1 and low[to] >= ord[cur])) {
int k = tree.size();
tree.g.emplace_back();
tree.add_edge(cur, k);
while(ssize(tmp) > sz) {
tree.add_edge(tmp.back(), k);
tmp.pop_back();
}
}
} else {
chmin(low[cur], ord[to]);
}
}
if(p == -1) {
int k = tree.size();
tree.g.emplace_back();
for(auto &i : tmp)
tree.add_edge(i, k);
tmp.clear();
}
};
for(int i = 0; i < n; ++i) {
if(ord[i] == -1)
dfs(dfs, i, -1);
}
return tree;
}
#line 3 "data-structure/sparse-table.hpp"
/*
auto opには
[](int i,int j){return min(i,j);}や(int(*)(int,int))min
を渡す
↑このmin,std::minじゃないな
prod 単位元を渡していないので l==rは不可
*/
template <class T, auto op> struct SparseTable {
SparseTable(const vector<T> &vec) {
int n = vec.size();
int b = bit_width(unsigned(n));
v.resize(b);
v[0] = vec;
rep(i, b - 1) {
v[i + 1].resize(n - (1 << i));
rep(j, ssize(v[i]) - (1 << i)) {
v[i + 1][j] = op(v[i][j], v[i][j + (1 << i)]);
}
}
}
T prod(int l, int r) const {
if(l + 1 == r)
return v[0][l];
int b = bit_width(unsigned(r - l - 1)) - 1;
return op(v[b][l], v[b][r - (1 << b)]);
}
private:
vector<vector<T>> v;
};
#line 4 "tree/euler-tour.hpp"
// https://maspypy.com/euler-tour-%E3%81%AE%E3%81%8A%E5%8B%89%E5%BC%B7
// https://nyaannyaan.github.io/library/tree/euler-tour.hpp
template <class G> struct EulerTour {
int root, id;
vector<int> in, out, dep;
using pii = pair<int, int>; // depth,vartex
SparseTable<pii, [](pii a, pii b) { return a.first < b.first ? a : b; }>
rmq;
EulerTour(G &g, int root = 0)
: root(root), id(0), in(g.size(), -1), out(g.size(), -1),
dep(g.size(), 0), rmq([&] {
vector<pii> vec;
vec.reserve(2 * g.size());
dfs(g, root, -1, vec);
return vec;
}()) {}
int lca(int x, int y) const {
int ix = in[x], iy = in[y];
if(ix > iy)
swap(ix, iy);
return rmq.prod(ix, iy + 1).second;
}
int dist(int x, int y) const {
int l = lca(x, y);
return dep[x] + dep[y] - 2 * dep[l];
}
private:
void dfs(G &g, int now, int prev, vector<pii> &vec) {
vec.push_back({dep[now], now});
in[now] = id++;
for(auto nex : g[now]) {
if(nex == prev)
continue;
dep[nex] = dep[now] + 1;
dfs(g, nex, now, vec);
}
vec.push_back({dep[now] - 1, prev});
out[now] = id++;
}
};
#line 6 "/home/y_midori/cp/test/test.test.cpp"
void solve() {
INT(n, m);
Graph<bool> g(n);
g.read(m);
auto bct = block_cut_tree(g);
EulerTour tour(bct);
INT(q);
while(q--) {
INT(x, y);
--x, --y;
print((tour.dist(x, y) - 1) / 2);
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
}