hdoj 3172 Virtual Friends 【并查集 + STL map】
来源:互联网 发布:淘宝反作弊模型 编辑:程序博客网 时间:2024/06/08 13:24
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6154 Accepted Submission(s): 1741
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.
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).
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
13Fred BarneyBarney BettyBetty Wilma
Sample Output
234
题意:给出n组关系,每组关系有两个人的名字,表示这两个人是朋友。 遵循原则:你的朋友的朋友也是你的朋友。需要你在输入每组关系的同时 输出在这对关系的前提下有多少个人刚刚成为朋友。
并查集应用,就是每次判断某个人是否出现有些麻烦。暴力的话时间复杂度太高,我用的是map来建立 字符串与它出现序号的 映射。 字典树可以做的,抽个时间补上。
注意:输入t时要处理到文件结束!!!
ac代码:
#include <cstdio>#include <cstring>#include <map>#include <string>#define MAX 200000+1using namespace std;int set[MAX], fri[MAX];//set 存储父节点 fri 记录朋友数目 map<string, int> rec;void init(){for(int i = 1; i < MAX; i++){set[i] = i;fri[i] = 1;//初始自己一个朋友 } } int find(int p){int t;int child = p;while(p != set[p])p = set[p];while(child != p){t = set[child];set[child] = p;child = t;}return p;}int merge(int x, int y){int fx = find(x);int fy = find(y);if(fx != fy){//fy 当作父节点 set[fx] = fy;fri[fy] += fri[fx];}return fri[fy];}int main(){int t;int n;char a[30], b[30];while(scanf("%d", &t) != EOF){while(t--) { scanf("%d", &n); init(); int num = 1; rec.clear(); while(n--) { scanf("%s%s", a, b); if(!rec[a]) rec[a] = num++; if(!rec[b]) rec[b] = num++; printf("%d\n", merge(rec[a], rec[b])); } }}return 0;}
0 0
- hdoj 3172 Virtual Friends 【并查集 + STL map】
- HDOJ 3172 Virtual Friends(并查集 + STL)
- Virtual Friends<并查集+map求解>
- HDU-3172 Virtual Friends 并查集+map
- hdu 3172 Virtual Friends(并查集+map)
- HDU 3172 Virtual Friends(带权并查集,map)
- 杭电 3172 Virtual Friends【并查集 + map容器】
- hdu 3172 Virtual Friends 【并查集+map】
- hdu 3172 Virtual Friends (map+并查集)
- HDU 3172 Virtual Friends 并查集+map
- HDU 3172 Virtual Friends(并查集+map+输入)
- hdu 3172 virtual friends 并查集
- HDU 3172 Virtual Friends (并查集)
- 【并查集】 HDU 3172 Virtual Friends
- hdu 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends 并查集
- HDU 并查集 - 3172 Virtual Friends
- 3172 Virtual Friends【并查集】
- LeetCode77:Combinations
- ubuntu 14.10 安装android NDK
- ubuntu下opencl简单教程
- waqs后台服务设计原则
- iOS开发网络篇—HTTP协议
- hdoj 3172 Virtual Friends 【并查集 + STL map】
- 搭建Zookeeper与Hbase过程及遇到的问题总结
- waqs如何在服务转换中使用事务
- Platt SMO算法
- Mac OSX 安装Maven 解决java_home无法识别的问题
- 1022. Digital Library (30)
- iOS编程:学习篇(二)
- DynamicsAX开发中的各种陷阱(持续更新中。。。)
- 约瑟夫环的推导