Codeforces 1167C - News Distribution

题目链接:http://codeforces.com/problemset/problem/1167/C


思路:初看好像是简单并查集,于是敲了个模板上去果断TLE!orz脑子抽了写了个O(n2)的算法。后面优化了一下就过了。

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 const int maxn = 5e5 + 5;
 5 int far[maxn];
 6 //int Rank[maxn];
 7 int sum[maxn];
 8 int n,m;
 9 int find(int x)
10 {
11     if(far[x] == x)return x;
12     else return far[x] = find(far[x]);
13 }
14 bool check(int x,int y)
15 {
16     return find(x) == find(y);
17 }
18 void unite(int x,int y)
19 {
20     x = find(x);
21     y = find(y);
22     if(x == y)return;
23     far[y] = x;
24     sum[x] += sum[y];//集合的合并
25    /* if(Rank[x] > Rank[y]) far[y] = x;
26     else
27     {
28         far[x] = y;
29         if(Rank[y] == Rank[x]) Rank[y]++;
30     }*/
31 }
32 void init(int n)
33 {
34     for(int i = 0;i <= n;i++)
35     {
36         far[i] = i;
37         //Rank[i] = 0;
38         sum[i] = 1;
39     }
40 }
41 int main()
42 {
43     while(~scanf("%d%d",&n,&m))
44     {
45         init(n);
46         int t;
47         int a,b;
48         while(m--)
49         {
50             scanf("%d",&t);
51             if(t >= 1)
52             {
53                 scanf("%d",&a);
54                 for(int i = 1;i < t;i++)
55                 {
56                     scanf("%d",&b);
57                     if(!check(a,b) )
58                     {
59                         unite(a,b);
60                     }
61                 }
62             }
63         }
64         for(int i = 1;i <= n;i++)
65         {
66             int x = find(i);
67             printf("%d ",sum[x]);
68         }
69         printf("\n");
70     }
71     return 0;
72 }

猜你喜欢

转载自www.cnblogs.com/Carered/p/10947313.html