当前位置:网站首页>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
边栏推荐
- Get the attribute value difference between two different objects with reflection and annotation
- About me
- Double pointer instrument panel reading (I)
- The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
- Reading notes: meta matrix factorization for federated rating predictions
- JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
- Solve tp6 download error course not find package topthink / think with stability stable
- Express中间件③(自定义中间件)
- Analysis of redo log generated by select command
- MySQL [SQL performance analysis + SQL tuning]
猜你喜欢
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
【重心坐标插值、透视矫正插值】原理以及用法见解
Kettle--控件解析
Dolphin scheduler integrates Flink task pit records
Dolphin scheduler scheduling spark task stepping record
MySQL [read / write lock + table lock + row lock + mvcc]
SQL learning window function
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
Search ideas and cases of large amount of Oracle redo log
随机推荐
Leetcode brush question 897 incremental sequential search tree
JS compares different elements in two arrays
AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
Analysis of unused index columns caused by implicit conversion of timestamp
软考系统集成项目管理工程师全真模拟题(含答案、解析)
Lenovo Savior y9000x 2020
Software test system integration project management engineer full truth simulation question (including answer and analysis)
Campus takeout system - "nongzhibang" wechat native cloud development applet
Dolphin scheduler integrates Flink task pit records
Tensorflow Download
TCP reset Gongji principle and actual combat reproduction
AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)
这个SQL语名是什么意思
Detailed explanation of Oracle tablespace table partition and query method of Oracle table partition
OSS cloud storage management practice (polite experience)
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Ora-16047 of a DG environment: dgid mismatch between destination setting and target database troubleshooting and listening vncr features
Common types and basic usage of input plug-in of logstash data processing service
SQL learning window function
Solution of discarding evaluate function in surprise Library