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个数使得这些数大于 ...
随机推荐
- AngularJS 指令实践指南(一)
指令(Directives)是所有AngularJS应用最重要的部分.尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令.这篇教程会为你讲述如何自定义指令,以及介绍如何在 ...
- android API版本对应的系统版本及Android获取手机和系统版本等信息的代码
学了这么久的Android,竟然一直对其API对应的名称关系一值搞不清楚,现在网上认真看了下资料,转载一个觉得写得不错的作者的文章,记下来: [背景] 之前折腾android期间,慢慢地知道了,And ...
- XMPP的总体架构和Jabber ID
通常XMPP的架构用C/S来实现,但是也并不是强制的,Client和Server,Server和Server之间通过TCP连接来通信. 架构的简单示意图如下: C1-----S1----S2----C ...
- Java简单从文件读取和输出
Java简单从文件读取和输出 用Scanner输入,用PrintStream输出 功能:从in.txt读入,输出到out.txt 代码: package ioTest; import java.io. ...
- MAC OpenGL 环境搭建
MAC OpenGL 环境搭建 基础库介绍 先要安装两个库一个是GLEW(OpenGL Extension Wrangler Library),另外一个是GLFW(Graphics Library F ...
- c#网络通信框架networkcomms内核解析之三 消息同步调用
networkcomms.net 来自英国的网络通信框架 官方网址 www.networkcomms.net 中文网址www.networkcomms.cn 客户端发送消息给服务器,服务器计算结果返回 ...
- 常用的HTTP请求头与响应头
HTTP消息头是指,在超文本传输协议( Hypertext Transfer Protocol ,HTTP)的请求和响应消息中,协议头部分的那些组件.HTTP消息头用来准确描述正在获取的资源.服务器或 ...
- XML 介绍
XML eXtensible Markup language:可扩展的标记语言 解决HTML不可扩展的问题, 作用:保存或传输数据,不是用来显示数据的. XML介绍 1. 基于文本格式的 2. 标 ...
- jsp2自定义标签+属性
前面说过jsp2自定义标签如果要加入属性需要设置属性的getter和setter方法.代码如下: public class GetParam extends SimpleTagSupport { pr ...
- Git 学习第三天(一)
远程克隆: 在github新建一个仓库,起名为gitskills 勾选此项,会自动创建一个readme.md文件,然后通过命令 git clone git@github.com:Your.name/g ...