UVa 11503:virtual Friends

Source: Internet
Author: User

Topic Link:

Uva: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=3172

Type: And look-up set, hash

Original title:

This is the sorts of things online. For example, your can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends ' friends, their friends ' friends ' friends, and So on), the has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.

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

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

Input specification

The "a" 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 are 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. A name is a string of 1 to letters (uppercase or lowercase).

Sample Input

1
3
Fred Barney
Barney Betty
Betty Wilma

Output specification

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

Output for Sample Input

2
3
4

Meaning

If A and B are friends, C is a friend of B, then A and B are friends. In this way, a network of Friends is formed, and only one person in this network is the one who knows all the people in the net. Enter some friends relationship, each input a relationship, output two people in the net of the total number of friends

Analysis and Summary:

It is obvious that we are looking at the topic of the collection. The key to this question is to convert people's names into numbers.

This is a very big problem. A place for dad: The input T is also multiple groups, to use while (scanf ("%d", &t)!=eof) {while (t--) {...} }

1. Directly with the STL map to deal with. But the speed is poor: 2.124s (UVa), 625MS (HDU)

* * and check set + map * time:2.124s (UVa), 625MS (HDU) */#include <iostream> #include <map> #include <str  
     
ing> #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 (a!=b) {Father[a] = b;  
    NUM[B] + = Num[a];  
the 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; }

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.