NOJ [1246] Virtual Friends

本文介绍了一种使用并查集算法来维护和更新虚拟社交网络中个人社交圈子规模的方法。通过实例演示了如何处理新增友谊关系,并实时计算受影响个体的社交网络大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 问题描述
  • 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.

    这题题意就是并查集的思想,至于怎么存字符串,刚好学会了map,正好派用场
    逗比的我找错误找了半小时
    #include<stdio.h>
    #include<string.h>
    #include<map>
    #include<iterator>
    using namespace std;
    
    int father[100010];
    int a[100010];
    int find(int x)
    {
       if (father[x] == x) return x;
         father[x]=find(father[x]);
         return father[x];
    }
    
    void unit(int x,int y)
    {
    	int xx=find(x);
    	int yy=find(y);
    	if(xx!=yy)
    	{
    		 father[yy]=xx;
             a[xx]+=a[yy];
    	}
    }
      map<string,int>friends;
    int main()
    {
      int t,n;
     
      while(~scanf("%d",&t))
      {
        while(t--)
        {
          scanf("%d",&n);
          int i,j,cnt=0;
          int p,q;
          friends.clear();
          memset(a,0,sizeof(a));
          memset(father,0,sizeof(father));
          for(i=0;i<n;i++)
          { 
    	     char name1[22],name2[22];
            scanf("%s%s",name1,name2);
            if(friends[name1]==NULL)  //此人没出现过
            { 
    		   cnt++;
              friends[name1]+=cnt;
              father[friends[name1]]=friends[name1];
              a[father[cnt]]=1;
            }
    
            if(friends[name2]==NULL)
            {
    		  cnt++;
              friends[name2]+=cnt;
              father[friends[name2]]=friends[name2];
              a[father[cnt]]=1;
            }
            unit(friends[name1],friends[name2]);
            printf("%d\n",a[find(friends[name1])]);
          }
        }
      }
      return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值