Ultraviolet A 11503-virtual friends

Source: Internet
Author: User

Question link:

Ultraviolet A: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 24 & page = show_problem & problem = 2498

HDU: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3172

Type: query set, hash

Original question:

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 'Friends,
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 Specification

The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer F,
The number of friendships formed, 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 by a space. A name is a string of 1 to 20 letters (uppercase or lowercase ).

Sample Input
13Fred BarneyBarney BettyBetty Wilma
Output Specification

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.

Output for sample input
234
Question:If A and B are friends, and C is friends of B, then A and B are friends. In this way, a network of friends will be formed. Only one person in the network will know everyone in the network. Enter a friend relationship. Each time you enter a relationship, the total number of friends in the two's friend network is output.

Analysis and Summary:It is obvious to query the set questions. The key to this question is to convert a person's name into a number. This is a very pitfall: the input T is also a set of groups, to use while (scanf ("% d", & T )! = EOF) {While (t --){...}}

1. Use STL map directly for processing. But the speed is not good: 2.124 S (ultraviolet A), 625 ms (HDU)
/** Query set + map * Time: 2.124 S (ultraviolet A), 625 ms (HDU) */# include <iostream> # include <map> # include <string> # include <cstdio> # include <cstring> # define n 100005 using namespace STD; Map <string, int> MP; int father [N], num [N]; void Init () {for (INT I = 0; I <n; ++ I) father [I] = I;} int find (int x) {int I, j = x; while (J! = Father [J]) J = Father [J]; while (X! = J) {I = Father [X]; father [x] = J; X = I;} return J;} void Union (INT X, int y) {int A = find (x); int B = find (y); if (! = B) {FATHER [a] = B; num [B] + = num [a] ;}} int main () {# ifdef local freopen ("input.txt ", "r", stdin); # endif int t, n, k; string person1, person2; while (scanf ("% d", & T )! = EOF) {While (t --) {scanf ("% d", & N); int CNT = 1; Init (); MP. clear (); While (n --) {CIN> person1> person2; int x1, x2; If (! (X1 = MP [person1]) {x1 = MP [person1] = CNT ++; num [cnt-1] = 1 ;}if (! (X2 = MP [person2]) {X2 = MP [person2] = CNT ++; num [cnt-1] = 1;} Union (x1, x2 ); int x = find (X1); printf ("% d \ n", num [x]) ;}} return 0 ;}
2. Use a hash table to map the relationship between strings. The speed is quite good, and both of them have entered rank.
/** Ultraviolet A 11503-virtual friends * and query set + hash * Time: 0.240 S (ultraviolet A), 156 ms (HDU) */# include <cstdio> # include <cstring> # define n 200005int N, F [N], rank [N]; char name [N] [25]; const int maxhashsize = 1000003; int head [maxhashsize], next [N]; inline void init_lookup_table () {memset (Head, 0, sizeof (head ));} inline int Hash (char * Str) {int seed = 131, V = 0; while (* Str) V = V * seed + (* STR ++ ); return (V & 0x7fffffff) % maxhas Hsize;} int try_to_insert (INT s) {int H = hash (name [s]); int u = head [H]; while (u) {If (strcmp (name [u], name [s]) = 0) return U; u = next [u];} next [s] = head [H]; head [H] = s; return s;} void Init () {for (INT I = 0; I <n; ++ I) f [I] = I, rank [I] = 1;} int find (int x) {int I, j = x; while (J! = F [J]) J = f [J]; while (X! = J) {I = f [X]; F [x] = J; X = I;} return J;} int Union (INT X, int y) {int A = find (x), B = find (y); if (a = B) Return rank [a]; rank [a] + = rank [B]; f [B] = A; return rank [a];} int main () {int t; char name1 [25], name2 [25]; while (~ Scanf ("% d", & T) {While (t --) {Init (); init_lookup_table (); scanf ("% d", & N ); int Pos = 1; if (n = 0) {puts ("0"); continue;} For (INT I = 0; I <n; ++ I) {int A, B; scanf ("% s", name [POS]); If (A = try_to_insert (POS) = POS) {++ Pos ;} scanf ("% s", name [POS]); If (B = try_to_insert (POS) = POS) {++ Pos ;} printf ("% d \ n", Union (a, B) ;}} return 0 ;}
3. Use the dictionary tree (to be supplemented ...)



-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double(For details, refer)

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.