zoukankan      html  css  js  c++  java
  • Friends(采用树结构的非线性表编程)

    Description

    Download as PDF

    There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends and B and C are friends then A and C are friends, too.

    Your task is to count how many people there are in the largest group of friends.

    Input

    Input consists of several datasets. The first line of the input consists of a line with the number of test cases to follow. The first line of each dataset contains tho numbers N and M, where N is the number of town's citizens (1≤N≤30000) and M is the number of pairs of people (0≤M≤500000), which are known to be friends. Each of the following M lines consists of two integers A and B (1≤A≤N, 1≤B≤N, A≠B) which describe that A and B are friends. There could be repetitions among the given pairs.

    Output

    The output for each test case should contain one number denoting how many people there are in the largest group of friends.

    Sample Input

    Sample Output

    2

    3 2

    1 2

    2 3

    10 12

    1 2

    3 1

    3 4

    5 4

    3 5

    4 6

    5 2

    2 1

    7 10

    1 2

    9 10

    8 9

    3

    6

     
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int p[30000],c[30000];
    int find(int x)
    {return p[x]==x ? x : p[x]=find(p[x]);
    }
    int main()
    {
     int t,i,x,y;
     cin>>t;
     while(t--)
     {
      int m,n;
      cin>>m>>n;
      for(i=1;i<=m;i++)
      {p[i]=i;c[i]=0;}
      for(i=1;i<=n;i++)
      {
     cin>>x>>y;
      x=find(x);
      y=find(y);
      if(x!=y)
       p[x]=y;
      }
      for(i=1;i<=m;i++)
      {x=find(i);
      c[x]++;
      }
      
      int max=0;
            for(int i=1; i<=m; i++)
                if(c[i]>max)
                    max=c[i];
       
       
       cout<<max<<endl;
       
     }
     return 0;
    }

  • 相关阅读:
    例行性工作排程 (crontab)
    数组
    继续我们的学习。这次鸟哥讲的是LVM。。。磁盘管理 最后链接文章没有看
    htop资源管理器
    转:SSL协议详解
    转:SSL 握手协议详解
    转:Connection reset原因分析和解决方案
    使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
    转:logback的使用和logback.xml详解
    转:Java logger组件:slf4j, jcl, jul, log4j, logback, log4j2
  • 原文地址:https://www.cnblogs.com/lengxia/p/4387820.html
Copyright © 2011-2022 走看看