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>
#include <math.h>
#include <stack>
#include <queue>

using namespace std;

int f[30010];
int c[30010];

int find(int x)
{
    if (f[x] == x) return x;
    else return f[x] = find(f[x]);
}

int main()
{
    int t;
    int n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            f[i]=i;c[i]=1;
        }
        int a,b;
        int ans = -1;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            int t1 = find(a);
            int t2 = find(b);
            if (t1 != t2)
            {
                f[t2] = t1;
                c[t1] += c[t2];
                ans = max(ans,c[t1]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:转载请注明出处。

时间: 2024-11-04 20:12:30

UVA 10608 Friends【并查集】的相关文章

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 - 12232 Exclusive-OR (并查集扩展偏离向量)

Description You are not given n non-negative integersX0,X1,..., Xn-1 less than220, but they do exist, and their values never change. I'll gradually provide you some facts about them, and ask you some questions. There are two kinds of facts, plus one

uva 1455 - Kingdom(并查集+线段树)

题目链接:uva 1455 - Kingdom 题目大意:平面上又n个城市,初始时城市之间没有任何双向道路相连,要求一次执行指令. road A B :在城市A和城市B之间连接一条双向道路 line C:询问一条y=C的水平线上穿过多少州和这些州总共有多少城市. 一个联通分量算一个州,C保证为小数部分为0.5的实数. 解题思路:线段树维护每个位置上州和城市的个数,并查集维护哪些城市属于同一个州,并且要记录这些州上下范围.每次新建一条道路,要相应根据两个州的y坐标范围对线段树进行维护. #incl

uva 1160 - X-Plosives(并查集)

题目链接:uva 1160 - X-Plosives 题目大意:每一种化合物由两种简单的元素组成,现在有n种化合物要装车,如果出现说有k种化合物刚好由k种元组组成,就会发生化学发应.工人在每次装车的时候会检查是否有可能发生发应,有的话将放弃装车.问说最后有几个化合物没有装车. 解题思路:并查集,将每个元组视为一个节点,一种化合物视为边,如果新增一条边形成环则不能加入,统计没有添加的边数即可. #include <cstdio> #include <cstring> #include

Graph Connectivity UVA, 459(并查集)

Graph Connectivity UVA, 459 Time Limit: 3000 MS  Graph Connectivity  Consider a graph G formed from a large number of nodes connected by edges. G is said to be connected if a path can be found in 0 or more steps between any pair of nodes in G. For ex

Network Connections UVA 793(并查集)

Network Connections Time Limit: 3000ms            Memory Limit: 131072KB [PDF Link]  Network Connections  Bob, who is a network administrator, supervises a network of computers. He is keeping a log connections between the computers in the network. Ea

UVA - 459 (并查集)

链接:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22649 题意就是求有多少联通的块.(一开始自己脑补成双联通 然后死也过不了样例. 这道题目的读入也是挺恶心的. #pragma comment(linker, "/STACK:10240000,10240000") #include <algorithm> #include <iostream> #include <ss

UVA 10158 War 并查集

D - War(8.4.3) Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: War A war is being lead between two countries, A and B. As a loyal citizen of C, you dec

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