Circle of Friends

Source: Internet
Author: User

Idea: Each set of numbers entered is stored with arraylist<integer> arr, each circle of friends (set) is represented by a hashset<integer> tmp,arraylist<integer> res Number of people used to store each circle of friends.

For arr[i],i to be even, it is the first number of each group, and I is odd to indicate that it is the second number of each group.

The last set of numbers (2) is used as the initial element of the hashset<integer> tmp, then the number of each set of numbers preceding it is determined to appear in the collection, and if so, the set of numbers is added to the collection, and the set of numbers is deleted in ArrayList arr;

If the number of elements in the collection has changed during the traversal, you will need to iterate over them again. When the elements in the collection have not changed after a traversal, the circle of friends has been found.

Use the same method for the remaining number of arraylist<integer> arr, until arr.size () = 0.

The Java implementation is as follows:

Import Java.util.arraylist;import java.util.collections;import Java.util.hashset;import Java.util.List;import Java.util.scanner;public class Main {public static void Main (string[] args) {Scanner input = new Scanner (system.in); int n = Input.nextint ();//list

  

Circle of Friends

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.