https://vjudge.net/problem/CodeForces-1167Cios
In some social network, there are n users communicating with each other in m groups of friends. Let's analyze the process of distributing some news between users.this
Initially, some user x 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.spa
For each user x you have to determine what is the number of users that will know the news if initially only user x starts distributing it..net
並查集, 在統計一個組的人數.code
#include <iostream> #include <cstdio> #include <cstring> #include <vector> //#include <memory.h> #include <queue> #include <set> #include <map> #include <algorithm> #include <math.h> #include <stack> #include <string> #include <assert.h> #define MINF 0x3f3f3f3f using namespace std; typedef long long LL; const int MAXN = 5e5+10; int Fa[MAXN], Sum[MAXN]; int n, m; int GetF(int x) { if (Fa[x] == x) return x; Fa[x] = GetF(Fa[x]); return Fa[x]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1;i <= n;i++) Fa[i] = i; for (int i = 1;i <= m;i++) { int k, h, v; cin >> k; if (k) cin >> h; int he = GetF(h); for (int j = 1;j < k;j++) { cin >> v; int tv = GetF(v); if (tv != he) Fa[tv] = he; } } for (int i = 1;i <= n;i++) { int t = GetF(i); Sum[t]++; } for (int i = 1;i <= n;i++) cout << Sum[GetF(i)] << ' '; cout << endl; return 0; }