-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireEscapeDFS.cpp
More file actions
executable file
·62 lines (60 loc) · 1.71 KB
/
Copy pathFireEscapeDFS.cpp
File metadata and controls
executable file
·62 lines (60 loc) · 1.71 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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll visitedCount = 0;
void dfs(vector< bool > &visited, vector< vector< ll > > &children, ll index)
{
visited[index] = true;
visitedCount++;
vector< ll >::iterator iter = children[index].begin();
for(;iter != children[index].end();)
{
if(!visited[*iter])
{
dfs(visited, children, *iter);
iter++;
}
else
{
iter = children[index].erase(iter);
}
}
}
int main()
{
ll testcases;
cin >> testcases;
for(ll k = 0; k < testcases; k++)
{
ll numberOfTerms, numberOfQueries;
cin >> numberOfTerms >> numberOfQueries;
vector< bool > visited(numberOfTerms + 1, false);
vector< vector < ll > > children(numberOfTerms + 1);
for(ll i = 0; i < numberOfQueries; i++)
{
ll node1Index, node2Index;
cin >> node1Index >> node2Index;
children[node1Index].push_back(node2Index);
children[node2Index].push_back(node1Index);
}
vector< ll > solution;
ll numberOfRoutes = 0;
for(ll i = 1; i <= numberOfTerms; i++)
{
if(!visited[i])
{
numberOfRoutes++;
visitedCount = 0;
dfs(visited, children, i);
solution.push_back(visitedCount);
}
}
vector< ll >::iterator iter = solution.begin();
ll numberOfCaptains = 1;
for(; iter != solution.end(); iter++)
{
numberOfCaptains = (numberOfCaptains * (*iter)) % 1000000007;
}
cout << numberOfRoutes << " " << numberOfCaptains << endl;
}
}