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个数使得这些数大于 ...
随机推荐
- Linux系统磁盘分区、删除分区、格式化、挂载、卸载、开机自动挂载的方法总结
Linux系统按照MBR(Master Boot Record)传统分区模式: 注意:传统的MBR(Master Boot Record)分区方式最大只能分2T容量的硬盘,超过2T的硬盘一般采用GPT ...
- NX二次开发-MFC对话框获取UG界面句柄,设置MFC对话框在UG界面固定显示位置
extern "C" DllExport void ufusr(char *param, int *retcod, int param_len) { if (UF_initiali ...
- error LNK2001: 无法解析的外部符号 __imp__RegEnumKeyExA@32
错误: error LNK2001: 无法解析的外部符号 __imp__OpenProcessToken@12 error LNK2001: 无法解析的外部符号 __imp__LookupPrivil ...
- 项目管理模式:外瀑布内敏捷(有人称为“信封法”)--转自知乎大神:CORNERSTONE
作者:CORNERSTONE 链接:https://www.zhihu.com/question/265968122/answer/878124580 来源:知乎 著作权归作者所有.商业转载请联系作者 ...
- checkbox、radio使用jquery改变状态以及其他操作
$('input[type=checkbox]:checked').each(function(index,elem){ $(elem).attr("checked",false) ...
- csdn左侧个人栏目美化,css英文颜色大全,跑马灯效果,点击转到qq联系,点击转到发送邮件。
跑马灯效果: <a href="http://mmmmmm.me" target="_blank"><marquee><font ...
- Collection map
HashMap 的工作原理及代码实现 底层数组+链表实现,可以存储null键和null值,线程不安全 初始size为16,扩容:newsize = oldsize*2,size一定为2的n次幂 扩容针 ...
- Django框架(六)—— 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传
目录 视图层 一.视图函数 二.视图层之HttpRequest对象 三.视图层之HttpResponse对象 四.视图层之JsonResponse对象 五.CBV和FBV 六.文件上传 视图层 一.视 ...
- docker容器的常见操作
进入容器 docker exec -it 12a022ee8127 /bin/bash 交互模式进入容器 docker exec -it 12a022ee8127 ip a 查看容器的ip等信息 批量 ...
- MacOS安装npm全局包的权限问题
MacOS,安装npm全局包提示没有写入权限: npm WARN checkPermissions Missing write access to /usr/local/lib/node_module ...