当前位置:网站首页>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
边栏推荐
- Window function row commonly used for fusion and de duplication_ number
- Zero copy technology
- Tangent space
- Test on the time required for Oracle to delete data with delete
- Oracle view related
- 零拷貝技術
- Solution of discarding evaluate function in surprise Library
- Core concepts of microservice architecture
- Double pointer instrument panel reading (I)
- Oracle lock table query and unlocking method
猜你喜欢
Leetcode | 38 appearance array
Double pointer instrument panel reading (I)
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
Lenovo Savior y9000x 2020
Oracle job scheduled task usage details
Tangent space
[machine learning] Note 4. KNN + cross validation
切线空间(tangent space)
Zero copy technology
零拷貝技術
随机推荐
Dolphin scheduler source package Src tar. GZ decompression problem
Two ways to deal with conflicting data in MySQL and PG Libraries
零拷貝技術
[code analysis (5)] communication efficient learning of deep networks from decentralized data
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
Special window function rank, deny_ rank, row_ number
Use future and countdownlatch to realize multithreading to execute multiple asynchronous tasks, and return results after all tasks are completed
Campus takeout system - "nongzhibang" wechat native cloud development applet
Oracle lock table query and unlocking method
爱可可AI前沿推介 (4.23)
AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
PG SQL intercepts the string to the specified character position
Small case of web login (including verification code login)
Innobackupex incremental backup
Express②(路由)
Solution: you have 18 unapplied migration (s) Your project may not work properly until you apply
Why do you need to learn container technology to engage in cloud native development
[code analysis (1)] communication efficient learning of deep networks from decentralized data
19c RAC steps for modifying VIP and scanip - same network segment
【重心坐标插值、透视矫正插值】原理以及用法见解