Hdu3172 virtual friends and query set

Source: Internet
Author: User

 

Problem description

 

These days, you can do all sorts of things online. for example, you can use varous websites to make virtual friends. for some people, growing their social network (their friends, their friends 'ds ds, their friends 'ds DS 'Friends, and so on), has become
An addictive Hober. Just as some people collect stamps, other people collect virtual friends.  

Your task is to observe the interactions on such a website and keep track of the size of each person's network. 

Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.

 

Input

 

Input file contains multiple test cases.  
The first line of each case indicates the number of test friendship nest.
Each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. each of the following F lines contains the names of two people who have just become friends, separated
A space. A name is a string of 1 to 20 letters (uppercase or lowercase ).

 

Output

 

Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.

 

Sample Input

 

 
13 Fred barneybarney bettybetty Wilma

 

Sample output

 

 
234

The question is as follows: Let's give you two people who are friends and judge how many of them have such relationships in the set of friends and relationships. For example, Xiao Ming and Xiao Hong are friends, xiao Hong and Xiao Li are friends, so Xiao Ming and Xiao Li are also friends. The meaning of the question is very clear and simple application of querying sets. The key is how the names are represented in numbers one by one. Here we use map for implementation, and there are many implementation methods. I 'd like to hear more about it.

Code:

# Include <iostream> # include <string> using namespace STD; # define Max limit 5int parent [Max], deep [Max]; # include <map> int Max; Map <string, int> A; int find (int x) {int S = x; int temp; while (s! = Parent [s]) {S = parent [s];} while (s! = X) {temp = parent [X]; parent [x] = s; X = temp;} return s;} void Union (int x, int y) {int A = find (x); int B = find (y); if (a = B) return; if (! = B) {parent [a] = B; deep [B] + = deep [a] ;}} int main () {int t, n; string name1, name2; while (scanf ("% d", & T )! = EOF) {While (t --) {scanf ("% d", & N); max = 0;. clear (); While (n --) {CIN> name1> name2; if (. find (name1) =. end () {max ++; A [name1] = max; parent [Max] = max; deep [Max] = 1 ;}if (. find (name2) =. end () {max ++; A [name2] = max; parent [Max] = max; deep [Max] = 1 ;}union (A [name1], A [name2]); int P = find (A [name2]); printf ("% d \ n", deep [p]) ;}} return 0 ;}

To achieve better efficiency.

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.