HDU 3172 Virtual Friends
2018-03-04 22:10
344 查看
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10023 Accepted Submission(s): 2930
[align=left]Problem Description[/align]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.
[align=left]Input[/align]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).
[align=left]Output[/align]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.
[align=left]Sample Input[/align]1
3
Fred Barney
Barney Betty
Betty Wilma
[align=left]Sample Output[/align]2
3
4题解:
另开一个ran数组,保存每个节点的子节点数量。
代码:
#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
int par[200007],ran[200007];
void init()
{
for(int i=0;i<200007;i++)
par[i]=i,ran[i]=1;//开始都只有本身
}
int find(int x)
{
if(x==par[x])return x;
int t=par[x];
par[x]=find(par[x]);
ran[par[x]]+=ran[x];
ran[x]=0;//此前以x为父节点的点都直接压缩到了par[x],因此ran[x]=0
return par[x];
}
void unite(int x,int y)
{
int x1=find(x),y1=find(y);
if(x1!=y1)
{
par[x1]=y1;
ran[y1]+=ran[x1];
ran[x1]=0;
}
}
int main()
{
int T;
while(~scanf("%d",&T))
{
while(T--)
{
int n;
init();
scanf("%d",&n);
int k=0;
for(int i=1;i<=n;i++)
{
string t1,t2;
cin>>t1>>t2;
if(!mp[t1])mp[t1]=++k;
if(!mp[t2])mp[t2]=++k;
unite(mp[t1],mp[t2]);
int x=find(mp[t1]);
printf("%d\n",ran[x]);
}
mp.clear();
}
}
return 0;
}
相关文章推荐
- HDU 3172 Virtual Friends 并查集+字典树
- hdu 3172 Virtual Friends(map)
- hdu 3172 Virtual Friends(简单并查集)
- hdu 3172 Virtual Friends(加权并查集)
- hdu 3172 Virtual Friends (并查集)
- HDU-3172 Virtual Friends 并查集+map
- 并查集 HDU 3172 Virtual Friends
- hdu 3172 Virtual Friends(并查集,字典树)
- HDU 3172 Virtual Friends (并查集)
- 【STL+并查集+卡格式卡时间】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(带权并查集,map)
- HDU - 3172 Virtual Friends(并查集)
- (HDU - 3172)Virtual Friends
- HDU 3172 Virtual Friends(并查集)
- 【 HDU 3172 Virtual Friends】 并查集+map指针优化
- 文章标题 HDU 3172 : Virtual Friends (并查集+map)