forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_triangle.cpp
More file actions
73 lines (60 loc) · 1.29 KB
/
Copy pathcount_triangle.cpp
File metadata and controls
73 lines (60 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;
long long answer = 0;
int N, M;
bool map[50][50] = {
0,
};
void dfs(int start, int current, int before, int depth) {
if (depth == 2) {
if (current == start) {
answer++;
}
return;
}
int iteration_start, iteration_end;
if (depth != 1) {
iteration_start = current + 1;
iteration_end = N;
} else {
iteration_start = 0;
iteration_end = current;
}
for (int i = iteration_start; i < iteration_end; i++) {
if (map[current][i] && i != before) {
dfs(start, i, current, depth + 1);
}
}
}
void solution() {
answer = 0;
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
map[i][j] = 0;
}
}
int x, y;
for (int i = 0; i < M; i++) {
cin >> x >> y;
x--;
y--;
map[x][y] = 1;
map[y][x] = 1;
}
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
if (map[i][j]) {
dfs(i, j, i, 0);
}
}
}
cout << "number of triangle : " << answer;
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solution();
return 0;
}