题目:

In some social network, there are nn users communicating with each other in mm groups of friends. Let's analyze the process of distributing some news between users.

Initially, some user xx receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know.

For each user xx you have to determine what is the number of users that will know the news if initially only user xx starts distributing it.

输入:

The first line contains two integers nn and mm (1≤n,m≤5⋅1051≤n,m≤5⋅105) — the number of users and the number of groups of friends, respectively.

Then mm lines follow, each describing a group of friends. The ii-th line begins with integer kiki (0≤ki≤n0≤ki≤n) — the number of users in the ii-th group. Then kiki distinctintegers follow, denoting the users belonging to the ii-th group.

It is guaranteed that ∑i=1mki≤5⋅105∑i=1mki≤5⋅105.

输出:

Print nn integers. The ii-th integer should be equal to the number of users that will know the news if user ii starts distributing it.

样例:

Example

Input
7 5
3 2 5 4
0
2 1 2
1 1
2 6 7
Output
4 4 1 4 4 2 2 

分析:不想分析。。。

 1 #include<iostream>
2 #include<sstream>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<string>
6 #include<cstring>
7 #include<algorithm>
8 #include<functional>
9 #include<iomanip>
10 #include<numeric>
11 #include<cmath>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #include<cctype>
16 const double PI = acos(-1.0);
17 const int INF = 0x3f3f3f3f;
18 const int NINF = -INF - 1;
19 typedef long long ll;
20 #define MOD 1000007
21 using namespace std;
22 int far[500005];
23 int sum[500005];
24 int n, m;
25 int find(int x)
26 {
27 if(far[x] == x) return x;
28 else return far[x] = find(far[x]);
29 }
30 bool check(int x, int y)
31 {
32 return find(x) == find(y);
33 }
34 void unite(int x, int y)
35 {
36 x = find(x), y = find(y);
37 if(x == y) return;
38 far[y] = x;
39 sum[x] += sum[y];
40 }
41 void init(int n)
42 {
43 for(int i = 0;i <= n;i++)
44 {
45 far[i] = i;
46 sum[i] = 1;
47 }
48 }
49 int main()
50 {
51 cin >> n >> m;
52 init(n);
53 int t;
54 int a, b;
55 while (m--)
56 {
57 cin >> t;
58 if (!t) continue;
59 cin >> a;
60 for (int i = 1; i < t; ++i)
61 {
62 cin >> b;
63 if (!check(a, b) )
64 {
65 unite(a, b);
66 }
67 }
68 }
69 for(int i = 1 ;i <= n; ++i)
70 {
71 int x = find(i);
72 cout << sum[x] << ' ';
73 }
74 return 0;
75 }

Codeforces 1167c(ccpc wannafly camp day1) News Distribution 并查集模板的更多相关文章

  1. Codeforces 745C:Hongcow Builds A Nation(并查集)

    http://codeforces.com/problemset/problem/744/A 题意:在一个图里面有n个点m条边,还有k个点是受限制的,即不能从一个受限制的点走到另外一个受限制的点(有路 ...

  2. Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集

    C. The Labyrinth 题目连接: http://www.codeforces.com/contest/616/problem/C Description You are given a r ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  5. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  6. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  7. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  8. Codeforces 954I Yet Another String Matching Problem(并查集 + FFT)

    题目链接  Educational Codeforces Round 40  Problem I 题意  定义两个长度相等的字符串之间的距离为:   把两个字符串中所有同一种字符变成另外一种,使得两个 ...

  9. [codeforces 859 E] Desk Disorder 解题报告 (并查集+思维)

    题目链接:http://codeforces.com/problemset/problem/859/E 题目大意: 有$n$个人,$2n$个座位. 给出这$n$个人初始的座位,和他们想坐的座位. 每个 ...

随机推荐

  1. hdu 1517 Multiplication Game

    题意: 用整数p乘以2到9中的一个数字.斯坦总是从p = 1开始,做乘法,然后奥利乘以这个数,然后斯坦,以此类推.游戏开始前,他们画一个整数1 < n < 4294967295,谁先到达p ...

  2. 51Nod - 1632

    B国拥有n个城市,其交通系统呈树状结构,即任意两个城市存在且仅存在一条交通线将其连接.A国是B国的敌国企图秘密发射导弹打击B国的交通线,现假设每条交通线都有50%的概率被炸毁,B国希望知道在被炸毁之后 ...

  3. KafkaConsumer 简析

    使用方式 创建一个 KafkaConsumer 对象订阅主题并开始接收消息: Properties properties = new Properties(); properties.setPrope ...

  4. codeforces 1029E Tree with Small Distances【思维+贪心】 【非原创】

    题目:戳这里 学习博客:戳这里 题意:给一个树加最少的边,使得1到所有点的距离小于等于2. 解题思路:分析样例3可以看出,如果一个点到1的距离大于2,那么建立1到该点的父亲节点的边将比直接与该点建边更 ...

  5. L3-002. 堆栈【主席树 or 线段树 or 分块】

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道"堆栈"是一种"先 ...

  6. POJ - 3665 icow

    Fatigued by the endless toils of farming, Farmer John has decided to try his hand in the MP3 player ...

  7. sizeof和strlen在string类中的使用

    字符串的sizeof和strlen 考虑下面的问题: char a[] = "abcdef"; char b[20] = "abcdef"; string s ...

  8. unity编辑器扩展学习

    扩展编辑器实际上就是在unity菜单栏中添加一些按钮,可以一键执行一些重复性的工作. 一.添加按钮 1.简单使用MenuItem特性 using UnityEngine; using UnityEdi ...

  9. /usr/lib/nvidia-384/libEGL.so.1 is not a symbolic link

    记得要将384改为自己系统对应的a. sudo mv /usr/lib/nvidia-384/libEGL.so.1 /usr/lib/nvidia-384/libEGL.so.1.org sudo ...

  10. Linux 如何查看一个文件夹下面有多少个文件

    Linux 如何查看一个文件夹下面有多少个文件 $ tree $ find ./ -type f | wc -l $ ls -l | grep "^-" | wc -l refs ...