Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- class Relationships {
- List<Integer> friends;
- public Relationships() {
- friends = new ArrayList<>();
- }
- }
- Scanner scan = new Scanner(System.in);
- //n
- int friendsCount = scan.nextInt();
- Relationships[] relationships = new Relationships[friendsCount];
- for(int i = 0; i < friendsCount; i++) relationships[i] = new Relationships();
- //m
- int pairsCount = scan.nextInt();
- for(int i = 0; i < pairsCount; i++) {
- int personA = scan.nextInt();
- int personB = scan.nextInt();
- relationships[personA].friends.add(personB);
- relationships[personB].friends.add(personA);
- }
- int invitedCount = friendsCount;
- boolean hasBeenDeleted = false;
- do {
- for (int i = 0; i < friendsCount; i++) {
- if (!(relationships[i].friends.size() >= 3) || !(invitedCount - relationships[i].friends.size() >= 3)) {
- hasBeenDeleted = true;
- for (int f : relationships[i].friends) {
- relationships[f].friends.remove(i);
- }
- invitedCount--;
- break;
- }
- }
- } while(hasBeenDeleted);
- System.out.println(invitedCount);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement