当前位置:网站首页>L2-024 部落 (25 分)(并查集)
L2-024 部落 (25 分)(并查集)
2022-04-23 08:43:00 【.Ashy.】
描述:
在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈。我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同一个部落。
输入格式:

输出:
首先在一行中输出这个社区的总人数、以及互不相交的部落的个数。随后对每一次查询,如果他们属于同一个部落,则在一行中输出Y,否则输出N。
输入样例:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
输出样例:
10 2
Y
N
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll maxx = 1e18;
const int N = 1e6+100;
const int p = 1e4+10;
const double eps = 1e-8;
int n,m,t,k;
int a,bs;
int b[p];
int pre[N],max1,cnt;
map<int,int>mp;
int find(int x)
{
if(x==pre[x]) return x;
else return pre[x]=find(pre[x]);
}
void unionn(int x,int y)
{
int ya=find(x);
int yb=find(y);
pre[ya]=yb;
}
int main()
{
cin>>n;
for(int i=1;i<=1e6;i++)
{
pre[i]=i;
}//处理根节点
for(int i=1;i<=n;i++)
{
cin>>k;
for(int j=1;j<=k;j++)
{
cin>>b[j];
max1=max(max1,b[j]);
}//输入,找出总人数
for(int j=1;j<=k;j++)
{
for(int s=j+1;s<=k;s++)
{
unionn(b[j],b[s]);
}
}//并
}
cin>>m;
for(int i=1;i<=max1;i++)
{
if(mp[find(i)]==0)
{
mp[find(i)]=1;
cnt++;
}//找不相交集合个数
}
cout<<max1<<" "<<cnt<<endl;
for(int i=1;i<=m;i++)
{
cin>>a>>bs;
if(find(a)==find(bs))
cout<<"Y"<<endl;
else
cout<<"N"<<endl;
}//查
return 0;
}
版权声明
本文为[.Ashy.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/woshilichunyang/article/details/124357487
边栏推荐
猜你喜欢

经典题目刷一刷

Excle plus watermark

洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置

RPC procedure

Consensus Token:web3.0生态流量的超级入口

2021李宏毅机器学习之Adaptive Learning Rate

洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口

Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting

Use of Arthas in JVM tools

RPC过程
随机推荐
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
idea底栏打开services
根据后序和中序遍历输出先序遍历 (25 分)
Detailed description of self feeling of auricular point weight loss 0422
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
Shell脚本进阶
OneFlow学习笔记:从Functor到OpExprInterpreter
Word plus watermark
Illegal character in scheme name at index 0:
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
第一性原理 思维导图
请问中衍期货安全靠谱吗?
Record: JS several methods to delete one or more items in the array
完全二叉搜索树 (30 分)
RCC introduction of Hal Library
Go语言自学系列 | golang结构体作为函数参数
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
K210 learning notes (II) serial communication between k210 and stm32