Codeforces 1167C - News Distribution
题目链接: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的更多相关文章
- 【CodeForces - 1167C 】News Distribution(并查集)
News Distribution 题意 大概就是分成几个小团体,给每个人用1 - n编号,当对某个人传播消息的时候,整个小团体就知道这个消息,输出 分别对1 - n编号的某个人传递消息时,有多少人知 ...
- 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 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 ...
- CCPC-Wannafly Summer Camp 2019 Day1
A - Jzzhu and Cities CodeForces - 449B 题意:n座城市,m条路,k条铁路啥的吧,然后要求最多能删多少条铁路保持1到$n$的最短路不变. 思路:因为铁路是从1出发的 ...
- Codeforces Gym 100425A Luggage Distribution 二分 数学
A - Luggage DistributionTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...
- 【算法系列学习】codeforces D. Mike and distribution 二维贪心
http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...
- codeforces 798 D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- CodeForces - 798D Mike and distribution 想法题,数学证明
题意:给你两个数列a,b,你要输出k个下标,使得这些下标对应的a的和大于整个a数列的和的1/2.同时这些下标对应的b //题解:首先将条件换一种说法,就是要取floor(n/2)+1个数使得这些数大于 ...
随机推荐
- BZOJ 5120: [2017国家集训队测试]无限之环(费用流)
传送门 解题思路 神仙题.调了一个晚上+半个上午..这道咋看咋都不像图论的题竟然用费用流做,将行+列为奇数的点和偶数的点分开,也就是匹配问题,然后把一个点复制四份,分别代表这个点的上下左右接头,如果有 ...
- python实现语音录入识别
一.介绍 1.第一步录音存入本地 2.调用百度语音识别sdk 注意点:百度语音识别对声音源有要求,比特率必须是256kbps 二.代码 #安装必要库 pip install baidu-aip #百度 ...
- 【Java架构:基础技术】一篇文章搞掂:Spring Tool Suite(STS)
1.简介 STS,是一个自定义版本的Eclipse,她可以方便地用来与各种Spring项目进行交互协作,建议采用Spring进行开发的时候,可以尝试使用这个IDE 2.下载与安装 登录https:// ...
- thinkphp 使用redis 整理(二) mark 一下
参考手册 http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html redis 几种数据类型选择,参考 : https://b ...
- error C2065: CoInitializeEx' : undeclared identifier 解决方法
错误: error C2065: CoInitializeEx' : undeclared identifier 解决方法 原因: 本来程序的编译选项选择的是:使用标准windows库,当改为在静态库 ...
- linux jps命令
原文链接: http://www.cnblogs.com/qlqwjy/p/7928410.html https://blog.csdn.net/u013250071/article/details/ ...
- arttemplate02
1.后台传来的数据 { "code": 200, "checkRecords": [ { "id": "402881e75cc80 ...
- 2. Pycharm的介绍与使用
使用Python原生IDLE IDLE是Python软件包自带的一个集成开发环境,点击开始-->Python安装包-->IDLE.启动 IDLE 时,会显示>>>,可以在 ...
- 3、第一个Appium测试
运行脚本前环境准备: 1.IDE,推荐使用IJ 2.安装jdk环境,推荐>1.8 3.准备一台真机或者模拟器 4.SDK 5.maven环境 项目目录: CalculatorTest.java文 ...
- cesium中divPoint展示数据
cesium中divPoint展示数据 在用点击面获取位置信息的时候,会弹出一个divPoint框,用来展示这个面的属性信息:或者位置信息. 代码如下: var handlerClick = new ...