-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathDominos.cpp
129 lines (102 loc) · 2.74 KB
/
Dominos.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
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.
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.
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.
Input
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.
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.
Output
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.
Sample Input
1
3 2
1 2
2 3
Sample Output
1
*/
#include <bits/stdc++.h>
using namespace std;
void dfs1(vector<int>* graph, int start, bool* visited, stack<int> &s){
visited[start] = 1;
for (int i = 0; i < graph[start].size(); ++i)
{
if (visited[graph[start].at(i)] == 0)
{
dfs1(graph, graph[start].at(i), visited, s);
}
}
//Will put in stack in the last
s.push(start);
return;
}
void dfs2(vector<int>* graph, int start, bool* visited){
visited[start] = 1;
for (int i = 0; i < graph[start].size(); ++i)
{
int v = graph[start].at(i);
if (visited[graph[start].at(i)] == 0)
{
dfs2(graph, v, visited);
}
}
return;
}
void kosaraju(vector<int>* graph, vector<int>* grapht, int n){
bool* visited = new bool[n];
for (int i = 0; i < n; ++i)
{
visited[i] = 0;
}
stack<int> s;
for (int i = 0; i < n; ++i)
{
if (visited[i]==0)
{
dfs1(graph, i, visited, s);
}
}
//Step 2
int count = 0;
for (int i = 0; i < n; ++i)
{
visited[i] = 0;
}
while(!s.empty())
{
int v = s.top();
s.pop();
if (visited[v] == 0)
{
count++;
dfs2(graph, v, visited);
}
}
cout << count << '\n';
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int t;
cin>>t;
while(t--){
int n, m;
cin>>n>>m;
vector<int>* graph = new vector<int>[n];
vector<int>* grapht = new vector<int>[n];
for (int i = 0; i < m; ++i)
{
int u, v;
cin>>u>>v;
graph[u-1].push_back(v-1);
grapht[v-1].push_back(u-1);
}
kosaraju(graph, grapht, n);
}
return 0 ;
}