-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path43.cpp
More file actions
65 lines (54 loc) · 1.25 KB
/
43.cpp
File metadata and controls
65 lines (54 loc) · 1.25 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
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <set>
#include <stack>
#include <numeric>
#include <regex>
#include <queue>
#include <iomanip>
using namespace std;
#define FILE_IO {freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);}
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL)
#define id first
#define vl second
void dfs(int u, vector<vector<int>> &G, vector<bool> &visited) {
visited[u] = true;
for (int v : G[u]) {
if (!visited[v]) {
dfs(v, G, visited);
}
}
}
int main() {
FAST_IO;
// FILE_IO;
vector<vector<int>> G(100010);
vector<bool> vst(100010);
int T, N, E, u, v;
cin >> T;
while (T--) {
cin >> N >> E;
for (int i = 0; i < N; ++i){
G[i].clear();
vst[i] = false;
}
for (int i = 0; i < E; ++i) {
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
int cnt = 0;
for (int i = 0; i < N; ++i) {
if (!vst[i]) {
dfs(i, G, vst);
++cnt;
}
}
cout << cnt << endl;
}
return 0;
}