当前位置:网站首页>L2-024 部落 (25 分)
L2-024 部落 (25 分)
2022-04-23 13:48:00 【黑谷小健】
题意:
在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈。我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同一个部落。
思路:
一个非常裸的并查集吧,注意连完边后再遍历一遍确定把边都连上了,再继续查询操作
代码:
#include<bits/stdc++.h>
#include<sstream>
#include<unordered_map>
#define endl '\n'
using namespace std;
const int maxn=10005;
int p[maxn],a[maxn];
int findx(int x)
{
if(p[x]!=x)
{
return p[x]=findx(p[x]);
}
else {
return p[x];
}
}
unordered_map<int ,int >mo;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,i,j,k,t,cnt=0,cnt1=0,q;
cin>>n;
for(i=1;i<maxn;i++) p[i]=i;
for(i=0;i<n;i++)
{
cin>>k;
int t=-1,d;
for(j=0;j<k;j++)
{
cin>>d;
if(mo[d]==0){
mo[d]++;
cnt++;
}
if(t==-1) {
t=d;
}
else {
int m1=findx(t);
int m2=findx(d);
if(m1!=m2)
{
p[m2]=m1;
}
}
}
}
mo.clear();
for(i=1;i<=cnt;i++)
{
p[i]=findx(p[i]);
if(mo[p[i]]==0) cnt1++;
mo[p[i]]++;
}
cin>>q;
cout<<cnt<<" "<<cnt1<<endl;
for(i=0;i<q;i++)
{
int u,v;
cin>>u>>v;
if(findx(u)!=findx(v))
{
cout<<"N"<<endl;
}
else {
cout<<"Y"<<endl;
}
}
return 0;
}
版权声明
本文为[黑谷小健]所创,转载请带上原文链接,感谢
https://blog.csdn.net/RIPKEY/article/details/124313117
边栏推荐
- Campus takeout system - "nongzhibang" wechat native cloud development applet
- Oracle job scheduled task usage details
- Double pointer instrument panel reading (I)
- Express②(路由)
- SQL learning window function
- MySQL [read / write lock + table lock + row lock + mvcc]
- leetcode--380.O(1) 时间插入、删除和获取随机元素
- TCP reset Gongji principle and actual combat reproduction
- 【报名】TF54:工程师成长地图与卓越研发组织打造
- 爱可可AI前沿推介 (4.23)
猜你喜欢
【vmware】vmware tools 地址
【项目】小帽外卖(八)
Express②(路由)
[machine learning] Note 4. KNN + cross validation
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
MySQL [acid + isolation level + redo log + undo log]
Express ② (routing)
【重心坐标插值、透视矫正插值】原理以及用法见解
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
MySQL and PgSQL time related operations
随机推荐
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
Personal learning related
Influence of openssh version on SSH mutual trust creation in RAC environment
Dolphin scheduler configuring dataX pit records
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
Storage scheme of video viewing records of users in station B
Oracle renames objects
Lenovo Savior y9000x 2020
What does the SQL name mean
UNIX final exam summary -- for direct Department
QT calling external program
SQL learning | complex query
Ai21 labs | standing on the shoulders of giant frozen language models
Core concepts of microservice architecture
Reading notes: Secure federated matrix factorization
Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
爱可可AI前沿推介 (4.23)
Apache seatunnel 2.1.0 deployment and stepping on the pit
Express②(路由)