Sicily 8543. Trees
来源:互联网 发布:华为matebook x知乎 编辑:程序博客网 时间:2024/06/04 20:25
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 31 22 33 46 51 22 33 44 55 66 61 22 31 34 55 66 40 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;}
- Sicily 8543. Trees
- sicily Level Traversal of Bianry Trees
- trees
- Sicily 1027
- sicily 1007
- sicily 1795
- sicily 1036
- sicily 1419
- sicily 1889
- sicily 1684
- sicily 1686
- sicily 1004
- sicily 1135
- sicily 1152
- sicily 1151
- sicily 1002
- sicily 1156
- sicily 1424
- ORACLE函数大全
- 映射静态资源
- SAXParseException: cvc-id.1: There is no ID/IDREF binding for IDREF
- Java中字符输入输出流与字节输入输出流的区别
- xml和json互转的格式,结构
- Sicily 8543. Trees
- 黑马程序员---类加载
- Sicily 7996. 购买电脑
- VS2010下使用QT环境配置
- asp.net MVC 学习(第一章) 认识MVC 以及 MVC 编码流程
- Objective-C语法之异常处理
- Linux下用iptables上网
- Sicily 11598. XOR
- 看cuda初级教程视频笔记(周斌讲的)--CUDA、GPU编程模型