并查集 HDU 3172 Virtual Friends
2016-08-10 15:18
477 查看
题目:
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
Sample Output
除了1个数组记录father,还需要1个数组记录这个集合中的元素数目。
num数组并不是每个数都有意义,只有每个集合的根的num才是这个集合的数目,其他绝大部分num都再也没用了。
(貌似数组开小了,应该扩大1倍,不过还是AC了)
代码:
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
除了1个数组记录father,还需要1个数组记录这个集合中的元素数目。
num数组并不是每个数都有意义,只有每个集合的根的num才是这个集合的数目,其他绝大部分num都再也没用了。
(貌似数组开小了,应该扩大1倍,不过还是AC了)
代码:
#include<iostream> #include<map> using namespace std; map<string, int>m; int fa[100005]; int num[100005]; char s[21]; int k1, k2; int t, f, k; map<string, int>::iterator p; int find(int x) //找祖先 { int r = x; while (fa[r] != r)r = fa[r]; while (fa[x] != x) { int f = fa[x]; fa[x] = r; x = f; } return r; } int mapfind(string s) { p = m.find(s); if (p == m.end()) { m.insert(pair<string, int>(s, k)); fa[k] = k; num[k++] = 1; } return find(m.find(s)->second); } int main() { while (cin >> t) { while (t--) { cin >> f; k = 1; m.clear(); while (f--) { cin >> s; k1 = mapfind(s); cin >> s; k2 = mapfind(s); if (k1 != k2) { fa[k1] = k2; num[k2] += num[k1]; } cout << num[k2] << endl; } } } return 0; }
相关文章推荐
- hdu 3172 Virtual Friends (并查集 + 字典树)
- HDU 3172 Virtual Friends(带权并查集,map)
- HDU 3172 Virtual Friends (并查集)
- hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09
- HDU 3172 Virtual Friends (并查集)
- hdu 3172 Virtual Friends(并查集)
- HDU 3172 Virtual Friends (并查集节点统计)
- HDU 3172 并查集-Virtual Friends
- HDU 3172 Virtual Friends(并查集)
- HDU 3172 Virtual Friends (并查集与map的运用,同HDU1829)
- hdu 3172 virtual friends 并查集
- HDU 3172 Virtual Friends 带权并查集 -秩
- hdu 3172 Virtual Friends(简单并查集)
- HDU 并查集 - 3172 Virtual Friends
- HDU - 3172 Virtual Friends(并查集)
- hdu 3172 Virtual Friends(加权并查集)
- hdu 3172 Virtual Friends(并查集,字典树)
- HDU 3172 Virtual Friends(带权并查集)
- hdu 3172 Virtual Friends (并查集)
- HDU 3172 Virtual Friends