From a5c848b17c5876c62634e87d6ac98134b9250095 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Wed, 15 Mar 2023 12:03:32 +0800 Subject: [PATCH] add: 230315-CN --- cpp/2303/230315-CN.cpp | 29 +++++++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230315-CN.cpp diff --git a/cpp/2303/230315-CN.cpp b/cpp/2303/230315-CN.cpp new file mode 100644 index 0000000..1638f5e --- /dev/null +++ b/cpp/2303/230315-CN.cpp @@ -0,0 +1,29 @@ +#include + +/** + * 1615. Maximal Network Rank + * + * There is an infrastructure of n cities with some number of roads connecting these cities. Each roads[i] = [ai, bi] indicates that there is a bidirectional road between cities ai and bi. + * The network rank of two different cities is defined as the total number of directly connected roads to either city. If a road is directly connected to both cities, it is only counted once. + * The maximal network rank of the infrastructure is the maximum network rank of all pairs of different cities. + * Given the integer n and the array roads, return the maximal network rank of the entire infrastructure. + */ + +class Solution { +public: + static int maximalNetworkRank(int n, const std::vector>& roads) { + bool G[105][105]{}; + int cnt[105]{}; + for (const auto& i : roads) { + G[i[0]][i[1]] = G[i[1]][i[0]] = true; + ++cnt[i[0]]; + ++cnt[i[1]]; + } + int ans = 0; + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + if (i != j) + ans = std::max(ans, cnt[i] + cnt[j] - G[i][j]); + return ans; + } +}; diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 86d660c..250d13c 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230315.cpp) +ADD_EXECUTABLE(2303 230315-CN.cpp)