Problem 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.
#include<stdio.h>
int fath[505],cycl[505],k,n;
void setfirst()
{
k=n;
for(int i=1;i<=n;i++)
{
fath[i]=i; cycl[i]=0;
}
}
int find_fath(int x)
{
if(x!=fath[x])
fath[x]=find_fath(fath[x]);
return fath[x];
}
void setTree(int a,int b)
{
a=find_fath(a);
b=find_fath(b);
if(cycl[b]&&cycl[a])
return ;
k--;
if(a!=b)
{
if(cycl[a])
fath[b]=a;
else
fath[a]=b;
}
else
cycl[a]=1;
}
int main()
{
int a,b,m,t=1;
while(scanf("%d%d",&n,&m)>0&&m+n!=0)
{
setfirst();
while(m--)
{
scanf("%d%d",&a,&b);
setTree(a,b);
}
printf("Case %d: ",t++);
if(k>1)printf("A forest of %d trees.\n",k);
if(k==1)printf("There is one tree.\n");
if(k==0)printf("No trees.\n");
}
}

hnsd11348tree(并查集)的更多相关文章

  1. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  2. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  3. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  4. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  5. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  6. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  9. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

随机推荐

  1. Html 中select标签的边框与右侧倒三角的去除

    首先是边框的去除:可以设置属性border:none;或border:0px; 不过这还是有一个bug,不同浏览器会在选中select标签时,加上一个边框: 之后是右侧倒三角的去除:设置属性 appe ...

  2. 使用php实现爬虫程序 套取网站的图片实例

    <?php //去采集a67 图片 网站链接 http://www.xiamov.com/list/1/p.2 你也可以采集其他网站的图片 //创建链接 dedecms--a67 //设置执行不 ...

  3. JDK与Tomcat的联系

    如果服务器没有安装JDK或没有配置JDK环境变量,则Tomcat启动出错 报错:需要JAVA_HOME 或JRE_HOME环境变量 所以必须首先安装JDK 配置环境变量 web服务器Tomcat才能运 ...

  4. YII 创建后台模块

    1,在protected/config/main.php目录下修改如下目录 'modules'=>array( // uncomment the following to enable the ...

  5. 【Windows核心编程】Windows常见数据类型

    一,常见数据类型 WORD:               16位无符号整形数据 DWORD:             32位无符号整型数据(DWORD32) DWORD64:         64位无 ...

  6. 传值 UI考试知识点

    传值: 1. 属性传值:从前往后 2. 代理传值:从后往前 3. block: 4. 单例:普通写法和GCD写法 5 . 通知 NSNotification GCD 单例: static PlayMu ...

  7. 最精简的代理设计模式demo - 保姆看孩子

    1,协议文件 @protocol NursePtotocol <NSObject> //保姆的协议方法 - (void)startToAmuseBaby; @end 2,管理类(使用类) ...

  8. IOS设计模式之三:MVC模式

    IOS设计模式之三:MVC模式   模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...

  9. WPF学习笔记-自定义窗口

    代码部分 <Style x:Key="for_noresize_window" TargetType="{x:Type Window}"> < ...

  10. 快速替换图片的组合-AE-样片!

    模板下载网址:http://pan.baidu.com/s/1hqCbErM