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. NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用

    NewRowNeeded事件当 VirtualMode 属性为 true 时,将在用户定位到 DataGridView 底部的新行时发生,适合给新行建立一些默认数据和按规则应该产生的数据,但此时不推荐 ...

  2. 微信小程序,大多数人误解的8个问题

    作者:王安,数字天堂DCloud公司创始人兼CEO 注:本文内容包含技术.商业,不懂技术的读者可以只看商业相关的内容.本文仅代表作者一家之言,如有不同意见,欢迎留言讨论~ 8个误解 坊间所传的信息很多 ...

  3. CSS lib

    Yahoo的轻型CSS框架Pure来加速web开发 http://purecss.io/buttons/#

  4. DIV+CSS外部字体引用

    注意: 由于各个浏览器兼容问题大家还是少用这个,下面是具体的使用方法和效果截图: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN&qu ...

  5. SQL Server 连接字符串和身份验证

    SQL Server .NET Data Provider 连接字符串包含一个由一些属性名/值对组成的集合.每一个属性/值对都由分号隔开.         PropertyName1=Value1;P ...

  6. SignalR实现B/S系统对windows服务运行状态的监测

    基于SignalR实现B/S系统对windows服务运行状态的监测 通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windo ...

  7. Lua开发环境配置

    Lua(英语发音:/ˈluːə/)程序设计语言是一个简洁.轻量.可扩展的脚本语言,是葡萄牙语中“Luna”(月亮)的意思. Lua is a powerful, fast, lightweight, ...

  8. bzoj2724

    分块大法好!首先预处理第i块到第j块的答案,这是可以在O(n*tot)内处理出来的 tot表示块数然后考虑询问对于[l,r],答案只可能是[l,r]之间所夹整块[i,j]的答案和非整块中的位置上的数下 ...

  9. POJ Countries in War 3114

    题目大意: 给定一些城市,然后再给一些寄信的路信,A,B,H代表把信从A城市寄到B城市需要H小时. 如果没有直接可以寄达的,可以先通过另外一个城市到达,比如A,B可以寄信,B,C可以寄信,那么,A,C ...

  10. PHP删除数组中特定元素

    方法一: <?php $arr1 = array(1,3, 5,7,8); $key = array_search(3, $arr1); if ($key !== false) array_sp ...