From 552f6e3464c62b3c8624b48b051a63a06f1ca942 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 8 Jul 2023 21:58:29 +0800 Subject: [PATCH] D - Add One Edge https://atcoder.jp/contests/abc309/submissions/43392527 --- AtCoder/ABC309/D/D.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 AtCoder/ABC309/D/D.cpp diff --git a/AtCoder/ABC309/D/D.cpp b/AtCoder/ABC309/D/D.cpp new file mode 100644 index 00000000..3d8baa82 --- /dev/null +++ b/AtCoder/ABC309/D/D.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 3e5 + 5; + +int n1, n2, m, dist1[N], dist2[N]; +bool vis[N]; +std::vector g[N]; + +void bfs(int s, int* dist) { + std::queue q; + + q.emplace(s); + vis[s] = true; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (int v : g[u]) { + if (vis[v]) continue; + + vis[v] = true; + + dist[v] = dist[u] + 1; + q.emplace(v); + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n1 >> n2 >> m; + + for (int i = 1, u, v; i <= m; i++) { + cin >> u >> v; + + g[u].emplace_back(v); + g[v].emplace_back(u); + } + + bfs(1, dist1); + bfs(n1 + n2, dist2); + + cout << *std::max_element(dist1 + 1, dist1 + 1 + n1) + + *std::max_element(dist2 + n1 + 1, dist2 + n1 + n2 + 1) + 1 + << endl; + + return 0; +}