```
#define TREE 1
#define GRAPH 2
int find(int v, int *p)
{
while(v != p[v])
v = p[v];
return v;
}
void union(int i, int j, int *p)
{
if(i > j)
p[i] = j;
else
p[j] = i;
}
int Is_TREE_Or_GRAPH(int a[][] , int n)
{
int idx, p[n];
int u, u1, v, v1;
for (idx = 0; idx < n; idx++)
p[idx] = idx;
for (row = 0; row < n; row++)
{
for (col = 0; col < row; col++)
{
if(a[row][col] == 1)
{
u = row;
v = col;
u1 = find(u, p);
v1 = find(v, p);
if (u1 == v1)
return GRAPH;
union(u1, v1, p);
}
}
}
return TREE;
}
```