zoukankan      html  css  js  c++  java
  • UVA 10608 FRIENDS

    Problem Description

    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

    题意:一个镇上有N个人,他们之间有M组朋友关系。有一句名言说:“我朋友的朋友也是我的朋友”。问朋友最多的那个人能有多少朋友。

    一个简单的并查集:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #define N 30005
     4 
     5 int f[N], s[N];
     6 int a, b, n, m, i, num, ans;
     7 
     8 int _find(int x)
     9 {
    10     if (x == f[x])
    11         return f[x];
    12     else
    13         return _find(f[x]);
    14 }
    15 
    16 int main()
    17 {
    18     scanf("%d", &num);
    19     while(num--){
    20         ans = 0;
    21         scanf("%d%d", &n, &m);
    22         for (i = 1; i <= n; i++){
    23             f[i] = i;
    24             s[i] = 1;
    25         }
    26         for (i = 0; i < m; i ++){
    27             scanf("%d%d", &a, &b);
    28             int pa = _find(a);
    29             int pb = _find(b);
    30             if (pa != pb) {
    31                 f[pa] = pb;
    32                 s[pb] += s[pa];
    33                 if(ans < s[pb])
    34                     ans = s[pb];
    35                 }
    36             }
    37             printf("%d
    ", ans);
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    多线程上传和下载大文件(当文件服务器对上传文件大小有限制)
    http和https的区别?
    将数据导出到 excel ,然后下载下来
    注解@Transactional(rollbackFor = Exception.class) 的用法
    浅谈 MongoDB
    浅谈负载均衡
    join,left join,inner join,full join的区别?
    Bootstrap如何适配移动浏览器
    常用正则表达式
    js获取节点
  • 原文地址:https://www.cnblogs.com/zzy9669/p/3871473.html
Copyright © 2011-2022 走看看