-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.26
53 lines (49 loc) · 1.03 KB
/
11.26
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
#include <bits/stdc++.h>
using namespace std;
const int MXN = 510;
vector<int> adj[MXN];
bool vis[MXN], isTree;
int n, m, u, v, tc, trees;
void dfs(int cur, int prev) {
for (int nxt : adj[cur]) {
if (nxt == prev)
continue;
if (vis[nxt]) {
isTree = false;
continue;
}
vis[nxt] = true;
dfs(nxt, cur);
}
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
while (1) {
cin >> n >> m;
if (!n && !m) break;
fill(vis, vis + n + 1, 0);
for (int i = 1; i <= n; i++)
adj[i].clear();
trees = 0;
while (m--) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
vis[i] = true;
isTree = true;
dfs(i, -1);
trees += isTree;
}
cout << "Case " << ++tc << ": ";
if (!trees)
cout << "No trees." << '\n';
else if (trees == 1)
cout << "There is one tree." << '\n';
else
cout << "A forest of " << trees << " trees." << '\n';
}
}