zoukankan      html  css  js  c++  java
  • Circles of Friends

    A circle of friends is a network of friend relationships. If A is a friend of B, then B is considered a friend of A no matter B admits or not, and they are said to belong to the same circle. Here we assume that friendship is transitive, that is, if A is a friend of B, and B is a friend of C, then A is a friend of C and the three of them all belong to the same circle.

    On the other hand, A is not so close to C as B is. We define the distance D(X, Y) between two friends X and Y as the minimum number of friends between them. For example, D(A, B) = 0, and D(C, A) = 1. The diameter of a friends circle is the maximum distance between any pair of friends in the circle.

    Now given some people's relationships, you are supposed to find the number of friends circles and the circle with the largest diameter.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives an integer N (2), which is the total number of people involved, and hence they are numbered from 1 to N. Then N lines follow, each in the format:

    p1​​ ... pk​​

    where k (0) is the number of friends and p1​​ to pk​​ (if k>0) are the friends' indices. The i-th line corresponds to the i-th person. All the numbers in a line are separated by spaces. It is guaranteed that no one is given as a friend of oneself.

    Output Specification:

    For each case, print in a line the number of friends circles, and the largest diameter, separated by exactly one space.

    Sample Input:

    17
    2 15 12
    1 17
    2 16 9
    1 8
    4 10 13 15 14
    0
    2 11 14
    1 4
    2 2 3
    2 13 11
    2 15 7
    2 1 14
    2 5 15
    0
    0
    1 3
    1 2
    
     

    Sample Output:

    4 3
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 vector<set<int> > v;
     4 vector<bool> vb;
     5 vector<int> vr;
     6 set<int>::iterator its;
     7 int spart,smax;
     8 void bfs(int n,bool flag)
     9 {
    10     vector<bool> vbb(vb.size(),true);
    11     int md=0;
    12     pair<int,int> p;
    13     queue<pair<int,int> > q;
    14     q.push(pair<int,int>(n,-1));
    15     if(flag)
    16     vb[n]=false;
    17     else
    18     vbb[n]=false;
    19     for(;!q.empty();q.pop())
    20     {
    21         p=q.front();
    22         if(p.second>md)
    23         {
    24             md=p.second;
    25             if(flag)
    26             {
    27                 vr.clear();
    28                 vr.push_back(p.first);
    29             }
    30         }
    31         else if(p.second==md)
    32         if(flag)
    33         vr.push_back(p.first);
    34         for(its=v[p.first].begin();its!=v[p.first].end();++its)
    35         {
    36             if(flag)
    37             {
    38                 if(vb[*its])
    39                 {
    40                     vb[*its]=false;
    41                     q.push(pair<int,int>(*its,p.second+1));
    42                 }
    43             }
    44             else
    45             {
    46                 if(vbb[*its])
    47                 {
    48                     vbb[*its]=false;
    49                     q.push(pair<int,int>(*its,p.second+1));
    50                 }
    51             }
    52         }
    53     }
    54     if(md>smax)
    55     smax=md;
    56     return;
    57 }
    58 int main()
    59 {
    60 //    ios::sync_with_stdio(false);
    61  //     freopen("data.txt","r",stdin);
    62       int n,k,x;
    63       spart=0,smax=0;
    64       scanf("%d",&n);
    65       v.resize(n);
    66       vb.resize(n,true);
    67       for(int i=0;i<n;i++)
    68       {
    69           scanf("%d",&k);
    70           for(;k--;)
    71           {
    72               scanf("%d",&x);
    73               x--;
    74             v[i].insert(x);
    75             v[x].insert(i);
    76         }
    77     }
    78     for(int t=0;t<v.size();t++)
    79     {
    80         if(vb[t])
    81         {
    82             bfs(t,true);
    83             for(int i=0;i<vr.size();i++)
    84             bfs(vr[i],false);
    85             spart++;
    86             t=0;
    87         }
    88     }
    89     printf("%d %d",spart,smax);
    90 
    91     return 0;
    92 }
    诚者,君子之所守也。
  • 相关阅读:
    阿里数据中台
    智慧公安及移动执法平台(中云微迅)
    平安智慧城市的智慧政务
    数据分析师干货-业务逻辑篇
    数据分析师常用商业模型(一)
    智慧政务大数据云平台解决方案
    说几个flink好做spark却不好做的场景
    spark比flink好用的点
    干货好文!自底向上——知识图谱构建技术初探
    用户分析模型
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285794.html
Copyright © 2011-2022 走看看