zoukankan      html  css  js  c++  java
  • Virtual Friends HDU

    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. 

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

    题意:找朋友,输出相应的朋友数量
    思路:就是并查集加个秩,在用map存一下,方便输出
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<map>
    #include<set>
    #include<vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define eps 1e-10
    const int maxn=100100;
    map<string,int>p;
    int pre[maxn],ran[maxn],maxx;
    void init()
    {
        int i;
        for(i=1;i<maxn;++i)
        {
            pre[i] = i;
            ran[i]=1;
        }
    }
    
    int find(int x)
    {
        if(x!=pre[x])
        {
            pre[x] = find(pre[x]);
        }
        return pre[x];
    }
    
    void combine(int x,int y)
    {
        int fx = find(x);
        int fy = find(y);
        if(fx!=fy)
        {
            pre[fx] = fy;
            ran[fy] += ran[fx];
        }
    }
    
    
    int main()
    {
       
        int n,m;
        char name[30],na[30];
        while(scanf("%d",&n)!=EOF)
        {
            while(n--)
            {
            p.clear();
            init();
            int k=1;
                scanf("%d",&m);
            for(int i=0;i<m;i++)
            {
                scanf("%s %s",name,na);
                if(p.find(name)==p.end())
                    p[name]=k++;
                if(p.find(na)==p.end())
                    p[na]=k++;
                combine(p[name],p[na]);
                printf("%d
    ",ran[find(p[na])]);
            }
            }
        }
        return 0;
    }
  • 相关阅读:
    架设某大型网站服务器之全部过程
    利用js实现页面关闭时发送http请求
    用jquery解析JSON数据的方法
    在创业公司工作四年,如何赚百万
    一个http请求的详细过程
    VIM 查找替换命令的使用
    swfobject
    获取并显示某目录下的图片
    Windows下架设Subversion服务器
    OA系统概念(办公自动化系统)
  • 原文地址:https://www.cnblogs.com/smallhester/p/9500334.html
Copyright © 2011-2022 走看看