zoukankan      html  css  js  c++  java
  • hust 1259 Virtual Friends

    题目描述

    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.

    输入

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

    输出

    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.

    样例输入

    1
    3
    Fred Barney
    Barney Betty
    Betty Wilma
    

    样例输出

    2
    3
    4

    又是一个并查集的模板题,并查集很牛,但是它不难
    #include<map>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    map<string,int>people;
    string str1,str2;
    struct node
    {
        int root,num;
    }p[100001];
    
    void init()
    {
        people.clear();
        for (int i=0;i<=100000;i++)
        {
            p[i].num=1;
            p[i].root=i;
        }
    }
    
    int find(int x)
    {
        return x==p[x].root?x:p[x].root=find(p[x].root);
    }
    
    int main()
    {
        int n,m,t,x,y;
        cin>>t;
        while(t--)
        {
            init();
            m=1;
            cin>>n;
            for (int i=0;i<n;i++)
            {
                cin>>str1>>str2;
                if (!people[str1]) {people[str1]=m;x=m;m++;}
                else x=people[str1];
                if (!people[str2]) {people[str2]=m;y=m;m++;}
                else y=people[str2];
                x=find(x);
                y=find(y);
                if (x!=y)
                {
                    p[x].root=y;
                    p[y].num+=p[x].num;
                    cout<<p[y].num<<endl;
                }
                else cout<<p[x].num<<endl;
            }
        }
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    some requirement checks failed
    FTP下载文件时拒绝登陆申请怎么办?
    Linux查看与设定别名
    如何编写shell脚本
    Linux shell是什么
    Linux命令大全之查看登陆用户信息
    Linux命令大全之挂载命令
    论第二次作业之输入输出格式怎么合格(才疏学浅说的不对轻点喷我)
    文件词数统计
    软件工程作业--第一周
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3723180.html
Copyright © 2011-2022 走看看