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.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
随机推荐
- 属性集 Properties
5.1 概述 java.util.Properties 继承于 Hashtable ,来表示一个持久的属性集.它使用键值结构存储数据,每个键及其对应值都是一个字符串.该类也被许多Java类使用,比如获 ...
- 一文说通C#中的异步编程补遗
前文写了关于C#中的异步编程.后台有无数人在讨论,很多人把异步和多线程混了. 文章在这儿:一文说通C#中的异步编程 所以,本文从体系的角度,再写一下这个异步编程. 一.C#中的异步编程演变 1. ...
- GitHub/Git配置与简单的使用
今天我开始了初步的学习,首先从陌生的开始下手,GitHub,自己通过查询网络上的资料有了初步的理解与认识.进行了Git与GitHub的配置. 一.前期准备 首先下载Git,Git官网->http ...
- 【学习笔记】ThreadLocal与引用类型相关知识点
0 写在前边 今天以 "TheadLocal 为什么会导致内存泄漏" 为题与朋友们讨论了一波,引出了一些原理性的内容,本文就这个问题作答,并扩展相关的知识点 1 ThreadLoc ...
- kubeadm部署1.17.3[基于Ubuntu18.04]
基于 Ubuntu18.04 使用 kubeadm 部署Kubernetes 1.17.3 高可用集群 环境 所有节点初始化 # cat <<EOF>> /etc/hosts ...
- Vue中v-model指令的常用修饰符
v-model指令有三个可以选用的修饰符:.lazy..number以及.trim.vue官方对此的描述为: .number-输入字符串转为有效的数字 .lazy-取代input监听change事件 ...
- asp.netcore 3.1 program、Startup 类详解
Program类 public class Program { /// <summary> /// 应用程序入口 /// 1.asp.netcore 本质上是控制台程序 /// </ ...
- golang 字符型
目录 前言 1. 基本 介绍 2. 声明 3. 使用细节 4. 字符类型的本质 跳转 前言 不做文字的搬运工,多做灵感性记录 这是平时学习总结的地方,用做知识库 平时看到其他文章的相关知识,也会增加到 ...
- CPF 入门教程(一)
CPF netcore跨平台UI框架 开发之前,你需要先安装VS2019.为什么要vs2019?因为VS2019才能支持netcore3.0以上,netcore3.0之后才能发布独立的程序,之前的都只 ...
- TypeScript是什么,为什么要使用它?
转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文出处:https://medium.com/swlh/what-is-typescript-bf333e ...