-
Notifications
You must be signed in to change notification settings - Fork 0
/
4803_트리.cpp
72 lines (62 loc) · 1.54 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
#include<bits/stdc++.h>
#define endl "\n"
#define MAX 502
#define INF 987654321
#define ll long long
#define int ll
using namespace std;
typedef pair<int, int> p;
int N, M;
vector<int> graph[MAX];
bool visited[MAX];
bool finished[MAX];
// Node
int cntNode(int now){
int cnt = 1;
visited[now] = true;
for(int next: graph[now])
if(!visited[next])
cnt += cntNode(next);
return cnt;
}
// Edge
int cntEdge(int now){
int cnt = graph[now].size();
finished[now] = true;
for(int next: graph[now])
if(!finished[next])
cnt += cntEdge(next);
return cnt;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 0;
while(1){
tc++;
memset(visited, false, sizeof(visited));
memset(finished, false, sizeof(finished));
cin >> N >> M;
if(N == 0 && M == 0) break;
for(int i = 1; i <= N; ++i)
graph[i].clear();
for(int i = 0; i < M; ++i){
int a, b; cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
int res = 0;
for(int i = 1; i <= N; ++i)
if(!visited[i])
if(cntEdge(i) / 2 == cntNode(i) - 1)
res++;
cout << "Case " << tc << ":";
if(res == 0)
cout << " No trees."<< endl;
else if(res == 1)
cout << " There is one tree." << endl;
else
cout << " A forest of "<< res << " trees." << endl;
}
return 0;
}