# 3 Cycle

Given a graph with N vertices (numbered from 1 to N) and Two Lists (U,V) of size M where (U\[i],V\[i]) and (V\[i],U\[i]) are connected by an edge , 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 an edge.

**Input Format :**

```
Line 1 : Two integers N and M
Line 2 : List u of size of M
Line 3 : List v of size of M
```

**Return Format :**

```
The count the number of 3-cycles in the given Graph
```

**Constraints :**

**1<=N<=100**

**1<=M<=(N\*(N-1))/2**

**1<=u\[i],v\[i]<=N**

**Sample Input:**

```
3 3
1 2 3
2 3 1
```

**Sample Output:**

```
1
```



```java
import java.util.Scanner;

public class solution {
    public static int[][] adj = new int[1005][1005];

    public int solve(int n, int m, int U[], int V[]) {
        for (int i = 0; i < m; i++) {
            adj[U[i]][V[i]] = 1;
            adj[V[i]][U[i]] = 1;
        }
        int cycles = 0;
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++)
                if (adj[a][b] == 1) {
                    int u = b;
                    for (int j = 1; j <= n; j++)
                        if (adj[u][j] == 1) {
                            int v = j;
                            if (v == a) // basically working as visisted array
                                continue;
                            if (adj[v][a] == 1) // if a,u,v form a cycle
                                cycles++;
                        }
                }
        }
        return cycles / 6; // A cycle is counted 6 times , (anticlockwise + clockwise )*(from A + from U + from V)
    }
}
```

