C. News Distribution(并查集)
In some social network, there are nn users communicating with each other in mm groups of friends. Let's analyze the process of distributing some news between users.
Initially, some user xx receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.
For each user xx you have to determine what is the number of users that will know the news if initially only user xx starts distributing it.
The first line contains two integers nn and mm (1≤n,m≤5⋅1051≤n,m≤5⋅105) — the number of users and the number of groups of friends, respectively.
Then mm lines follow, each describing a group of friends. The ii-th line begins with integer kiki (0≤ki≤n0≤ki≤n) — the number of users in the ii-th group. Then kiki distinct integers follow, denoting the users belonging to the ii-th group.
It is guaranteed that ∑i=1mki≤5⋅105∑i=1mki≤5⋅105.
Print nn integers. The ii-th integer should be equal to the number of users that will know the news if user ii starts distributing it.
7 5
3 2 5 4
0
2 1 2
1 1
2 6 7
4 4 1 4 4 2 2
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath> const int maxn=5e5+;
typedef long long ll;
using namespace std;
int pre[maxn];
int find(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=find(pre[x]);
}
}
void Merge(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
pre[xx]=yy;
} }
int a[maxn];
int main()
{
int n,m;
cin>>n>>m;
int x;
for(int t=;t<=n;t++)
{
pre[t]=t;
}
int s1,s2;
for(int t=;t<m;t++)
{
scanf("%d",&x);
if(x!=)
scanf("%d",&s1);
for(int j=;j<x;j++)
{
scanf("%d",&s2);
Merge(s1,s2);
s1=s2;
}
} int ans=;
memset(a,,sizeof(a));
for(int t=;t<=n;t++)
{ a[find(t)]++;
}
for(int t=;t<=n;t++)
{
printf("%d ",a[find(t)]);
} return ;
}
C. News Distribution(并查集)的更多相关文章
- Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板
题目: In some social network, there are nn users communicating with each other in mm groups of friends ...
- 【CodeForces - 1167C 】News Distribution(并查集)
News Distribution 题意 大概就是分成几个小团体,给每个人用1 - n编号,当对某个人传播消息的时候,整个小团体就知道这个消息,输出 分别对1 - n编号的某个人传递消息时,有多少人知 ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
随机推荐
- Python生成测试数据-Faker(非LOL选手-李相赫)
Faker介绍 官方文档:https://faker.readthedocs.io/en/master/ Faker is a Python package that generates fake d ...
- ios 继承UITableViewController,更改tableview样式
// 继承UITableViewController,更改tableview样式 - (instancetype)initWithStyle:(UITableViewStyle)style { ret ...
- 关于Exceptionless日志收集框架如何关闭磁盘缓存
问题:在使用Exceptionless的时候,Exception在收集到日志时会默认在appdata里面缓存当条日志的缓存文件,时间久了之后,如果收集到的日志越多磁盘的空间就会不足. 我使用的环境是 ...
- Java环境变量,jdk和jre的区别,面向对象语言编程
什么是java? java是一门面向对象的编程语言,包括java SE, java ME, Java EE . 广泛使用的是作为后端语言的Java EE开发, 面向对象和面向过程? java,C++ ...
- 2020-07-04:tcp三次握手干了啥?time_wait什么时候出现?
福哥答案2020-07-04:三次握手如下:1.SYN j2.ACK j+1,SYN k3.ACK k+1 time_wait出现在断开连接第四次挥手的时候出现.TIME_WAIT状态存在有两个原因. ...
- Memcached高可用组件之repcached
在前边的tomcat session server msm的那篇博客我们用memcached做tomcat session服务器,默认官方memcached是不支持主从同步的,为了解决memcache ...
- 谈谈代码评审(code review)
什么是代码评审(code review)? 根据维基百科的定义,代码评审是一种通过若干人员检阅源代码方式来进行的软件质量保证活动.根据软件工程的经典理论,代码评审应该是收益很高的活动,因其产生在Cod ...
- 【建议收藏】swoft的最佳实践
这是一篇使用 swoft 两个月后的总结文章!,后续会陆续更新的 这是 web-api 开发的总结,如果使用 websocket 等服务的可能不适用,本章节会对一些规范.习惯,或者优化进行一些说明 一 ...
- 关于Redis的工作模式
1.哨兵模式 在redis3.0以前的版本要实现集群一般是借助哨兵sentinel工具来监控master节点的状态,如果master节点异常,则会做主从切换,将某一台slave作为master. 缺点 ...
- 分析dubbo心跳检测机制
目的: 维持provider和consumer之间的长连接 实现: dubbo心跳时间heartbeat默认是60s,超过heartbeat时间没有收到消息,就发送心跳消息(provider,cons ...