与えられたグラフのブリッジエッジの数を見つけるためのC++プログラム
n個の頂点とm個のエッジを含む重み付けされていない無向グラフが与えられたとします。グラフのブリッジエッジは、グラフを削除するとグラフが切断されるエッジです。与えられたグラフでそのようなグラフの数を見つける必要があります。グラフには、平行なエッジや自己ループは含まれていません。
したがって、入力がn =5、m =6、edges ={{1、2}、{1、3}、{2、3}、{2、4}、{2、5}、{3 、5}}の場合、出力は1になります。
グラフには、{2、4}のブリッジエッジが1つだけ含まれています。
これを解決するには、次の手順に従います-
mSize := 100
Define an array G of size mSize
Define one 2D array bridge
Define an array visitedof size mSize
Define an array vk and l of size mSize
Define an array edges containing integer pairs
Define a function depthSearch(), this will take v, p initialize it with -1,
visited[v] := 1
vk[v] := (increase l[v] = t by 1)
for each x in G[v], do:
if x is same as p, then:
Ignore following part, skip to the next iteration
if visited[x] is non-zero, then:
l[v] := minimum of l[v] and vk[x]
Otherwise
depthSearch(x, v)
l[v] := minimum of l[v] and l[x]
if l[x] > vk[v], then:
bridge[v, x] := 1
Define a function bridgeSearch()
t := 0
for initialize i := 1, when i <= n, update (increase i by 1), do:
if not visited[i] is non-zero, then:
depthSearch(i)
for initialize i := 0, when i < m, update (increase i by 1), do:
a := first value of edges[i]
b := second value of edges[i]
insert b at the end of G[a]
insert a at the end of G[b]
bridgeSearch()
ans := 0
for initialize i := 1, when i <= n, update (increase i by 1), do:
for initialize j := 1, when j >= n, update (increase j by 1), do:
if i is not equal to j and bridge[i, j], then:
(increase ans by 1)
return ans 例
理解を深めるために、次の実装を見てみましょう-
#include <bits/stdc++.h>
using namespace std;
const int mSize = 100;
vector <int> G[mSize];
int n, m, t;
vector<vector<int>> bridge(mSize, vector<int>(mSize));
vector <int> visited(mSize);
vector <int> vk(mSize, -1), l(mSize, -1);
vector<pair<int, int>> edges;
void depthSearch(int v, int p = -1) {
visited[v] = 1;
vk[v] = l[v] = t++;
for (auto x: G[v]) {
if (x == p) {
continue;
}
if (visited[x]) {
l[v] = min(l[v], vk[x]);
} else {
depthSearch(x, v);
l[v] = min(l[v], l[x]);
if (l[x] > vk[v]) {
bridge[v][x] = 1;
}
}
}
}
void bridgeSearch() {
t = 0;
for (int i = 1; i <= n; ++i) {
if (!visited[i]) {
depthSearch(i);
}
}
}
int solve(){
for (int i = 0; i < m; ++i) {
int a, b; a = edges[i].first;
b = edges[i].second;
G[a].push_back(b);
G[b].push_back(a);
}
bridgeSearch();
int ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i != j and bridge[i][j]) ans++;
}
}
return ans;
}
int main() {
n = 5, m = 6;
edges = {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {2, 5}, {3, 5}};
cout<< solve();
return 0;
} 入力
5, 6, {{1, 2}, {1, 3}, {2, 3}, {2, 4}, {2, 5}, {3, 5}}
出力
1
-
グラフ内のスーパー頂点を見つけるためのC++プログラム
n個の頂点を持つグラフが与えられたとします。頂点には1からnの番号が付けられ、配列edgesで指定されたエッジによって接続されます。各頂点には、配列valuesで指定された1からnまでの数値内のx値があります。ここで、グラフからスーパー頂点を見つける必要があります。頂点1からiへの最短経路にi番目の頂点と同じ「x」値を持つ頂点がない場合、頂点iは「スーパー頂点」と呼ばれます。この基準を満たすすべての頂点を印刷します。 したがって、入力がn =5のようである場合、値={1、2、2、1、3}、エッジ={{1、2}、{2、3}、{2、3}、{2、4 }、{4、5}}の場合、出力は1 345に
-
グリッド内の照らされたセルの数を見つけるためのC++プログラム
次元h*wのグリッドが与えられていると仮定します。グリッド内のセルには、球根または障害物のいずれかを含めることができます。電球のセルはそれ自体とその右、左、上、下のセルを照らし、障害物のセルが光を遮らない限り、光はセルを通して輝くことができます。障害物セルは照明できず、電球セルからの光が他のセルに到達するのを防ぎます。したがって、配列「bulb」内のグリッド内の電球セルの位置と配列「obstacles」内の障害物セルの位置を考えると、照らされているグリッド内のセルの総数を見つける必要があります。 したがって、入力がh =4、w =4、bulb ={{1、1}、{2、2}、{3、3}}、障害物