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

    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
    这题可以用并查集+map来做,注意可能输入的两个字符串相同。
    #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #include<string> using namespace std; int pre[100010],num[100010]; int find(int x) { int i,j=x,r=x; while(r!=pre[r])r=pre[r]; while(j!=pre[j]){ i=pre[j]; pre[j]=r; j=i; } return r; } int main() { int T,n,m,i,j,index,a,b,t1,t2,sum; char s1[30],s2[30]; while(scanf("%d",&T)!=EOF) {while(T--) { map<string,int>hash; hash.clear(); index=1; scanf("%d",&n); for(i=1;i<=100005;i++){ pre[i]=i;num[i]=1; } for(i=1;i<=n;i++){ scanf("%s%s",s1,s2); if(hash[s1]==0)hash[s1]=index++; if(hash[s2]==0)hash[s2]=index++; t1=find(hash[s1]); t2=find(hash[s2]); if(t1==t2)printf("%d ",num[t1]); else{ pre[t1]=t2; num[t2]=num[t2]+num[t1]; printf("%d ",num[t2]); } } } } return 0; }
  • 相关阅读:
    jQuery+ThinkPHP+Ajax实现即时消息提醒功能
    依赖注入(DI)
    控制反转(IoC)
    ajax以base64上传图片到django
    python 多进程、多线程、协程
    python3 实现tcp/udp局域网通信
    同源策略与跨域访问
    nginx+uwsgi阿里云ubuntu服务器上简单部署flask RESTful风格项目
    python3 实现 websocket server 解决中文乱码
    python IO 多路复用 epoll简单模型
  • 原文地址:https://www.cnblogs.com/herumw/p/9464808.html
Copyright © 2011-2022 走看看