Skip to content

Commit 1540a37

Browse files
committed
added cycle detection
1 parent 0e45691 commit 1540a37

7 files changed

+131
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
#define lli long long int
3+
#define endl "\n"
4+
#define MAX 100005
5+
using namespace std;
6+
7+
vector <int> v1[MAX];
8+
bool visisted[MAX];
9+
bool cycle = false;
10+
11+
bool DFS(int i, int parent)
12+
{
13+
if(visisted[i] == false)
14+
{
15+
visisted[i] = true;
16+
for(int j = 0; j<v1[i].size(); j++)
17+
if(v1[i][j] != parent)
18+
DFS(v1[i][j], i);
19+
}
20+
else
21+
{
22+
cycle = true;
23+
}
24+
}
25+
26+
int main()
27+
{
28+
int v, e, x, y;
29+
cout<<"Enter the number of vertex: ";
30+
cin>>v;
31+
cout<<"Enter the number of edges: ";
32+
cin>>e;
33+
34+
for(int i = 0; i<e; i++)
35+
{
36+
cout<<"Enter both the vertex of the edge "<<i+1<<": ";
37+
cin>>x>>y;
38+
v1[x].push_back(y);
39+
v1[y].push_back(x);
40+
}
41+
42+
DFS(1, 0);
43+
44+
if(cycle)
45+
cout<<"cycle exists in graph";
46+
else
47+
cout<<"cycle does not exists in graph";
48+
49+
}
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <bits/stdc++.h>
2+
#define lli long long int
3+
#define endl "\n"
4+
#define MAX 2005
5+
using namespace std;
6+
7+
8+
9+
int main()
10+
{
11+
int n , k,h;
12+
cin>>n>>k;
13+
lli wid = 0;
14+
while(n--)
15+
{
16+
cin>>h;
17+
wid += (h > k) ? 2 : 1;
18+
}
19+
cout<<wid;
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
#define lli long long int
3+
#define endl "\n"
4+
#define MAX 2005
5+
using namespace std;
6+
7+
8+
9+
int main()
10+
{
11+
int n,A = 0,D= 0;
12+
string s;
13+
cin>>n>>s;
14+
for(auto c: s)
15+
if(c == 'A') A++;
16+
else D++;
17+
if(A > D)
18+
cout<<"Anton";
19+
else if(D > A)
20+
cout<<"Danik";
21+
else
22+
cout<<"Friendship";
23+
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
#define lli long long int
3+
#define endl "\n"
4+
#define MAX 2005
5+
using namespace std;
6+
7+
8+
9+
int main()
10+
{
11+
int a, b;
12+
cin>>a>>b;
13+
int yrs = 0;
14+
while(a <= b)
15+
a = a*3, b = b*2, yrs++;
16+
cout<<yrs;
17+
18+
}

training-sheet/A/004. Team.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <bits/stdc++.h>
2+
#define lli long long int
3+
#define endl "\n"
4+
#define MAX 2005
5+
using namespace std;
6+
7+
8+
9+
int main()
10+
{
11+
int n,x,y,z, count = 0;
12+
cin>>n;
13+
while(n--)
14+
{
15+
cin>>x>>y>>z;
16+
if((x+y+z) > 1) count++;
17+
}
18+
cout<<count;
19+
20+
}

0 commit comments

Comments
 (0)