Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.ttu.algoritmid.bond;
- import java.util.HashMap;
- import java.util.Map;
- public class AL08 {
- public enum Network {
- FRIENDLY, UNFRIENDLY, UNKNOWN;
- }
- Map<String, Network> people;
- private DisjointSubsets disjointSubsets = new DisjointSubsets();
- public AL08() {
- people = new HashMap<>();
- friendly("A");
- unfriendly("U");
- }
- public DisjointSubsets getDisjointSubsets() {
- return disjointSubsets;
- }
- public void talkedToEachOther(String name1, String name2) {
- disjointSubsets.union(name1,name2);
- String rootOne = disjointSubsets.find(name1);
- String rootTwo = disjointSubsets.find(name2);
- if(people.get(rootOne) != people.get(rootTwo))
- {
- if(people.get(rootOne) == Network.UNKNOWN)
- {
- people.put(rootOne, people.get(rootTwo));
- }
- else
- {
- people.put(rootTwo, people.get(rootOne));
- }
- }
- }
- public void addPerson(String name) {
- disjointSubsets.addSubset(name);
- people.put(name, Network.UNKNOWN);
- }
- public void friendly(String name) {
- disjointSubsets.addSubset(name);
- people.put(name, Network.FRIENDLY);
- }
- public void unfriendly(String name) {
- disjointSubsets.addSubset(name);
- people.put(name, Network.UNFRIENDLY);
- }
- public Network memberOfNetwork(String name) {
- String root = disjointSubsets.find(name);
- return people.get(root);
- }
- public static void main(String[] args) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement