HDU 3172 Virtual Friends(带权并查集)

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7434    Accepted Submission(s): 2133


Problem Description
These days, you can do all sorts of things online. For example, you 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), 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 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 by 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
   
   
   
   
1 3 Fred Barney Barney Betty Betty Wilma
 

Sample Output
   
   
   
   
2 3 4
 

Source
University of Waterloo Local Contest 2008.09
 

Recommend
chenrui   |   We have carefully selected several similar problems for you:   3038  3234  3047  2818  1558 
这道题比较难处理的一个地方是需要处理的数据是字符串,这里看别人用了一个STL中的一个映射 map是STL中的一个,映照容器,包含在#include<map>中,string是键值,int是数据,如果是map["happy"]=6,string str="happy",那么map[str]=6;
#include<stdio.h>  
#include<map>//注意  
#include<string>  
using namespace std;  
int pre[100005],num[100005];  
map<string,int>m;//STL中的一个映照容器,第一次接触  
void init()  
{  
    int i;  
    for(i=0;i<=100005;i++)  
    {  
        pre[i]=i;  
        num[i]=1;                 
    }       
}  
int find(int x)  
{  

   if(x==pre[x])
   return x;
   return pre[x]=find(pre[x]); 
}  
void merge(int x,int y)  
{  
    int fx,fy;  
    fx=find(x);  
    fy=find(y);  
    if(fy!=fx)  
    {  
        pre[fx]=fy;  
        num[fy]+=num[fx];  
        printf("%d\n",num[fy]);  
    }  
    else  
    {  
        printf("%d\n",num[fy]);  
    }  
      
}  
int main()  
{  
    int t;  
    while(scanf("%d",&t)!=EOF)//坑   
    {  
          
        while(t--)  
        {  
            int n;  
            char a[200],b[200];  
            init();  
            m.clear(); //清空 
            scanf("%d",&n);  
            int ans=1;  
            while(n--)  
            {  
                scanf("%s %s",a,b);  
                if(!m[a])  
                {  
                    m[a]=ans++;  
                }  
                if(!m[b])  
                {  
                    m[b]=ans++;  
                }  
                merge(m[a],m[b]);  
            }  
        }  
    }  
}  


你可能感兴趣的:(HDU 3172 Virtual Friends(带权并查集))