1. 程式人生 > >HDU 3172 Virtual Friends(帶權並查集)

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
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

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

HDU - 3038 / 3048 待補

hdu return bsp value 題目 ios i++ mes sca 題目鏈接:點我點我 題意: 題解: 兩題代碼差不多,放個3047的。 1 #include <cstdio> 2 #include <iostream>

HDU 3038 How Many Answers Are Wrong

define pro tor memset set sizeof fin printf class 題目鏈接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1272 題目大意:有n條信息,每條信息都給出區間l到r的值,如果

HDU 3047 Zjnu Stadium,難想到

union blank then submit urn lin lock sts count M - Zjnu Stadium Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d

HDU 3038 How Many Answers Are Wrong

Problem Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play t

HDU 3635 Dragon Balls!!!

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7926    Accepted

HDU3635 Dragon Balls

align size any cat webkit city ret follow num 題目鏈接:   http://acm.hdu.edu.cn/showproblem.php?pid=3635 題目描述: Dragon Balls Problem Descript

——poj2236

algorithm blog oid poj2236 stream 命令 net 最大 spa 題目:Wireless Network 題意:給定n臺已損壞計算機的位置和計算機最遠通信距離d,然後分別根據命令執行以下兩種操作: "O p" (1 <= p <=

POJ 1182 食物鏈

動物 return i++ rip width d+ oid body 思維 食物鏈 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78551 Accepte

poj1703 Find them, Catch them

har find 輸出 scanf -- oot n) puts stdin 題目鏈接 http://poj.org/problem?id=1703 題意 有兩個幫派:龍幫和蛇幫,兩個幫派共有n個人(編號1~n),輸入m組數據,每組數據為D [a][b]或A [a][

poj2492 A Bug's Life

tab set 復雜 鏈接 image 之間 判斷 clu 成了 題目鏈接 http://poj.org/problem?id=2492 題意 蟲子有兩種性別,有n只蟲子,編號1~n,輸入m組數據,每組數據包含a、b兩只蟲子,表示a、b為不同性別的蟲子,根據輸入的m組

poj1182 食物鏈

txt amp 帶權並查集 htm 代碼 || blog 參考 oid 題目鏈接 http://poj.org/problem?id=1182 思路 前面做的帶權並查集的權值記錄該結點與其父結點是否是同一類,只有兩種取值情況(0,1),在這題中某結點a和其父結點b的取

POJ 1988 Cube Stacking

ons from += same file rep stream scribe 並查集 Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes label

HihoCoder - 1515 分數調查

amp code space blog contain script += esp 判斷 小Hi的學校總共有N名學生,編號1-N。學校剛剛進行了一場全校的古詩文水平測驗。 學校沒有公布測驗的成績,所以小Hi只能得到一些小道消息,例如X號同學的分數比Y號同學的分數高S分

食物鏈

clu style org 之間 i++ init esc rip 但是 題目鏈接:http://poj.org/problem?id=1182 題目: Description 動物王國中有三類動物A,B,C,這三類動物的食物鏈構成了有趣的環形。A吃B, B吃C,C吃

HDU1289 A Bug's Life

clu some assume ever different 合並操作 ports main separate HDU1289 帶權並查集 Problem Description Background Professor Hopper is researching the

銀河英雄傳說

lse space std 如果 print code i++ int 維護 題意就是要維護並查集,切詢問兩個點是不是在一棵樹上,如果是,輸出他們間的數減一 #include<iostream> #include<cstdio> using name

How Many Answers Are Wrong

題目: TT and FF are … friends. Uh… very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him.

BZOJ1202:狡猾的商人

else class puts tex algorithm str 修改 任務 scrip 1202: [HNOI2005]狡猾的商人 題目鏈接:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 Descriptio

Cube Stacking 【POJ - 1988】【

題目連結   題意就是給你一串箱子,題目中沒有給出箱子的數量,問你經過一系列移動之後,某個ID箱子下面有幾個箱子,然後移動是把這一整列都按照原來的順序放在另一列的頂端上去。   所以,我想到了用並查集來寫,對於一個箱子,我們把它放到另一列箱子的上面,我們可以維護下