-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCC-FIRESC.cpp
More file actions
127 lines (110 loc) · 2.58 KB
/
CC-FIRESC.cpp
File metadata and controls
127 lines (110 loc) · 2.58 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
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
/*
This code is written by Shammi Anand
contact : shammianand101@gmail.com, shammianand.me
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define vi vector<int>
#define pii pair<int, int>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define F(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(), a.end()
// #define INF 1e9+7
#define nl "\n"
#define w(x) int x; cin>>x; while(x--)
template<typename T_vector>
void output(const T_vector &v, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = int(v.size());
for (int i = start; i < end; i++) {
cout << v[i] << (i < end - 1 ? ' ' : '\n');
}
}
const int INF = 1e9 + 7;
int choose;
void shammi() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
class Graph {
int V;
list<int> *adj;
// A function used by DFS
void DFSUtil(int v, bool visited[]);
public:
Graph(int V); // Constructor
~Graph();
void addEdge(int v, int w);
pii connectedComponents();
};
pii Graph::connectedComponents() {
int count = 0;
int ans = 1;
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int v = 0; v < V; v++)
visited[v] = false;
for (int v = 0; v < V; v++) {
if (visited[v] == false) {
// print all reachable vertices
// from v
choose = 0;
DFSUtil(v, visited);
ans = ((ans % INF) * (choose % INF)) % INF;
// cout << "\n";
count++;
}
}
// delete [] visited;
delete[] visited;
return {count, ans};
}
void Graph::DFSUtil(int v, bool visited[]) {
// Mark the current node as visited and print it
visited[v] = true;
// cout << v << " ";
choose++;
// Recur for all the vertices
// adjacent to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
Graph::~Graph() {
// cout << "Done\n";
delete[] adj;
}
// method to add an undirected edge
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
adj[w].push_back(v);
}
// int ans = 1;
int main() {
shammi();
w(t) {
// choose = 0;
int n, m; cin >> n >> m;
Graph g(n);
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
g.addEdge(u - 1, v - 1);
}
pii ans = g.connectedComponents();
cout << (ans.f % INF) << " " << (ans.s % INF) << nl;
// ~Graph();
}
return 0;
}