zoukankan      html  css  js  c++  java
  • hnsd11348tree(并查集)

    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.
    ",k);
            if(k==1)printf("There is one tree.
    ");
            if(k==0)printf("No trees.
    ");
        }
    }
    


  • 相关阅读:
    网页中的图片查看器viewjs使用
    检测和删除多余无用的css
    网页中插入视频的方案
    WebSocket使用教程
    JS+CSS简单实现DIV遮罩层显示隐藏【转藏】
    使用GPS经纬度定位附近地点(某一点范围内查询)
    使用SQL Server Management Studio 创建数据库备份作业
    SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤
    关于LINQ方方面面的入门、进阶、深入的文章。
    LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3297240.html
Copyright © 2011-2022 走看看