(hdu step 5.1.6)Virtual Friends(在结点为名字结点的条件下,求并查集的节点数)


题目:

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 329 Accepted Submission(s): 98
 
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
University of Waterloo Local Contest 2008.09
 
Recommend
chenrui


题目大意:

               如果A和B是朋友,那么这个圈子里有2个人,如果这时B和C又是朋友,那么这时候这个圈子里面就有3个人了。


题目分析:

              并查集。简单题。唯一需要注意的是,要为名字结点建立一个索引,转换成梳子节点来做就行了。



代码如下:

/*
 * f.cpp
 *
 *  Created on: 2015年3月3日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <map>

using namespace std;

const int maxn = 100001;
int father[maxn];
int nums[maxn];//并查集的节点数.nums[i]=a表示以i为根节点的并查集的节点数是a


/**
 * 初始化
 */
void init() {
	int i;
	for (i = 1; i < maxn; ++i) {//遍历所有的结点
		father[i] = i;//所有节点的父亲默认是他自己
		nums[i] = 1;//以i为根结点的并查集的节点数默认为1
	}
}

int find(int a) {
	if (a == father[a]) {
		return a;
	}

	return father[a] = find(father[a]);
}

void join(int a, int b) {
	int fa = find(a);
	int fb = find(b);

	if (fa != fb) {

		father[fa] = fb;

		//求并查集的节点数的关键代码
		nums[fb] += nums[fa];//将以fa为根节点的并查集的节点数加到以fb为根节点的并查集上去
		nums[fa] = 0;//以fa为根节点的并查集的节点数置为0

	}
}

int main() {
	int t;
	while (scanf("%d", &t) != EOF) {
		while (t--) {
			init();
			map<string, int> mp;//用于为名字结点建立一个索引,转换成数字节点
			int index = 1;

			int n;
			scanf("%d", &n);

			int i;
			for (i = 1; i <= n; ++i) {
				string a;
				string b;

				cin >> a >> b;


				if (mp.find(a) == mp.end()) {//如果map中还没有该名字
					mp[a] = index++;//则为该名字建立一个索引
				}

				if (mp.find(b) == mp.end()) {
					mp[b] = index++;
				}

				join(mp[a], mp[b]);

				printf("%d\n", nums[find(mp[b])]);
			}
		}
	}

	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值