-
Notifications
You must be signed in to change notification settings - Fork 0
/
3cycle.cpp
86 lines (68 loc) · 1.78 KB
/
3cycle.cpp
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
74
75
76
77
/*
3 Cycle
Send Feedback
Given a graph with N vertices (numbered from 0 to N-1) and M undirected edges, then count the distinct 3-cycles in the graph. A 3-cycle PQR is a cycle in which (P,Q), (Q,R) and (R,P) are connected by an edge.
Input Format :
The first line of input contains two space separated integers, that denotes the value of N and M.
Each of the following M lines contain two integers, that denote the vertices which have an undirected edge between them. Let us denote the two vertices with the symbol u and v.
Output Format :
Print the count the number of 3-cycles in the given graph
Constraints :
0 <= N <= 100
0 <= M <= (N*(N-1))/2
0 <= u <= N - 1
0 <= v <= N - 1
Time Limit: 1 sec
Sample Input 1:
3 3
0 1
1 2
2 0
Sample Output 1:
1
*/
#include<bits/stdc++.h>
using namespace std;
// DFS Technique
// Depth First Search
// Taking the input 0 to n-1 and the edges
#include<bits/stdc++.h>
using namespace std;
void three_cycle(int** edges, int n) {
int ans = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++){
if(edges[i][j]){
for(int k=0;k<n;k++){
if(edges[j][k]&&edges[k][i]){
ans+=1;
}
}
}
}
}
cout<<ans/6;
}
int main() {
int n; // number of vertices
int e; // number of edges;
cin>>n>>e;
int** edges = new int*[n];
for(int i=0;i<n;i++){
edges[i] = new int[n];
for(int j=0;j<n;j++)
edges[i][j] = 0;
}
for(int i=0;i<e;i++){
int f, s;
cin>>f>>s;
edges[f][s] = 1;
edges[s][f] = 1;
}
three_cycle(edges, n);
for(int i=0;i<n;i++){
delete[] edges[i];
}
delete[] edges;
return 0;
}