NOJ [1246] Virtual Friends

来源:互联网 发布:java fastjson 转数组 编辑:程序博客网 时间:2024/05/16 01:33
  • 问题描述
  • 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 眼睛不小心捅伤怎么办 我的爸爸是小偷怎么办 违停罚款忘记交怎么办 顺风车无人接单怎么办 来例假腰特别疼怎么办 把人撞死了全责怎么办 朝鲜与美合作对付中国怎么办 申请美国大学gpa不够怎么办 武装突袭3有地雷怎么办 辐射4狗肉跟丢了怎么办 洛奇英雄传死绑S怎么办 在老挝遇到坏人带枪怎么办 买了sd卡卡槽塞不下怎么办 现役军人家庭被邻居欺服怎么办 地铁买票买多了怎么办 免税店买的东西转机怎么办 绿能电动车坏了怎么办? 永久单车收不到验证码怎么办 24速山地车档乱了怎么办 新电瓶车被偷了怎么办 汽车前风挡玻璃砸出洞怎么办 凯迪拉克xt5钥匙掉了怎么办 凯迪拉克xt5媒体不好用怎么办 晒黄的白鞋怎么办 白鞋子被晒黄了怎么办? 耐克空军一号白色发黄怎么办 空军一号破皮了怎么办 匡威鞋帆布破了怎么办 脚腕起疙瘩很痒怎么办 跑步后脚踝微疼怎么办 跑步跑得脚疼怎么办 nike air 鞋头脱胶怎么办 耐克空军一号磨脚怎么办 白鞋刷完变黄了怎么办 乔丹气垫坏了怎么办 气垫鞋气垫坏了怎么办 建行u盾忘记密码怎么办 工商银行u盾忘记密码怎么办 民生银行不给u盾怎么办 银行不给开u盾怎么办 有车有空想赚点外快怎么办