题目链接: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. 管理员技术(六): 硬盘分区及格式化、 新建一个逻辑卷、调整现有磁盘的分区、扩展逻辑卷的大小、添加一个swap分区

    一.硬盘分区及格式化 问题: 本例要求熟悉硬盘分区结构,使用fdisk分区工具在磁盘 /dev/vdb 上按以下要求建立分区: 1> 采用默认的 msdos 分区模式        2> ...

  2. 出现Warning: date(): It is not safe to rely on the system's timezone settings的解决办法

    在没有配置,尤其是新安装的PHP中使用date函数时,会报这个错误: Warning: date(): It is not safe to rely on the system's timezone ...

  3. [bzoj2752]高速公路 题解(线段树)

    2752: [HAOI2012]高速公路(road) Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2102  Solved: 887[Submit] ...

  4. tomcat源码分析一之getCanonicalFile和getAbsolutePath的区别

    最近在看tomcat源码 1.getPath(): 返回定义时的路径,(就是你写什么路径,他就返回什么路径) 2.getAbsolutePath(): 返回绝对路径,但不会处理“.”和“..”的情况 ...

  5. CPUID读取有关Cache的信息

    1: void cpuidTest() 2: { 3: u32 val_eax, val_ebx, val_ecx, val_edx; 4: asm("cpuid" 5: : &q ...

  6. Laravel/php 一些调试技巧

    1. 模型属性不知道哪里修改? 直接覆盖模型的 setAttribute 方法,监测到某一个属性改动的时候,抛一个异常就可以看到堆栈了 use Illuminate\Database\Eloquent ...

  7. Unity中对注册表进行修改

    问题背景: PC端软件开发,当我在Unity中的PlayerSetting中设置好分辨率,每次打包运行后会记忆上次退出时窗口的分辨率(记忆窗口状态),导致下次打开时不是PlayerSetting中的初 ...

  8. ArcGis相接面补节点c#

    相接(Touch)面执行切割后 新面与原相接面会缺少公共节点. private void AddPointToTouchesPolygon(IFeatureCursor newFeatureCurso ...

  9. Mongodb安装遇到的问题

    今天我第一次接触MongoDB,在安装过程中,因为对这个东西不了解,报错了我也不知道是哪里的错,还在网上查了半天资料,后来我懂了,感觉上午就是在浪费时间,是我没睡醒,没睡醒.. 在安装上MongoDB ...

  10. 从零开始搭建系统1.4——MySql安装及配置

    安装环境:CentOS7 64位 ,安装MySQL5.7 1.创建mysql目录 2.在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...