Problem D UVA 10608

Problem I

FRIENDS

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

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov

又是并查集

比赛的时候看到又混乱了

其实很简单

 1 #include <iostream>
 2
 3 using namespace std;
 4 int fa[30001];
 5 int visit[30001];
 6 int ans,m,n;
 7
 8 void init()
 9 {
10     for(int i =0;i<=n;i++)
11     {
12         fa[i]=i;
13         visit[i]=0;
14     }
15     ans = 0;
16 }
17
18 int find(int x)
19 {
20     while(x!=fa[x])
21         x = fa[x];
22
23     return x;
24 }
25
26
27 void merge(int x,int y)
28 {
29     int a = find(x);
30     int b = find(y);
31     if(a!=b)
32     {
33         fa[a] = b;
34     }
35 }
36
37 int main()
38 {
39     cin.sync_with_stdio(false);
40     int T;
41     cin>>T;
42     while(T--)
43     {
44         cin>>n>>m;
45         init();
46         while(m--)
47         {
48             int x,y;
49             cin>>x>>y;
50             merge(x,y);
51         }
52
53         for(int i =0;i<=n;i++)
54         {
55             visit[find(i)]++;
56         }
57
58         for(int i =0;i<=n;i++)
59         {
60             ans=ans>visit[i]?ans:visit[i];
61         }
62         cout<<ans<<endl;
63     }
64
65     return 0;
66 }

Problem D UVA 10608

时间: 2024-08-27 10:53:48

Problem D UVA 10608的相关文章

uva 10608 Friends(并查集)

uva 10608 Friends 题目大意:给出两两之间的关系,求出最大的关系网. 解题思路:并查集裸体. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int N = 30005; typedef long long ll; int n, m; in

UVA 10608

并查集简单题#include <iostream>#include <cstdio>using namespace std;#define max 30010int f[max];int getf(int k){    while(k!=f[k]){        k=f[k];    }    return k;}void combine(int a,int b){    int roota=getf(a);    int rootb=getf(b);    if(roota&g

Problem W UVA 662 二十三 Fast Food

Fast Food Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 662 Appoint description:  System Crawler  (2015-08-27) Description The fastfood chain McBurger owns several restaurants along a highway. Recen

UVA 10608 Friends【并查集】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1549 题意:给定n个人m种朋友关系,求最大朋友圈的人数.裸并查集 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>

UVA 10608 Friends 并查集

并查集水题 有n个人,m队朋友,朋友的朋友,也是朋友,A与B是朋友,B与C是朋友,那么A与C也是朋友,即A,B,C在同一个并查集里,合并即可: 最后会有几个"朋友圈子",求最大的朋友圈的人数. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int r[30005]; int x[30010]; i

Uva 10608 Friends

题目是给出总人数,和两个人之间的朋友关系,最后求最多的朋友的人数. 思路:用并查集去求. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 int caseNum,citizen,pairNum; 7 typedef struct node{ 8 int data_;//节点对应人的编号 9 int rank_;//节点对应的秩 10 int

E - The Blocks Problem ( UVA - 101)

- 题目大意 先理解给出的四个移动方式: move a onto b:把木块a.b上的木块放回各自的原位,再把a放到b上: move a over b:把a上的木块放回各自的原位,再把a发到含b的堆上: pile a onto b:把b上的木块放回各自的原位,再把a连同a上的木块移到b上: pile a over b:把a连同a上木块移到含b的堆上. 然后根据条件来移动即可. - 解题思路 如果把每个情况都写出来肯定很麻烦,所以首先先找出他们的共同点然后来写出算法,这样就会简单很多,如 找到某木

CSU-ACM暑假集训基础组训练赛(2) 解题报告

Problem A Codeforces 451A •题意:给你n+m根筷子,将他们分别水平.垂直放置,形成n*m个节点.两个人轮流选择一个点,将穿过这个点的两根筷子拿走,谁可以逼迫对方率先无法继续操作,谁就获胜,问获胜的是先手还是后手. •找规律即可.n,m中较小的那个数是奇数,先手胜,否则后手胜. 1 #include <cstdio> 2 int main(){ 3 int a,b; 4 scanf("%d%d",&a,&b); 5 a = a <

UVA 322 ships (POJ 1138)

题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=258 http://poj.org/problem?id=1138 题目描写叙述:  Ships  Probably everyone who ever attended school knows the game where two opposing players place