hdu 5.1.7 virtual friend

本文探讨并查集在解决社交网络问题上的应用,详细介绍了如何使用并查集跟踪和计算社交网络中个体的社交圈大小。通过实例分析,展示了输入数据处理、并查集操作及输出结果的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hdu终于正常了……之前的题,都有些记不清了(什么记性啊喂!)

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 54 Accepted Submission(s): 27
 
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建立字符串到数字的映射。map<string,int>p p.find(a) p.end() p.clear,就这些函数。用多了还挺顺手的。

Input file contains multiple test cases. ……害我wa了一次。

#include <iostream>
#include <map>
#include <string>
using namespace std;

int root[100002];
int cnt[100002];//how many friend does (the root) have
int num;
map<string ,int> p;//map, cast string to int

void clear()
{
     int i;
     for(i=0;i<100002;i++)
     {
         root[i]=i;
         cnt[i]=1;
     }
     p.clear();
     num=0;
}

int find(int x)
{
    if(root[x]!=x)
    {
        root[x]=find(root[x]);//update root[]
    }
    return root[x];
}

void merge(int a,int b)
{
     int fa=find(a);
     int fb=find(b);
     if(fa!=fb)
     {
         root[fb]=fa;
         cnt[fa]+=cnt[fb];
     }
     cout<<cnt[fa]<<endl;
}
     
     

int main()
{
    int cas;
    while(cin>>cas)//Input file contains multiple test cases.
    {
    while(cas--)
    {
        int n;
        
        
        clear();
        cin>>n;
        while(n--)
        {
            string a,b;
            cin>>a>>b;
            if(p.find(a)==p.end())//no a in map
            {
                p[a]=++num;//cast a to ++num
            }
            if(p.find(b)==p.end())//no a in map
            {
                p[b]=++num;//
            }
            merge(p[a],p[b]);
        }
    }
    }
    system("pause");
    return 0;
}
            
            
            

二专这周结课,可以周末和米娜(当然主要是队长大人哈哈)一起做题了~可惜本周的两个还是参加不了……残念……
恩,发现上面这种做法现在超时了,感谢   littlesheep_shaun~~

不过,网上找找不超时的方法只有hash表,这,看不懂...

唉...ctrl-c&v

#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 200003
using namespace std;
typedef char Word[21];
Word que[MAXN];

const int HashSize = 1000003;
int head[HashSize], next[MAXN], rear;
int father[MAXN], num[MAXN]; 

inline void init_lookup_table(){
    rear=1; 
    memset(head, 0, sizeof(head));
}

inline int hash(Word &s){
    int seed = 131, v=0;
    for(int i=0; i<strlen(s); ++i){
        v = (v*seed + s[i]);
    }
    return (v & 0x7FFFFFFF)%HashSize;
}

bool try_to_insert(int s){
    int h = hash(que[s]);
    int u = head[h];
    while(u){
        if(strcmp(que[u], que[s])==0) return false;
        u = next[u];
    }
    next[s] = head[h];
    head[h] = s;
    num[s] = 1; // 加入成功后, 该位置的人数变为1
    return true;
}

int search(Word &s){
    int h = hash(s);
    int u = head[h];
    while(u){
        if( strcmp(que[u], s)==0 ) return u;
        u = next[u];
    }
}


void init_unionset(){
    for(int i=0; i<MAXN; i++){
        father[i]=i; num[i] = 0;
    }
}

int find(int x){  
    int i, j=x;
    while(j!=father[j]) j = father[j]; // 找树根
    while(x != j){ // 路径压缩
        i = father[x];
        father[x] = j;
        x = i;
    }
    return j;
}  

void Union(int x,int y){  
    x = find(x);  
    y = find(y);  
    if(x!=y){
        father[y] = x;
        num[x] += num[y];
    }
}


int main(){

    int T, n;
    Word name1, name2;
    while(scanf("%d", &T)!=EOF){

		while(T--){

		    init_lookup_table();
		    init_unionset();

		    scanf("%d",&n);

		    if(n==0) {
		        printf("0\n");
		        continue;
		    }
		    for(int i=0; i<n; ++i){
		        scanf("%s %s",name1, name2);
		        
		        strcpy(que[rear], name1);
		        if(try_to_insert(rear)) ++rear;

		        strcpy(que[rear], name2);
		        if(try_to_insert(rear)) ++rear;

		        int a = search(name1),  b = search(name2);

		        Union(a, b);

		        printf("%d\n",num[find(a)]); 
		    }
		}
    }
    return 0;
}

咕噜咕噜滚走

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值