zoukankan      html  css  js  c++  java
  • C. News Distribution(并查集)

    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.

    Input

    The first line contains two integers nn and mm (1n,m51051≤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 (0kin0≤ki≤n) — the number of users in the ii-th group. Then kiki distinct integers follow, denoting the users belonging to the ii-th group.

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

    Output

    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
    Copy
    7 5
    3 2 5 4
    0
    2 1 2
    1 1
    2 6 7
    
    output
    Copy
    4 4 1 4 4 2 2 

     代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    
    const int maxn=5e5+5;
    typedef long long ll;
    using namespace std;
    int pre[maxn];
    int find(int x)
    {
        if(x==pre[x])
        {
            return x;
        }
        else
        {
            return pre[x]=find(pre[x]);
        }
    }
    void Merge(int x,int y)
    {
        int xx=find(x);
        int yy=find(y);
        if(xx!=yy)
        {
            pre[xx]=yy;
        }
        
    }
    int a[maxn];
    int main()
    {
        int n,m;
        cin>>n>>m;
        int x;
        for(int t=1;t<=n;t++)
        {
            pre[t]=t;
        }
        int s1,s2;
        for(int t=0;t<m;t++)
        {
            scanf("%d",&x);
            if(x!=0)
            scanf("%d",&s1);
            for(int j=1;j<x;j++)
            {
                scanf("%d",&s2);
                Merge(s1,s2);
                s1=s2;
            }
        }
    
        int ans=0;
        memset(a,0,sizeof(a));
        for(int t=1;t<=n;t++)
        {
        
            a[find(t)]++;
        }
        for(int t=1;t<=n;t++)
        {
            printf("%d ",a[find(t)]);
        }
    
        
        return 0;
    }
  • 相关阅读:
    Android与互联网的交互方式有三种
    Android 本地tomcat服务器接收处理手机上传的数据之案例演示
    Android 本地tomcat服务器接收处理手机上传的数据之环境搭建
    win7 查看当前java路径
    Android 使用tomcat搭建HTTP文件下载服务器
    Android 本地搭建Tomcat服务器供真机测试
    Android org.apache.http.*找不到
    Android IndicatorSeekBar
    Volley overview
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error(Sqlite code 14): Could not open database,(OS error
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10877798.html
Copyright © 2011-2022 走看看