class from descriptionFri, 20 Jan 2023

In many social networks, users have the option to specify other users as their friends. In addition, there is often a friend recommendation system that shows users people they know indirectly (through one of their friends) and suggests adding these people to the friends list. You have to develop a system for recommending friends. There are n users in the social network we are interested in, each of which is assigned a unique id from 1 to n. Each user of this network has no more than 5 friends. Obviously, no user is a friend to himself, and if user x is in user y's friends list, then user y is also in user x's friends list. Let's describe how the list of possible friends for each user should be formed. For user x, the list should include users y such that: y is not a friend of x and is not the same as x; user y and user x have at least one mutual friend; there is no such user y' who satisfies the first two constraints, and who has strictly more mutual friends with x than y has with x. In other words, the list of possible friends of user x includes all such users who are not his friends, for whom the number of mutual friends with x is maximum. Note that the list of possible friends may be empty. You have to write a program that generates lists of possible friends for all users of the network according to a given structure of the social network.

#include <iostream> #include <vector> using namespace std; struct Relation { int user; int friend_; }; class Network { public: Network(int n) : nodes(n), friends(n) {} void Add(const Relation& r) { nodes[r.user - 1].emplace_back(r.friend_ - 1); nodes[r.friend_ - 1].emplace_back(r.user - 1); } vector<vector<int>> FindPotentialFriends() { for (int i = 0; i < nodes.size(); ++i) { for (auto fi : nodes[i]) { for (auto ffi : nodes[fi]) { if (i == ffi) continue; ++friends[i][ffi]; } } } vector<vector<int>> res; for (int i = 0; i < friends.size(); ++i) { vector<int> cur_friends

Questions about programming?Chat with your personal AI assistant