HDU 3172 Virtual Friends 并查集+map
2016-08-10 20:48
423 查看
B - Virtual Friends
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
题目输入有坑
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
题目输入有坑
#include<stdio.h> #include<string> #include<cstring> #include<queue> #include<algorithm> #include<functional> #include<vector> #include<iomanip> #include<math.h> #include<iostream> #include<sstream> #include<stack> #include<set> #include<map> using namespace std; int pre[100005],sz[100005]; int Find(int x) { int r=x; while(r!=pre[r]) r=pre[r]; int i=x,j; while(pre[i]!=r) { j=pre[i]; pre[i]=r; i=j; } return r; } int mix(int x,int y) { int fx=Find(x),fy=Find(y); if(fx!=fy) { pre[fy]=fx; sz[fx]=sz[fx]+sz[fy]; } return sz[fx]; } int main() { cin.sync_with_stdio(false); int T,N,cnt; map<string,int> M; string a,b; while (cin>>T) { while (T--) { for (int i=0; i<10005; i++) pre[i]=i,sz[i]=1; cnt=1; M.clear(); cin>>N; for (int i=1; i<=N; i++) { cin>>a>>b; if (!M[a]) M[a]=cnt++; if (!M[b]) M[b]=cnt++; cout<<mix(M[a],M[b])<<endl; } } } return 0; }
相关文章推荐
- hdu 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(并查集+map+输入)
- HDU 3172 Virtual Friends(带权并查集,map)
- 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
- HDU 3172 Virtual Friends (并查集节点统计)
- hdu 3172 Virtual Friends
- HDU 并查集 - 3172 Virtual Friends
- HDU 3172 Virtual Friends(并查集)
- 【STL+并查集+卡格式卡时间】HDU 3172——Virtual Friends