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摘要
    一步一步学Linq to sql系列文章
    公布一些常用的WebServices
    ASP.NET AJAX入门系列(8):自定义异常处理
    jquery制作图片幻灯片插件
    ASP.NET AJAX入门系列(2):使用ScriptManager控件
    ClientScript.GetCallbackEventReference几个参数的使用实例
    jquery中this的使用说明
    程序员的最后归宿究竟是什么?
    英语26个字母的日语读法
  • 原文地址:https://www.cnblogs.com/herumw/p/9464808.html
Copyright © 2011-2022 走看看