Trees

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述
A graph consists of a set 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 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.
Give a graph, report the number of connected components that are also trees. 
输入
The input consist of a number of cases. Each case starts with two non-negative integer 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 vertices connected by an edge. The vertices are labeled from 1 to n. The end of input is indicated by a line containing n = m = 0.
输出
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).
  
样例输入
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
样例输出
Case 1: A forest of 3 trees.
Case 2: There is one tree.
Case 3: No Trees.

题意:给出一张由n个点和m条边构成的无向图,不是连通的,判断图的每一部分是否是一个树,即图中有几棵树。

满足下列条件可以的点和边可以构成一棵树:1.n个点由n-1条边相连,任意两个点之间只有一条边相连。

解题思路:用并查集把各个部分找出来,对于每一部分判断所有点的度之和与点个数的关系,

如果度之和等于点数*2-2,则可以构成一棵树。

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 5e2 + 10;
int father[N], deg[N];
vector<int> vec[N];void Initial(int n) //初始化
{for(int i = 1; i <= n; i++)father[i] = i, deg[i] = 0;
}int Find(int x) //寻找父节点
{if(father[x] != x)father[x] = Find(father[x]);return father[x];
}void Union(int a, int b) //合并两个集合
{int p = Find(a);int q = Find(b);if(p != q)father[p] = q;
}int main()
{int n, m, i, j, cas = 1;while(~scanf("%d%d",&n,&m) && (n+m)){Initial(n); //并查集初始化int u, v;for(i = 0; i < m; i++){scanf("%d%d",&u,&v);deg[u]++;deg[v]++;Union(u,v);} //求每个点的度数for(i = 1; i <= n; i++){vec[i].clear();//删除容器中保存的所有元素if(Find(i) == i){for(j = 1; j <= n; j++){if(Find(j) == Find(i))vec[i].push_back(j);}}}//找出哪些点属于同一个集合int ans = 0;for(i = 1; i <= n; i++){int cnt = vec[i].size();if(cnt == 0)continue;int sum = 0;for(j = 0; j < cnt; j++){sum += deg[vec[i][j]];}//求集合中点的度数之和if(sum == cnt * 2 - 2)ans++;  //图中无环}printf("Case %d: ",cas++);if(ans == 0)printf("No Trees.\n");else if(ans == 1)printf("There is one tree.\n");elseprintf("A forest of %d trees.\n",ans);}return 0;
}

NYOJ 920 Trees相关推荐

  1. UVA122 树的层次遍历 Trees on the level(两种方法详解)

    UVA122 树的层次遍历 Trees on the level 输入: (11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ...

  2. 多元回归树分析Multivariate Regression Trees,MRT

    作者:陈亮 单位:中国科学院微生物研究所 多元回归树分析 多元回归树(Multivariate Regression Trees,MRT)是单元回归树的拓展,是一种对一系列连续型变量递归划分成多个类群 ...

  3. R语言使用party包中的ctree函数构建条件推理决策树(Conditional inference trees)、使用plot函数可视化训练好的条件推理决策树、条件推理决策树的叶子节点的阴影区域表

    R语言使用party包中的ctree函数构建条件推理决策树(Conditional inference trees).使用plot函数可视化训练好的条件推理决策树.条件推理决策树的叶子节点的阴影区域表 ...

  4. R语言构建决策树(decision trees)模型并进行调优和解释

    R语言构建决策树(decision trees)模型并进行调优和解释 目录 R语言构建决策树(decision trees)

  5. 人脸对齐--One Millisecond Face Alignment with an Ensemble of Regression Trees

    One Millisecond Face Alignment with an Ensemble of Regression Trees CVPR2014 http://www.csc.kth.se/~ ...

  6. hdu1693Eat the Trees(插头dp)

    传送门 先坑着,等啥时候会了再来填坑 不得不说思路真的是很妙啊 1 //minamoto 2 #include<iostream> 3 #include<cstdio> 4 # ...

  7. Some inputs do not have OOB scores. This probably means too few trees were used to compute any relia

    Some inputs do not have OOB scores. This probably means too few trees were used to compute any relia ...

  8. LeetCode之All Possible Full Binary Trees(Kotlin)

    问题: A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list o ...

  9. NYOJ 30 Gone Fishing JAVA+解析

    Gone Fishing这道题目运用的多次折合成一次这种思想我首次见,我想的一个思路是,每次算一下鱼量和时间代价比,这个代码我没有敲,下面的代码是一位仁兄敲得,我研读了一下,做了一个注释,应该有利于后 ...

最新文章

  1. 使用 SAP Cloud SDK 连接 OData 服务
  2. keil5函数 默认返回值_Python中如何调用random()函数
  3. windows功能_你的Windows杀毒软件有这个功能吗?
  4. jQuery.extend() 使用语法详解
  5. 数据结构 - 单链表(Linked List)实现在内存中实现数据以链表形式生成并根据序号排序
  6. 机器学习实战 | 数据探索
  7. jqueryui时间插件_jQueryUI Progressbar插件
  8. 悬赏数据控!2018网易有数可视化大赛火热开启!
  9. android oppo 驱动,OPPO R9驱动安装不了怎么办 OPPO R9驱动安装不了的解决方法
  10. idea 商业版 社区版_idea社区版和商业版的区别
  11. Illustrator CS5序列号
  12. WebSphere如何重启服务
  13. AI全自动原创视频生成器-震撼来袭
  14. 如何用服务器内存做系统盘,服务器内存扩容怎么做
  15. No.2 Earth
  16. NOIP 2016 提高组 Day 1 第二题 天天爱跑步
  17. jQuery - 设置div的内容和属性
  18. SpringBoot模拟单点登录
  19. IEEE论文公式快捷获取
  20. 2-2 学生成绩链表处理 (20 分)

热门文章

  1. Harbor管理docker镜像(1.7.5版本-主从复制)
  2. Linux学习总结(五十四)LVS nat 模式搭建
  3. 亲和属性和链路管理组的TE隧道路径控制原理
  4. Java中对象的深克隆和浅克隆
  5. Bzoj 2453: 维护队列 Bzoj 2120: 数颜色 分块,bitset
  6. 高级特性(2)- XML
  7. 谷歌的网页排序算法(PageRank Algorithm)
  8. Android 中MVC实例之Activity,Window和View
  9. 2009暑期实践报告
  10. CSS 多浏览器兼容又一方案