using
System;
using
System.Collections.Generic;
class
GFG {
public
static
void
Main(String[] args)
{
List<List<
int
> > initialGroups
=
new
List<List<
int
> >();
initialGroups.Add(
new
List<
int
>() { 1, 3, 8 });
initialGroups.Add(
new
List<
int
>() { 2, 5 });
initialGroups.Add(
new
List<
int
>() { 4 });
initialGroups.Add(
new
List<
int
>() { 9, 6 });
initialGroups.Add(
new
List<
int
>() { 7, 10, 11, 12 });
List<List<
int
> > finalGroups
=
new
List<List<
int
> >();
finalGroups.Add(
new
List<
int
>() { 1, 3 });
finalGroups.Add(
new
List<
int
>() { 9 });
finalGroups.Add(
new
List<
int
>() { 8 });
finalGroups.Add(
new
List<
int
>() { 12, 11 });
finalGroups.Add(
new
List<
int
>() { 7, 6 });
finalGroups.Add(
new
List<
int
>() { 2, 5 });
finalGroups.Add(
new
List<
int
>() { 4, 10 });
var
peopleWithNoOutsideInteractions
= peoplesInteractionCheck(initialGroups,
finalGroups,
false
);
Console.WriteLine(
"Elements with no outside interactions"
);
printVector(peopleWithNoOutsideInteractions);
var
peopleWhoSwitchedGroups
= peoplesInteractionCheck(initialGroups,
finalGroups,
true
);
Console.WriteLine(
"Elements that changed their groups"
);
printVector(peopleWhoSwitchedGroups);
}
private
static
void
printVector(List<
int
> vec)
{
foreach
(
var
value
in
vec)
Console.Write($
"{value} "
);
Console.WriteLine();
}
private
static
bool
isSubsetOrEqual(List<
int
> parent,
List<
int
> child,
bool
strictEqual)
{
if
(strictEqual ? child.Count != parent.Count
: child.Count > parent.Count)
return
false
;
HashSet<
int
> hashset =
new
HashSet<
int
>(parent);
foreach
(
var
item
in
child)
if
(!hashset
.Contains(
(item)))
return
false
;
return
true
;
}
private
static
Dictionary<
int
,
int
>
getPersonToGroupIdxMap(List<List<
int
> > groups)
{
Dictionary<
int
,
int
> personToGroupIdxMap
=
new
Dictionary<
int
,
int
>();
int
groupIdx = 0;
foreach
(
var
group
in
groups)
{
foreach
(
var
person
in
group
)
{
personToGroupIdxMap[person] = groupIdx++;
}
}
return
personToGroupIdxMap;
}
private
static
List<
int
>
peoplesInteractionCheck(List<List<
int
> > initialGroups,
List<List<
int
> > finalGroups,
bool
strictEqual)
{
var
result =
new
List<
int
>();
var
initialpersonToGroupIdxMap
= getPersonToGroupIdxMap(initialGroups);
foreach
(
var
groupInFinal
in
finalGroups)
{
var
person = groupInFinal[0];
var
initialGroupIdx
= initialpersonToGroupIdxMap[person];
var
groupInInitial =
new
List<
int
>();
foreach
(
var
g
in
initialGroups)
{
if
((g).Contains((person)))
groupInInitial = g;
}
var
isSubsetOrEqualResult = isSubsetOrEqual(
groupInInitial, groupInFinal, strictEqual);
if
(strictEqual && !isSubsetOrEqualResult)
result.AddRange(groupInFinal);
if
(!strictEqual && isSubsetOrEqualResult)
result.AddRange(groupInFinal);
}
return
result;
}
}