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; }
  • 相关阅读:
    error C2065: “SHCNE_DELETE”: 未声明的标识符
    error C2872: ‘ofstream’ : ambiguous symbol
    error C2065: ‘__in’ : undeclared identifier
    LINK : fatal error LNK1561: 必须定义入口点
    VS2010提示error TRK0002: Failed to execute command
    js怎么获取一个单位为rem的元素真实宽/高度?
    将时间转化为几秒前 几分钟前 几小时前...
    uni-app picker 组件实现二级联动?
    uni-app scroll-view 点击实现元素居中?
    初始化CSS样式
  • 原文地址:https://www.cnblogs.com/herumw/p/9464808.html
Copyright © 2011-2022 走看看