-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFACEFRND.cs
69 lines (59 loc) · 2.04 KB
/
FACEFRND.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
// https://www.spoj.com/problems/FACEFRND/ #ad-hoc #hash-table
// Finds friends of someone's friends given their friends and their friends' friends.
public static class FACEFRND
{
public static int SolveWithHashSets(int friendCount, int[][] friendDefinitions)
{
var friends = new HashSet<int>();
for (int f = 0; f < friendCount; ++f)
{
friends.Add(friendDefinitions[f][0]);
}
var friendsOfFriends = new HashSet<int>();
for (int f = 0; f < friendCount; ++f)
{
int friendsOfFriendsCount = friendDefinitions[f][1];
for (int fof = 0; fof < friendsOfFriendsCount; ++fof)
{
int friendOfFriendID = friendDefinitions[f][fof + 2];
if (!friends.Contains(friendOfFriendID))
{
friendsOfFriends.Add(friendOfFriendID);
}
}
}
return friendsOfFriends.Count;
}
public static int SolveWithExcept(int friendCount, int[][] friendDefinitions)
{
var friends = new List<int>();
var friendsOfFriends = new List<int>();
for (int f = 0; f < friendCount; ++f)
{
friends.Add(friendDefinitions[f][0]);
int friendsOfFriendsCount = friendDefinitions[f][1];
for (int fof = 0; fof < friendsOfFriendsCount; ++fof)
{
friendsOfFriends.Add(friendDefinitions[f][fof + 2]);
}
}
return friendsOfFriends.Except(friends).Count();
}
}
public static class Program
{
private static void Main()
{
int friendCount = int.Parse(Console.ReadLine());
int[][] friendDefinitions = new int[friendCount][];
for (int f = 0; f < friendCount; ++f)
{
friendDefinitions[f] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
}
Console.WriteLine(
FACEFRND.SolveWithExcept(friendCount, friendDefinitions));
}
}