-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtea.cpp
More file actions
69 lines (68 loc) · 1.43 KB
/
tea.cpp
File metadata and controls
69 lines (68 loc) · 1.43 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
void dfs(ll temp2,vector<ll> adl[],bool vis[],string col[],ll parent){
vis[temp2]=true;
col[temp2]="grey";
cout << temp2 << " ";
vector<ll>::iterator it;
for(it=adl[temp2].begin();it!=adl[temp2].end();it++){
if(col[*it]=="white"){
dfs(*it,adl,vis,col,temp2);
}
else if(col[*it]=="grey"&&*it!=parent){
cout << "cycle exists" << endl;
exit();
}
}
}
int main(){
ll n,temp1,temp2,no;
cout << "Enter number of vertices: " << endl;
cin >> n;
vector<ll> adl[n+1];
cout <<"Enter no of edges: "<<endl;
cin >> no;
cout << "Now enter edges: "<< endl;
for(ll i=0;i<no;i++){
cin >> temp1>>temp2;
adl[temp1].pb(temp2);
adl[temp2].pb(temp1);
}
vector<ll>::iterator it;
for(ll i=1;i<=n;i++){
cout << i<<" - ";
for(it=adl[i].begin();it!=adl[i].end();it++){
cout << *it <<" - ";
}
cout<< endl;
}
string col[n+1];
for(ll a=0;a<n+1;a++)
col[a]="white";
bool vis[n];
memset(vis,false,sizeof(vis[0])*n);
cout<<"Enter source for dfs: "<<endl;
cin >> temp2;
dfs(temp2,adl,vis,col,-1);
/*
cout <<"Enter source for bfs: "<<endl;
cin >> temp1;
list<ll> que;
que.pb(temp1);
vis[temp1]=true;
while(!que.empty()){
temp1=que.front();
que.pop_front();
cout << temp1<<" ";
for(it=adl[temp1].begin();it!=adl[temp1].end();it++){
if(!vis[*it]){
vis[*it]=true;
que.pb(*it);
}
}
} */
return 0;
}