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; }
  • 相关阅读:
    postgresql获取随机数
    windows环境中Tomcat实现开机自启动
    让Tomcat支持中文路径名和中文文件名
    CentOS 环境变量编辑、保存、立即生效的方法
    eclipse/intellij idea 远程调试hadoop 2.6.0
    利用Spring的@Async异步处理改善web应用中耗时操作的用户体验
    ssh 免密码设置失败原因总结
    hadoop 2.6伪分布安装
    weblogic.nodemanager.common.ConfigException: Native version is enabled but nodemanager native library could not be loaded 解决办法
    velocity模板引擎学习(3)-异常处理
  • 原文地址:https://www.cnblogs.com/herumw/p/9464808.html
Copyright © 2011-2022 走看看