Sicily 8543. Trees

8543. Trees

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

A graph consists of a set of vertices and edges between pairs of vertices. Two vertices are connected if there is a path (subset of edges) leading from one vertex to another, and a connected component is a maximal subset of vertices that are all connected to each other. A graph consists of one or more connected components.

A tree is a connected component without cycles, but it can also be characterized in other ways. For example, a tree consisting of n vertices has exactly n-1 edges. Also, there is a unique path connecting any pair of vertices in a tree.

Given a graph, report the number of connected components that are also trees.

Input

The input consists of a number of cases. Each case starts with two non-negative integers n and m, satisfying n ≤ 500 and m ≤ n(n-1)/2. This is followed by m lines, each containing two integers specifying the two distinct vertices connected by an edge. No edge will be specified twice (or given again in a different order). The vertices are labelled 1 to n. The end of input is indicated by a line containing n = m = 0.

Output

For each case, print one of the following lines depending on how many different connected components are trees (T > 1 below):

	Case x: A forest of T trees.
	Case x: There is one tree.
	Case x: No trees.
      

x is the case number (starting from 1).

Sample Input

6 3
1 2
2 3
3 4
6 5
1 2
2 3
3 4
4 5
5 6
6 6
1 2
2 3
1 3
4 5
5 6
6 4
0 0

Sample Output

Case 1: A forest of 3 trees.
Case 2: There is one tree.
Case 3: No trees.

Problem Source

Rocky

// Problem#: 8543
// Submission#: 3379576
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <string.h>
#include <math.h>
using namespace std;

const int MAX_N = 505;

int N, M;
bool vis[MAX_N];
vector<int> E[MAX_N];
vector<int> P;
int e, p;

void dfs(int now) {
    int s = E[now].size();
    P.push_back(now);
    for (int i = 0; i < s; i++) {
        if (!vis[E[now][i]]) {
            p++;
            vis[E[now][i]] = true;
            dfs(E[now][i]);
        }
    }
}

void calE() {
    bool visFrom[MAX_N];
    int s = P.size();
    for (int i = 0; i < s; i++) visFrom[P[i]] = false;
    for (int i = 0; i < s; i++) {
        visFrom[P[i]] = true;
        int ss = E[P[i]].size();
        for (int j = 0; j < ss; j++) {
            if (!visFrom[E[P[i]][j]]) e++;
        }
    }
}

int main() {

    std::ios::sync_with_stdio(false);

    int counter = 1;

    while (1) {
        cin >> N >> M;

        if (!N && !M) break;

        for (int i = 1; i <= N; i++) {
            vis[i] = false;
            E[i].clear();
        }

        for (int i = 0; i < M; i++) {
            int f, t;
            cin >> f >> t;
            E[f].push_back(t);
            E[t].push_back(f);
        }

        int ans = 0;

        for (int i = 1; i <= N; i++) {
            if (!vis[i]) {
                P.clear();
                e = 0;
                p = 1;
                vis[i] = true;
                dfs(i);
                calE();
                if (p == e + 1) ans++;
            }
        }

        if (ans == 0) cout << "Case " << counter++ << ": No trees." << endl;
        if (ans == 1) cout << "Case " << counter++ << ": There is one tree." << endl;
        if (ans > 1) cout << "Case " << counter++ << ": A forest of " << ans << " trees." << endl;

    }

    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值