Social Networking Graph

On a social networking site, people are connected with other people. The whole system appears as a giant connected graph. You are required to answer the total number of people connected at t nodes away from each other (t distance connectivity). For example, Two persons directly connected are at 1 distance connectivity. While the two persons having a common contact without having direct connectivity, are at 2 distance connectivity.

Example:

Input:  n = 9, e = 10, edges={{1, 2}, {2, 3}, {1, 7}, {2, 4}, {3, 4}, {4, 7}, {7, 8}, {9, 7}, {7, 6}, {5, 6}}, x = 4, d = 2
Output: 4

Approach

C++

#include <bits/stdc++.h>
using namespace std;

vector<long longarr[100001];
long long vis[1000001];
long long dist[1000001];
void bfs(long long src)
{
    queue<long longq;
    q.push(src);
    dist[src] = 0;
    vis[src] = 1;
    while (!q.empty())
    {
        long long curr = q.front();
        q.pop();
        for (long long child : arr[curr])
        {
            if (vis[child] == 0)
            {
                vis[child] = 1;
                dist[child] = dist[curr] + 1;
                q.push(child);
            }
        }
    }
}
long long socialNetworkingGraph(vector<vector<long long>> &edges,
                                long long nlong long e,
                                long long xlong long d)

{
    for (long long i = 0i < ei++)
    {
        long long a = edges[i][0]b = edges[i][1];

        arr[a].push_back(b);
        arr[b].push_back(a);
    }

    for (long long i = 0i <= ni++)
    {
        vis[i] = 0;
        dist[i] = 0;
    }
    bfs(x);
    long long cnt = 0;
    for (long long i = 0i <= ni++)
    {
        if (dist[i] == d)
            cnt++;
    }
    return cnt;
}
int main()
{
    long long n = 9e = 10;
    vector<vector<long long>> edges = {{12},
                                       {23},
                                       {17},
                                       {24},
                                       {34},
                                       {47},
                                       {78},
                                       {97},
                                       {76},
                                       {56}};

    long long x = 4d = 2;
    cout << socialNetworkingGraph(edgesnexd<< "\n";

    return 0;
}


No comments:

Post a Comment