結果

問題 No.1326 ふたりのDominator
ユーザー 👑 NachiaNachia
提出日時 2022-01-27 19:41:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 10,139 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 98,836 KB
実行使用メモリ 13,292 KB
最終ジャッジ日時 2024-09-23 03:06:21
合計ジャッジ時間 3,521 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 60 ms
8,244 KB
testcase_13 AC 59 ms
8,376 KB
testcase_14 AC 59 ms
8,256 KB
testcase_15 AC 58 ms
8,172 KB
testcase_16 AC 57 ms
8,304 KB
testcase_17 AC 56 ms
8,648 KB
testcase_18 AC 66 ms
10,496 KB
testcase_19 AC 54 ms
10,712 KB
testcase_20 AC 56 ms
11,392 KB
testcase_21 AC 57 ms
11,296 KB
testcase_22 AC 68 ms
13,292 KB
testcase_23 AC 47 ms
8,244 KB
testcase_24 AC 59 ms
8,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <vector>
#include <utility>
namespace nachia{
struct AdjacencyList{
public:
struct AdjacencyListRange{
using iterator = typename std::vector<int>::const_iterator;
iterator begi, endi;
iterator begin() const { return begi; }
iterator end() const { return endi; }
int size() const { return (int)std::distance(begi, endi); }
const int& operator[](int i) const { return begi[i]; }
};
private:
int mn;
std::vector<int> E;
std::vector<int> I;
public:
AdjacencyList(int n, std::vector<std::pair<int,int>> edges, bool rev){
mn = n;
std::vector<int> buf(n+1, 0);
for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; }
for(int i=1; i<=n; i++) buf[i] += buf[i-1];
E.resize(buf[n]);
for(int i=(int)edges.size()-1; i>=0; i--){
auto [u,v] = edges[i];
E[--buf[u]] = v;
if(rev) E[--buf[v]] = u;
}
I = std::move(buf);
}
AdjacencyList(const std::vector<std::vector<int>>& edges = {}){
int n = mn = edges.size();
std::vector<int> buf(n+1, 0);
for(int i=0; i<n; i++) buf[i+1] = buf[i] + edges[i].size();
E.resize(buf[n]);
for(int i=0; i<n; i++) for(int j=0; j<(int)edges[i].size(); j++) E[buf[i]+j] = edges[i][j];
I = std::move(buf);
}
static AdjacencyList from_raw(std::vector<int> targets, std::vector<int> bounds){
AdjacencyList res;
res.mn = bounds.size() - 1;
res.E = std::move(targets);
res.I = std::move(bounds);
return res;
}
AdjacencyListRange operator[](int u) const {
return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
}
int num_vertices() const { return mn; }
int num_edges() const { return E.size(); }
AdjacencyList reversed_edges() const {
AdjacencyList res;
int n = res.mn = mn;
std::vector<int> buf(n+1, 0);
for(int v : E) ++buf[v];
for(int i=1; i<=n; i++) buf[i] += buf[i-1];
res.E.resize(buf[n]);
for(int u=0; u<n; u++) for(int v : operator[](u)) res.E[--buf[v]] = u;
res.I = std::move(buf);
return res;
}
};
struct AdjacencyListEdgeIndexed{
public:
struct Edge { int to; int edgeidx; };
struct AdjacencyListRange{
using iterator = typename std::vector<Edge>::const_iterator;
iterator begi, endi;
iterator begin() const { return begi; }
iterator end() const { return endi; }
int size() const { return (int)std::distance(begi, endi); }
const Edge& operator[](int i) const { return begi[i]; }
};
private:
int mn;
std::vector<Edge> E;
std::vector<int> I;
public:
AdjacencyListEdgeIndexed(int n, const std::vector<std::pair<int,int>>& edges, bool rev){
mn = n;
std::vector<int> buf(n+1, 0);
for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; }
for(int i=1; i<=n; i++) buf[i] += buf[i-1];
E.resize(buf[n]);
for(int i=(int)edges.size()-1; i>=0; i--){
auto [u,v] = edges[i];
E[--buf[u]] = { v, i };
if(rev) E[--buf[v]] = { u, i };
}
I = std::move(buf);
}
AdjacencyListEdgeIndexed() : AdjacencyListEdgeIndexed(0, {}, false) {}
AdjacencyListRange operator[](int u) const {
return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] };
}
int num_vertices() const { return mn; }
int num_edges() const { return E.size(); }
AdjacencyListEdgeIndexed reversed_edges() const {
AdjacencyListEdgeIndexed res;
int n = res.mn = mn;
std::vector<int> buf(n+1, 0);
for(auto [v,i] : E) ++buf[v];
for(int i=1; i<=n; i++) buf[i] += buf[i-1];
res.E.resize(buf[n]);
for(int u=0; u<n; u++) for(auto [v,i] : operator[](u)) res.E[--buf[v]] = {u,i};
res.I = std::move(buf);
return res;
}
};
} // namespace nachia
#include <vector>
namespace nachia{
class BiconnectedComponents{
private:
int mn;
int mm;
int mnum_bcs;
std::vector<std::pair<int, int>> medges;
std::vector<int> edgeidx_to_bcidx;
public:
BiconnectedComponents(int n, std::vector<std::pair<int, int>> edges){
std::vector<int> dfsi_to_vtx;
std::vector<int> vtx_to_dfsi;
std::vector<int> linked_over;
std::vector<int> dfs_parent;
mn = n;
int m = edges.size();
medges = std::move(edges);
nachia::AdjacencyListEdgeIndexed adj(n, medges, true);
dfsi_to_vtx.resize(n);
vtx_to_dfsi.resize(n);
dfs_parent.assign(n, -1);
linked_over.assign(n, -1);
int dfsi = 0;
auto dfs1 = [&](int p, auto self)->int {
vtx_to_dfsi[p] = dfsi;
dfsi_to_vtx[dfsi] = p;
int backedge = dfsi;
dfsi++;
for(auto [nx,i] : adj[p]){
if(dfs_parent[nx] != -1) backedge = std::min(backedge, vtx_to_dfsi[nx]);
else{
dfs_parent[nx] = i;
int link = self(nx, self);
backedge = std::min(backedge, link);
linked_over[nx] = (link < vtx_to_dfsi[p]) ? 1 : 0;
}
}
return backedge;
};
for(int i=0; i<n; i++) if(dfs_parent[i] == -1){
dfs_parent[i] = -2;
dfs1(i, dfs1);
}
std::vector<int> res(m);
auto dfs2 = [&](int p, int bcid, int& maxbcid, auto self)-> void {
if(dfs_parent[p] < 0){
for(auto [nx,i] : adj[p]) if(dfs_parent[nx] == i){
bcid = maxbcid++;
self(nx, bcid, maxbcid, self);
}
return;
}
for(auto [nx,i] : adj[p]) if(dfs_parent[nx] != i) res[i] = bcid;
for(auto [nx,i] : adj[p]) if(dfs_parent[nx] == i){
int nx_bcid = bcid;
if(!linked_over[nx]) nx_bcid = maxbcid++;
self(nx, nx_bcid, maxbcid, self);
}
};
int bcid = 0;
for(int i=0; i<n; i++) if(dfs_parent[i] < 0) dfs2(i, -1, bcid, dfs2);
edgeidx_to_bcidx = std::move(res);
mm = m;
mnum_bcs = bcid;
}
int get_num_bcts() const { return mnum_bcs; }
std::vector<std::vector<int>> get_bcs() const {
std::vector<std::vector<int>> res(mnum_bcs);
for(int i=0; i<mm; i++){
res[edgeidx_to_bcidx[i]].push_back(i);
}
return res;
}
AdjacencyList get_bct() const {
int bct_n = mn + mnum_bcs;
AdjacencyList bc_edgelists; {
std::vector<int> buf(mnum_bcs+1);
for(int bci : edgeidx_to_bcidx) ++buf[bci];
for(int i=1; i<=mnum_bcs; i++) buf[i] += buf[i-1];
std::vector<int> E(buf.back());
for(int i=0; i<mm; i++) E[--buf[edgeidx_to_bcidx[i]]] = i;
bc_edgelists = AdjacencyList::from_raw(std::move(E), std::move(buf));
}
std::vector<std::pair<int, int>> res(bct_n - 1);
int resi = 0;
std::vector<int> visited(mn);
for(int bci=0; bci<mnum_bcs; bci++){
for(int e : bc_edgelists[bci]){
auto [u,v] = medges[e];
if(!visited[u]){ visited[u] = 1; res[resi++] = {mn+bci,u}; }
if(!visited[v]){ visited[v] = 1; res[resi++] = {mn+bci,v}; }
}
for(int e : bc_edgelists[bci]){
auto [u,v] = medges[e];
visited[u] = visited[v] = 0;
}
}
return AdjacencyList(bct_n, res, true);
}
};
} // namespace nachia
#include <algorithm>
namespace nachia{
struct HeavyLightDecomposition{
private:
int N;
std::vector<int> P;
std::vector<int> PP;
std::vector<int> PD;
std::vector<int> D;
public:
HeavyLightDecomposition(const AdjacencyList& E = AdjacencyList(1, {}, false)){
N = E.num_vertices();
P.assign(N, -1);
std::vector<int> I = {0};
I.reserve(N);
for(int i=0; i<(int)I.size(); i++){
int p = I[i];
for(int e : E[p]) if(P[p] != e){
I.push_back(e);
P[e] = p;
}
}
std::vector<int> Z(N, 1);
std::vector<int> nx(N, -1);
PP.resize(N);
for(int i=0; i<N; i++) PP[i] = i;
for(int i=N-1; i>=1; i--){
int p = I[i];
Z[P[p]] += Z[p];
if(nx[P[p]] == -1) nx[P[p]] = p;
if(Z[nx[P[p]]] < Z[p]) nx[P[p]] = p;
}
for(int p : I) if(nx[p] != -1) PP[nx[p]] = p;
PD.assign(N,N);
PD[0] = 0;
D.assign(N,0);
for(int p : I) if(p != 0){
PP[p] = PP[PP[p]];
PD[p] = std::min(PD[PP[p]], PD[P[p]]+1);
D[p] = D[P[p]]+1;
}
}
int depth(int p) const {
return D[p];
}
int lca(int u, int v) const {
if(PD[u] < PD[v]) std::swap(u, v);
while(PD[u] > PD[v]) u = P[PP[u]];
while(PP[u] != PP[v]){ u = P[PP[u]]; v = P[PP[v]]; }
return (D[u] > D[v]) ? v : u;
}
int dist(int u, int v) const {
return depth(u) + depth(v) - depth(lca(u,v)) * 2;
}
int median(int x, int y, int z) const {
return lca(x,y) ^ lca(y,z) ^ lca(x,z);
}
};
} // namespace nachia
#include <iostream>
int main() {
using namespace std;
int n; cin >> n;
int m; cin >> m;
std::vector<std::pair<int, int>> edges(m);
for(auto& [u,v] : edges){ cin >> u >> v; u--; v--; }
auto hld_bct = nachia::HeavyLightDecomposition(nachia::BiconnectedComponents(n, edges).get_bct());
int q; cin >> q;
for(int queryi=0; queryi<q; queryi++){
int u,v; cin >> u >> v; u--; v--;
int d = hld_bct.dist(u,v);
int ans = std::max(0, d/2-1);
cout << ans << "\n";
}
return 0;
}
struct ios_do_not_sync{
ios_do_not_sync(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
}
} ios_do_not_sync_instance;
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0