-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind and cycle (1).cpp
More file actions
84 lines (64 loc) · 1.5 KB
/
find and cycle (1).cpp
File metadata and controls
84 lines (64 loc) · 1.5 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
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
struct group{
int u,v;
};
int find_set(int parent[],int x)
{
if(parent[x]==-1)
return x;
return find_set(parent,parent[x]);
}
void union_set(int parent[],int u,int v)
{
parent[v]=u;
}
int main()
{
int nn,f;
cout<<"Enter size of the tree"<<endl;
cin>>nn;
int tree[nn],par[nn];
for(int i=0;i<nn;i++)
par[i]=-1;
cout<<"Enter nodes on tree"<<endl;
for(int i=0;i<nn;i++)
cin>>tree[i];
for(int i=0;i<nn;i++)
{
if(i!=0)
par[tree[i]]=tree[0];
}
cout<<"Enter node to find its parent"<<endl;
cin>>f;
cout<<"Parent of "<<f<<" is :"<<find_set(par,f)<<endl<<endl<<endl;
cout<<"Detecting cycle in graph............"<<endl<<endl<<endl;
int edges;
int nodes;
int u,v;
cout<<"Enter number of edges and nodes"<<endl;
cin>>edges>>nodes;
int parent[nodes+1];
for(int i=0;i<=nodes;i++)
parent[i]=-1;
group p[edges];
cout<<"Enter u , v"<<endl;
for(int i=0;i<edges;i++)
{
cin>>p[i].u>>p[i].v;
}
int m,n;
for(int i=0;i<edges;i++)
{
m=p[i].u;
n=p[i].v;
if(find_set(parent,m)!=find_set(parent,n))
{
union_set(parent,m,n);
}else{
cout<<"cycle exist in graph....."<<endl;
return 0;
}
}
cout<<"cycle do not exist......."<<endl;
}