-
Notifications
You must be signed in to change notification settings - Fork 0
/
4803.cpp
83 lines (75 loc) · 1.46 KB
/
4803.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<vector>
using namespace std;
const string answer[4] = {"No trees.", "There is one tree.", "A forest of ", " trees."};
const int nodesize = 5e2 + 1;
int n,m;
vector<int> graph[nodesize];
bool visited[nodesize];
bool flag = false;
void input() {
int u,v;
while(m--) {
cin>>u>>v;
graph[u].push_back(v);
graph[v].push_back(u);
}
}
void traverse(int parent, int start) {
visited[start] = true;
for(auto g:graph[start]) {
if(visited[g] && parent!=g) {
flag = true;
continue;
}
if(visited[g]) {
continue;
}
traverse(start, g);
}
}
void solution() {
int count = 0;
for(int i=1; i<=n; i++) {
if(visited[i]) {
continue;
}
flag = false;
traverse(-1,i);
if(!flag) {
++count;
}
}
if(count>1) {
cout<<answer[2]<<count<<answer[3];
} else {
cout<<answer[count];
}
}
void init() {
for(int i=1; i<=n; i++) {
graph[i].clear();
}
fill(visited,visited+nodesize,false);
}
void solve() {
int tc = 1;
while(1) {
cin>>n>>m;
if(n==0 && m==0) {
break;
}
cout<<"Case "<<tc++<<": ";
init();
input();
solution();
cout<<endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}