HDU3172 UVA11503 Virtual Friends【并查集】

708 篇文章 19 订阅
48 篇文章 1 订阅

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9768    Accepted Submission(s): 2855


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
 

Source


问题链接:HDU3172 UVA11503 Virtual Friends

问题简述

  一个计算朋友圈大小的问题,参见上述链接。

问题分析

  这是一个有关图的连通性问题,可以用并查集来解决。并查集中,连通的各个结点都会指向相同的根。相互连通的子图,其结果之和存储在根结点中。

程序说明

  程序中,构建一个用并查集,使得相互连通的子图指向相同的根。该程序与其他程序略有不同的地方是,并查集用数组来存储(可以用向量vector来存储的),目的是加快计算时间。另外由于该问题的测试用例多,输出结果多,需要用C语言的输入输出方式,才能够加快速度,以免超时。同样,输出也合并到并查集中,以提高计算速度。

  代码不够简洁,又写了一个简洁版。


AC的C++语言程序(简洁版)如下:

/* HDU3172 UVA11503 Virtual Friends */

#include <iostream>
#include <map>
#include <string>
#include <stdio.h>

using namespace std;

const int N = 100000;

int ans[N + 1];

int f[N + 1];
void UFInit(int n)
{
    for(int i = 1; i <=n; i++)
        f[i] = i;
}
int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}
void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
        ans[b] += ans[a];
        printf("%d\n", ans[b]);
    } else
        printf("%d\n", ans[b]);
}

int main()
{
    int t, f, no, pno1, pno2;
    char name1[30], name2[30];

    while(scanf("%d", &t) != EOF) {
        while(t--) {
            map<string, int> m;
            UFInit(N);

            scanf("%d", &f);
            for(int i=0; i<=N; i++)
                ans[i] = 1;

            no = 0;
            for(int i=1; i<=f; i++) {
                scanf("%s%s", name1, name2);

                // 将名字放入map中,并将名字映射为整数
                if(!(pno1 = m[name1]))
                    m[name1] = (pno1 = ++no);
                if(!(pno2 = m[name2]))
                    m[name2] = (pno2 = ++no);

                // 合并,同时输出结果
                Union(pno1, pno2);
            }
        }
    }

    return 0;
}



AC的C++语言程序如下:

/* HDU3172 UVA11503 Virtual Friends */

#include <iostream>
#include <map>
#include <string>

using namespace std;

const int MAXN = 100000;

int v[MAXN+1];
int ans[MAXN+1];

// 并查集类
class UF {
private:
    int length;
public:
    UF(int n) {
        length = n;
        for(int i=0; i<=n; i++)
            v[i] = i;
    }

    int Find(int x) {
        for(;;) {
            if(v[x] != x)
                x = v[x];
            else
                return x;
        }
    }

    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if(x == y) {
            printf("%d\n", ans[y]);
            return false;
        } else {
            v[x] = y;
            ans[y] += ans[x];
            printf("%d\n", ans[y]);
            return true;
        }
    }

    void reset() {
        for(int i=0; i<=length; i++)
            v[i] = i;
    }
};

int main()
{
    int t, f, no, pno1, pno2;
    char name1[30], name2[30];
    map<string, int> m;
    UF uf(MAXN+1);

//    while(cin >> t) {
    while(scanf("%d", &t) != EOF) {
        while(t--) {
//            cin >> f;
            scanf("%d", &f);

            for(int i=0; i<=MAXN; i++)
                ans[i] = 1;

            m.clear();
            uf.reset();
            no = 0;
            for(int i=1; i<=f; i++) {
//                cin >> name1 >> name2;
                scanf("%s%s", name1, name2);

                // 将名字放入map中,并将名字映射为整数
                if(!(pno1 = m[name1]))
                    m[name1] = (pno1 = ++no);
                if(!(pno2 = m[name2]))
                    m[name2] = (pno2 = ++no);

                // 合并,同时输出结果
                uf.Union(pno1, pno2);
            }
        }
    }

    return 0;
}

/*
测试实例:
1
6
Fred Barney
Barney Betty
Betty Wilma
AAAAA BBBBB
AAAA  BBBBB
AAAA Fred
输出:
2
3
4
2
3
7
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值