Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         class Relationships {
  11.             List<Integer> friends;
  12.  
  13.             public Relationships() {
  14.                 friends = new ArrayList<>();
  15.             }
  16.         }
  17.  
  18.         Scanner scan = new Scanner(System.in);
  19.  
  20.         //n
  21.         int friendsCount = scan.nextInt();
  22.  
  23.         Relationships[] relationships = new Relationships[friendsCount];
  24.         for(int i = 0; i < friendsCount; i++) relationships[i] = new Relationships();
  25.  
  26.         //m
  27.         int pairsCount = scan.nextInt();
  28.         for(int i = 0; i < pairsCount; i++) {
  29.             int personA = scan.nextInt();
  30.             int personB = scan.nextInt();
  31.  
  32.             relationships[personA].friends.add(personB);
  33.             relationships[personB].friends.add(personA);
  34.         }
  35.  
  36.         int invitedCount = friendsCount;
  37.         boolean hasBeenDeleted = false;
  38.         do {
  39.             for (int i = 0; i < friendsCount; i++) {
  40.                 if (!(relationships[i].friends.size() >= 3) || !(invitedCount - relationships[i].friends.size() >= 3)) {
  41.                     hasBeenDeleted = true;
  42.                     for (int f : relationships[i].friends) {
  43.                         relationships[f].friends.remove(i);
  44.                     }
  45.                     invitedCount--;
  46.                     break;
  47.                 }
  48.             }
  49.         } while(hasBeenDeleted);
  50.  
  51.         System.out.println(invitedCount);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement