Virtual Friends(并查集+map)

Virtual Friends

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 70   Accepted Submission(s) : 29

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

题意非常简单。

有一个注意的地方就是1 那个是多组输入。。

wa了好多发

#include<iostream>
#include<map>
#include<stdio.h>
using namespace std;
int par[100005],num[100005];
int findi(int x)
{
    if(par[x]==x)
     return x;
    return par[x]=findi(par[x]);
}
void unioni(int x,int y)
{
    int xx=findi(x);
    int yy=findi(y);
    if(xx==yy)
        cout<<num[xx]<<endl;
    else
    {
        par[xx]=yy;
        num[yy]=num[yy]+num[xx];
        cout<<num[yy]<<endl;
    }

}
int main()
{
    int n;
    while(cin>>n)
    {

    map<string,int >ren;
    while(n--)
    {
        ren.clear();
        for(int i=1;i<=100000;i++)
        {
            par[i]=i;
            num[i]=1;
        }
        int m;
        cin>>m;
        string a,b;
        int t=1;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            if(ren[a]==0)
                ren[a]=t++;
            if(ren[b]==0)
                ren[b]=t++;
             int x=ren[a];
             int y=ren[b];
              unioni(x,y);
        }
    }
    }
    return 0;
}
时间: 2024-10-12 11:54:29

Virtual Friends(并查集+map)的相关文章

hdu 3172 Virtual Friends (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入n,给出n行数据,每行有两个字符串,输出关系网络中朋友的个数,n行. 思路: 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int N = 22; const int M = 200020; struct node { int c; node *chil

杭电3172--Virtual Friends(并查集+map)

Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6844    Accepted Submission(s): 1952 Problem Description These days, you can do all sorts of things online. For example, you can u

【日常学习】【并查集+map】codevs2639 约会计划题解

然而我居然让诸城一中悲剧机房的C++可以编译了··· 直接上题目 题目描写叙述 Description cc是个超级帅哥,口才又好.rp极高(这句话似乎降rp),又非常的幽默,所以非常多mm都跟他关系不错. 然而.最关键的是,cc可以非常好的调解各各妹妹间的关系.mm之间的关系及其复杂.cc必须严格掌握她们之间的朋友关系,好一起约她们出去,cc要是和不是朋友的两个mm出去玩.后果不堪设想-- cc仅仅掌握着一些mm之间的关系.可是cc比較聪明.他知道a和b是朋友,b和c 是朋友,那么a和c也是朋

[Swust OJ 772]--Friend(并查集+map的运用)

题目链接:http://acm.swust.edu.cn/problem/772/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 每个人都有朋友,朋友也有很多种,比如: 石友--情谊坚贞的朋友. 挚友--志同道合的朋友. 益友--于己有帮助的朋友. 网友--在互联网结识的朋友. 闺友--闺房中无话不谈的朋友. 君子交:指道义之交,即在道义上相互支持的朋友. 竹马之交:少年时骑竹马为戏的朋友,指自幼相交的朋友,等等. 现在dear

hdu 3172 并查集+map

/*这里将fa[]数组初始化为-1比较方便 输入格式有点坑 看的讨论*/ 1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 #include "map" 7 #include "string" 8 #includ

NBUT 1451 Elise (map +并查集)

[1451] Elise 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Elise is the Spider Queen. She has a skill, Spider Form(蜘蛛形态). When she transformed to the spider, there will be some small spiders around her. But she has a problem - the small spiders will have infighti

HDU 3172 Virtual Friends (map+并查集)

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), h

(hdu step 5.1.6)Virtual Friends(在结点为名字结点的条件下,求并查集的节点数)

题目: Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 329 Accepted Submission(s): 98   Problem Description These days, you can do all sorts of things online. For example, you can use

HDU 3172 Virtual Friends 带权并查集 -秩

ll T; while(~scanf("%d",&T)){ while(T--) { = = ... 思路: 用秩合并,看了题解才发现 if(fx == fy)要输出当前集合的秩而不是0... #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <map&