Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit babc52e

Browse files
authored
Merge pull request #272 from changicho/master
Add : count_triangle cpp
2 parents 1045393 + cd3fa73 commit babc52e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

graphs/count_triangle.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
long long answer = 0;
6+
int N, M;
7+
bool map[50][50] = {
8+
0,
9+
};
10+
11+
void dfs(int start, int current, int before, int depth) {
12+
if (depth == 2) {
13+
if (current == start) {
14+
answer++;
15+
}
16+
return;
17+
}
18+
19+
int iteration_start, iteration_end;
20+
if (depth != 1) {
21+
iteration_start = current + 1;
22+
iteration_end = N;
23+
} else {
24+
iteration_start = 0;
25+
iteration_end = current;
26+
}
27+
28+
for (int i = iteration_start; i < iteration_end; i++) {
29+
if (map[current][i] && i != before) {
30+
dfs(start, i, current, depth + 1);
31+
}
32+
}
33+
}
34+
35+
void solution() {
36+
answer = 0;
37+
cin >> N >> M;
38+
39+
for (int i = 0; i < N; i++) {
40+
for (int j = 0; j < N; j++) {
41+
map[i][j] = 0;
42+
}
43+
}
44+
45+
int x, y;
46+
for (int i = 0; i < M; i++) {
47+
cin >> x >> y;
48+
x--;
49+
y--;
50+
map[x][y] = 1;
51+
map[y][x] = 1;
52+
}
53+
54+
for (int i = 0; i < N - 1; i++) {
55+
for (int j = i + 1; j < N; j++) {
56+
if (map[i][j]) {
57+
dfs(i, j, i, 0);
58+
}
59+
}
60+
}
61+
62+
cout << "number of triangle : " << answer;
63+
}
64+
65+
int main() {
66+
ios_base ::sync_with_stdio(false);
67+
cin.tie(NULL);
68+
cout.tie(NULL);
69+
70+
solution();
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)