Skip to content

Commit 441daa6

Browse files
committed
Added more stuff
1 parent 77f6dc2 commit 441daa6

File tree

4 files changed

+131
-12
lines changed

4 files changed

+131
-12
lines changed

Advanced Graphs/Bottom.cpp

Whitespace-only changes.

Advanced Graphs/Dominos.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
/*
8+
Dominos are lots of fun. Children like to stand the tiles on their side in long lines. When one domino falls, it knocks down the next one, which knocks down the one after that, all the way down the line.
9+
However, sometimes a domino fails to knock the next one down. In that case, we have to knock it down by hand to get the dominos falling again.
10+
Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall.
11+
Input
12+
The first line of input contains one integer specifying the number of test cases to follow.Each test case begins with a line containing two integers,each no larger than 100 000. The first integer n is the number of domino tiles and the second integer m is the number of lines to follow in the test case. The domino tiles are numbered from 1 to n.
13+
Each of the following lines contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well.
14+
Output
15+
For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall.
16+
Sample Input
17+
1
18+
3 2
19+
1 2
20+
2 3
21+
Sample Output
22+
1
23+
*/
24+
25+
#include <bits/stdc++.h>
26+
27+
using namespace std;
28+
29+
void dfs1(vector<int>* graph, int start, bool* visited, stack<int> &s){
30+
visited[start] = 1;
31+
32+
for (int i = 0; i < graph[start].size(); ++i)
33+
{
34+
if (visited[graph[start].at(i)] == 0)
35+
{
36+
dfs1(graph, graph[start].at(i), visited, s);
37+
}
38+
}
39+
40+
//Will put in stack in the last
41+
s.push(start);
42+
43+
return;
44+
}
45+
46+
void dfs2(vector<int>* graph, int start, bool* visited){
47+
visited[start] = 1;
48+
for (int i = 0; i < graph[start].size(); ++i)
49+
{
50+
int v = graph[start].at(i);
51+
if (visited[graph[start].at(i)] == 0)
52+
{
53+
dfs2(graph, v, visited);
54+
}
55+
}
56+
57+
return;
58+
}
59+
60+
void kosaraju(vector<int>* graph, vector<int>* grapht, int n){
61+
bool* visited = new bool[n];
62+
for (int i = 0; i < n; ++i)
63+
{
64+
visited[i] = 0;
65+
}
66+
stack<int> s;
67+
68+
for (int i = 0; i < n; ++i)
69+
{
70+
if (visited[i]==0)
71+
{
72+
dfs1(graph, i, visited, s);
73+
}
74+
}
75+
76+
//Step 2
77+
int count = 0;
78+
for (int i = 0; i < n; ++i)
79+
{
80+
visited[i] = 0;
81+
}
82+
83+
while(!s.empty())
84+
{
85+
int v = s.top();
86+
s.pop();
87+
if (visited[v] == 0)
88+
{
89+
count++;
90+
dfs2(graph, v, visited);
91+
}
92+
}
93+
94+
cout << count << '\n';
95+
96+
97+
}
98+
99+
int main( int argc , char ** argv )
100+
{
101+
ios_base::sync_with_stdio(false) ;
102+
cin.tie(NULL) ;
103+
104+
int t;
105+
cin>>t;
106+
while(t--){
107+
int n, m;
108+
cin>>n>>m;
109+
110+
vector<int>* graph = new vector<int>[n];
111+
vector<int>* grapht = new vector<int>[n];
112+
for (int i = 0; i < m; ++i)
113+
{
114+
int u, v;
115+
cin>>u>>v;
116+
graph[u-1].push_back(v-1);
117+
grapht[v-1].push_back(u-1);
118+
}
119+
120+
kosaraju(graph, grapht, n);
121+
122+
}
123+
124+
125+
return 0 ;
126+
127+
128+
129+
}

Advanced Graphs/Test.txt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
1
2-
2 7 12
2+
3 2
33
1 2
4-
2 2
5-
2 6
6-
1 7
7-
1 1
8-
2 3
9-
1 3
10-
1 4
11-
2 1
12-
2 5
13-
2 4
14-
1 6
4+
2 3

Advanced Graphs/a.out

8.29 KB
Binary file not shown.

0 commit comments

Comments
 (0)