# CPP Program Dominos – Advanced Graphs

• Dominos are lots of fun. Children like to stand the tiles on their sides 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`

## Code:

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
using namespace std;

stack<int> st;
bool visited[100010];

void dfs2(int index){
visited[index]=true;
}
}
}

void dfs(int index){
visited[index]=true;
}
}
st.push(index);
}

int main(){
int tc;
scanf("%d",&tc);
while (tc--){
memset(visited,false,sizeof(visited));

int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++){
int a,b;
scanf("%d %d",&a,&b);
}

for(int i=1;i<=n;i++){
if(!visited[i]){
dfs(i);
}
}
memset(visited,false,sizeof(visited));
int count=0;
while(!st.empty()){
int index=st.top();
st.pop();
if(!visited[index]){
count++;
dfs2(index);
}
}
printf("%d\n",count);
for(int i=1;i<=n;i++){