/
A.cpp
65 lines (58 loc) · 1.02 KB
/
A.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
#include <algorithm>
#include <bitset>
#include <climits>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
void dfs(int cur, const vector<vector<int>> &graph, vector<bool> &visited)
{
visited[cur] = true;
if (graph[cur].empty())
{
return;
}
for (auto next : graph[cur])
{
if (visited[next])
continue;
dfs(next, graph, visited);
}
}
int slove(int n, vector<vector<int>> &graph)
{
int cnt = 0;
vector<bool> visited(n, false);
for (int i = 0; i < n; ++i)
{
if (!visited[i]){
dfs(i, graph, visited);
cnt++;
}
}
return 2 * (cnt - 1) + n - cnt;
}
int main()
{
auto t = 0;
cin >> t;
for (int i = 1; i <= t; ++i)
{
int n, m;
cin >> n >> m;
vector<vector<int>> graph(n);
for (int j = 0; j < m; j++)
{
int x1, x2;
cin >> x1 >> x2;
graph[x1 - 1].emplace_back(x2 - 1);
graph[x2 - 1].emplace_back(x1 - 1);
}
auto res = slove(n, graph);
cout << "Case #" << i << ": " << res << endl;
}
return 0;
}