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.

using System; using System.Collections.Generic; using System.Linq; namespace FriendsInNeed { class Program { private static int[][] graph; private static List<int>[] possibleFriends; private static HashSet<int>[] friends; private static int[] degrees; static void Main(string[] args) { ReadGraph(); possibleFriends = new List<int>[graph.Length]; friends = new HashSet<int>[graph.Length]; degrees = Enumerable.Repeat(0, graph.Length).ToArray(); CalculateDegrees(); CalculateFriends(); CalculatePossibleFriends(); PrintPossibleFriends(); } private static void CalculatePossibleFriends() { for (int i = 1; i < possibleFriends.Length; i++) { var possibleFriend = possibleFriends[i]; possibleFriend.Sort(); possibleFriends[i] = possibleFriend; } } private static void

Questions about programming?Chat with your personal AI assistant