题目链接:http://codeforces.com/problemset/problem/1167/C


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

思路:题目可理解为输出1 - n每个号码所在团体总人数,利用并查集不断向集合添加成员,记录每个集合的人数,保存在根节点的sum[]中,查询每个节点的根节点,输出根节点sum[];

总结:初看好像是简单并查集,于是敲了个模板上去果断TLE!orz脑子抽了写了个O(n2)的算法,后面优化了一下就过了。

AC代码:

 #include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 5e5 + ;
int far[maxn];
//int Rank[maxn];
int sum[maxn];
int n,m;
int find(int x)
{
if(far[x] == x)return x;
else return far[x] = find(far[x]);
}
bool check(int x,int y)
{
return find(x) == find(y);
}
void unite(int x,int y)
{
x = find(x);
y = find(y);
if(x == y)return;
far[y] = x;
sum[x] += sum[y];//集合的合并
/* if(Rank[x] > Rank[y]) far[y] = x;
else
{
far[x] = y;
if(Rank[y] == Rank[x]) Rank[y]++;
}*/
}
void init(int n)
{
for(int i = ;i <= n;i++)
{
far[i] = i;
//Rank[i] = 0;
sum[i] = ;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init(n);
int t;
int a,b;
while(m--)
{
scanf("%d",&t);
if(t >= )
{
scanf("%d",&a);
for(int i = ;i < t;i++)
{
scanf("%d",&b);
if(!check(a,b) )
{
unite(a,b);
}
}
}
}
for(int i = ;i <= n;i++)
{
int x = find(i);
printf("%d ",sum[x]);
}
printf("\n");
}
return ;
}

Codeforces 1167C - News Distribution的更多相关文章

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

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

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

  3. Codeforces Gym101522 D.Distribution of Days-算日期 (La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017)

    D.Distribution of Days The Gregorian calendar is internationally the most widely used civil calendar ...

  4. CCPC-Wannafly Summer Camp 2019 Day1

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

  5. Codeforces Gym 100425A Luggage Distribution 二分 数学

    A - Luggage DistributionTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...

  6. 【算法系列学习】codeforces D. Mike and distribution 二维贪心

    http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...

  7. codeforces 798 D. Mike and distribution

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  9. CodeForces - 798D Mike and distribution 想法题,数学证明

    题意:给你两个数列a,b,你要输出k个下标,使得这些下标对应的a的和大于整个a数列的和的1/2.同时这些下标对应的b //题解:首先将条件换一种说法,就是要取floor(n/2)+1个数使得这些数大于 ...

随机推荐

  1. 【Dart学习】-- Dart之匿名方法 & 回调函数 & 闭包

    一,匿名方法定义 (参数1,参数2,....){ 方法体... return 返回值 } 二,匿名方法特性 可赋值给变量,通过变量进行调用 可在其他方法中直接调用或传递给其他方法 void main( ...

  2. 前缀和+排序——cf1043E

    先不考虑第二个条件 要求i和所有其他人的分数和最小,选择x还是y,可以推出一个公式,即差xi-yi小的j都选y,反之都选x 那么按照xi-yi排序即可 然后再考虑第二个条件,做减法就行 /* xi+y ...

  3. delphi下运行vbscript脚本

    简单一个vb脚本,功能为打开被限制的注册表.Set wso = CreateObject("WScript.Shell")wso.RegWrite "HKEY_CURRE ...

  4. js总结体会

    1.表单元素提交之后(不刷新)数据还是会存在的,想要提交之后清除数据可以在ajax提交数据后,用reset()方法清除数据. 2.js中怎么判断哪个单选控件被选中了呢,即表单控件被选中哪些属性发生了变 ...

  5. ubuntu终端仿真程序和文件管理程序

    1.SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单的说是Windows下登录UNIX或Linux服务器主机的软件.可以理解为ubuntu下的Terminal. 如果Sec ...

  6. Openstack组件部署 — Nova_安装和配置Controller Node

    目录 目录 前文列表 Prerequisites 先决条件 To create the databases To create the service credentials Create the C ...

  7. on() 不支持hover事件

    因为 .hover() 是 jQuery 自己定义的事件… 是为了方便用户绑定调用 mouseenter 和 mouseleave 事件而已,它并非一个真正的事件,所以当然不能当做 .bind() 中 ...

  8. vue-router 路由配置

    前提: 项目由 vue-cli 脚手架创建. 首先,先下载 vue-router npm install vue-router 安装完成后,运行项目 npm run dev 打开 main.js , ...

  9. Linux 一些常识命令

    linux的性能优化: .CPU,MEM .DISK--RAID .网络相关的外设,网卡 linux系统性能分析: top:linux系统的负载,CPU,MEM,SWAP,占用CPU和内存比较的进程, ...

  10. android项目各个文件详解

    res目录说明 android应用的res目录是一个特殊的目录,该项目里存放了 android应用所用的全部资源,包括图片资源.字符串资源. 颜色资源.尺寸资源等. /res/value/string ...