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. 【POJ2155】【二维树状数组】Matrix

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  2. 1.dubbo的安装 quickstart

    按照官网给定的指导,执行下面的步骤即可 1.Import the dubbo source code to eclipse project 在eclipse中安装git插件 egit 直接可以从git ...

  3. oc 怎么接收NSSting字符的方法

    ]; //使用一个缓冲区 NSLog(@"请输入一个字符串:"); scanf("%s",buffer); NSString * str = [NSString ...

  4. phpcms栏目调用

    {loop subcat(0,0,0,$siteid) $r} {php $num++} <h3><a href="{$r[url]}">{$r[catna ...

  5. openerp 报表字段 report_rml_content_data

    按需求修改一个报表,本想按照打印出来的报表内容搜索rml文件,找到需要修改的rml问加. 但是发现搜索不到,后来查询报表动作,确认对应的rml文件中,没有发现 “报表中” 的字串. 猜测可能是rml直 ...

  6. 怎么用notepad配置来运行C语音环境

    想要运行C语言,我们可以用notepad软件来进行编辑,那么怎么用notepad 配置运行c语言开发环境呢? Notepad++是一款很好的编辑器,可以用来开发很多的工具,具体大家请看下文给大家详细讲 ...

  7. Java中传参的值传递和引用传递问题(转)

    今天遇到了一个java程序,需要用参数来返回值(虽然最后用另一种方法实现了),在网上看到这样一篇文章,很受启发. 本文章来自于http://hi.baidu.com/xzhilie/blog/item ...

  8. Hibernate -- 注解(Annotation)关系映射

    转自:http://www.cnblogs.com/tyler2000/archive/2011/01/20/1940354.html 1. Hibernate Annotation关系映射有下面几种 ...

  9. SaaS是个不错的生意模式和创业的选择(独立SAAS厂商的三大优势)

    1. 软件独特而又强大,界面绝美2. 数据存在本国3. 数据不留底 4. 可随时寻求卖身或者合作,不受制于人 --------------------------------------------- ...

  10. Extjs4中tabPanel

    好文章引用:Extjs4 TabPanel例子 感谢原作者...