并查集—Virtual Friends
来源:互联网 发布:商业地产it 编辑:程序博客网 时间:2024/05/18 03:51
**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**
网上交友,计算朋友间都有关系的最多人数
由于string类型的名字无法作为par数组的角标,因此用map为新出现的每一名字附上一个常量序号
又为了在集合上附上集合元素个数的量,因此为集合的根同样用map附上int量来表示
#include<cstdio>#include<iostream>#include<map>#include<string>using namespace std;int par[100005];int cnt;map<string, int> num;map<int, int> sum;void init(){ for (int i = 0; i < 100005; i++) { par[i] = i; sum[i] = 1; } //为节时,两者合并遍历 cnt = 0; num.clear(); //达到所有元素"初始化"为零目的}int find(int i){ /*if (i == par[i]) //与没有进行路径压缩的原代码(超时)对比 return i; return find(par[i]);*/ int r = i; while (r != par[r]) r = par[r]; int j = i; while (j != r) { int x = par[j]; par[j] = r; j = x; } return r;}int unit(string a, string b){ int apar = find(num[a]); int bpar = find(num[b]); if (apar != bpar) { par[apar] = bpar; sum[bpar] += sum[apar]; } return sum[bpar];}int main(){ int T; string a, b; while (scanf("%d", &T) != EOF) //没有!=EOF导致超时!? { while (T--) { init(); int F; scanf("%d", &F); for (int i = 1; i <= F; i++) //此处不能用while(F--)? { cin >> a >> b; //不能用scanf("%s %s",&a,&b)输入string类a b if (!num[a]) num[a] = ++cnt; if (!num[b]) num[b] = ++cnt; cout << unit(a, b) << endl; } } } return 0;}
- 并查集—Virtual Friends
- Virtual Friends 字典树 并查集
- HDU3172 Virtual Friends 并查集
- hdu 3172 virtual friends 并查集
- HDU 3172 Virtual Friends (并查集)
- 【并查集】 HDU 3172 Virtual Friends
- Hdu3172 - Virtual Friends - 并查集
- hdu 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends 并查集
- HDU 并查集 - 3172 Virtual Friends
- 3172 Virtual Friends【并查集】
- HDU 3172 Virtual Friends (并查集)
- hdu3172 Virtual Friends--并查集
- 并查集 HDU 3172 Virtual Friends
- HDU 3172 并查集-Virtual Friends
- HDUoj Virtual Friends (并查集
- Virtual Friends<并查集+map求解>
- Virtual Friends (并查集)
- LintCode 29-交叉字符串
- JavaScript技术书籍和资料
- SQL的集合操作和完整性约束
- 【matlab】震撼!只能用这两个字了,真是奇女子
- Caffe依赖包解析
- 并查集—Virtual Friends
- Spring初始化Bean方法
- 机器学习之文本特征选择
- Ubuntu15安装Docker
- 排序——插入排序
- 无向带权图最小割stoer-wagner算法(poj 2914 Minimum cut)
- 对字符串数字 进行排序操作
- Android中解析XML的三种方式
- 指向指针的指针