链接:

https://vjudge.net/problem/CodeForces-1167C

题意:

In some social network, there are n users communicating with each other in m groups of friends. Let's analyze the process of distributing some news between users.

Initially, some user x 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 x you have to determine what is the number of users that will know the news if initially only user x starts distributing it.

思路:

并查集, 在统计一个组的人数.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL; const int MAXN = 5e5+10;
int Fa[MAXN], Sum[MAXN];
int n, m; int GetF(int x)
{
if (Fa[x] == x)
return x;
Fa[x] = GetF(Fa[x]);
return Fa[x];
} int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1;i <= n;i++)
Fa[i] = i;
for (int i = 1;i <= m;i++)
{
int k, h, v;
cin >> k;
if (k)
cin >> h;
int he = GetF(h);
for (int j = 1;j < k;j++)
{
cin >> v;
int tv = GetF(v);
if (tv != he)
Fa[tv] = he;
}
}
for (int i = 1;i <= n;i++)
{
int t = GetF(i);
Sum[t]++;
}
for (int i = 1;i <= n;i++)
cout << Sum[GetF(i)] << ' ';
cout << endl; return 0;
}

CodeForces-1167C的更多相关文章

  1. Codeforces 1167C - News Distribution

    题目链接:http://codeforces.com/problemset/problem/1167/C 题意:大概就是分成几个小团体,给每个人用1 - n编号,当对某个人传播消息的时候,整个小团体就 ...

  2. 【CodeForces - 1167C 】News Distribution(并查集)

    News Distribution 题意 大概就是分成几个小团体,给每个人用1 - n编号,当对某个人传播消息的时候,整个小团体就知道这个消息,输出 分别对1 - n编号的某个人传递消息时,有多少人知 ...

  3. 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 ...

  4. CCPC-Wannafly Summer Camp 2019 Day1

    A - Jzzhu and Cities CodeForces - 449B 题意:n座城市,m条路,k条铁路啥的吧,然后要求最多能删多少条铁路保持1到$n$的最短路不变. 思路:因为铁路是从1出发的 ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  10. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. overflow-x scroll 内部元素滚动,父级容器代码

    display: -webkit-box; overflow-x: scroll; -webkit-overflow-scrolling: touch;

  2. CTF—攻防练习之ssh私钥泄露

    攻防练习1 ssh私钥泄露 靶场镜像:链接: https://pan.baidu.com/s/1xfKILyIzELi_ZgUw4aXT7w 提取码: 59g0 首先安装打开靶场机 没办法登录,也没法 ...

  3. Tensorflow 对上一节神经网络模型的优化

    本节涉及的知识点: 1.在程序中查看变量的取值 2.张量 3.用张量重新组织输入数据 4.简化的神经网络模型 5.标量.多维数组 6.在TensorFlow中查看和设定张量的形态 7.用softmax ...

  4. Pandas中关于 loc \ iloc 用法的理解

    转载至:https://blog.csdn.net/w_weiying/article/details/81411257 loc函数:通过行索引 "Index" 中的具体值来取行数 ...

  5. python3 selenuim PC端使用chrome模拟手机进行H5自动化

    情况说明:初次在做PC端使用chrome进行H5自动化测试时,以为和app端自动化一样使用click()就可以对按钮进行点击,找了好几天也没有找到解决方法,有些人说是工程问题,有些人是使用微信进行H5 ...

  6. 第七周&实验报告五

    实验四 类的继承 •实验目的 •理解抽象类与接口的使用: •了解包的作用,掌握包的设计方法. •实验要求 •掌握使用抽象类的方法. •掌握使用系统接口的技术和创建自定义接口的方法. •了解 Java ...

  7. 单例模式 之 单例模式——Holder

    之前我写过 单例模式中的饿汉模式和懒汉模式 他们虽然都能实现单例模式 但是优缺点很明显 饿汉模式:不能懒加载(类加载就会被实例化),消耗很大,在并发情况下安全性很高. 懒汉模式:能实现懒加载,但是在并 ...

  8. POJ - 1287 Networking (最小生成树&并查集

    You are assigned to design network connections between certain points in a wide area. You are given ...

  9. BZOJ 1053 反素数 题解

    题面 引理1:  1~n中的最大反质数,就是1~n中约数个数最多的数中最小的一个(因为要严格保证g(x)>g(i)): 引理2:1~n中任何数的不同因子不会超过10个,因为他们的乘积大于2,00 ...

  10. C++中组合和继承的概念及意义

    1,继承在面向对象中具有举足轻重的地位,面向对象当中的很多高级技术都和继承是息息相关的,比如面向对象的高端课程<设计模式>中的每一种技术都和继承有关,因此我们非常有必要在学习 C++ 时, ...