Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7362 Accepted Submission(s): 2115
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
1
3
Fred Barney
Barney Betty
Betty Wilma
Sample Output
2
3
4
1
3
Fred Barney
Barney Betty
Betty Wilma
Sample Output
2
3
4
题目大意:给出那么些个关系,他们两个能够做朋友,问他俩所在朋友圈里边一共有多少朋友。
思路:并查集、map
关键语句:
Input file contains multiple test cases.
The first line of each case indicates the number of test friendship nest
多组数据,每行首先先输入一个t,代表这组数据有t个小数据。
TLE解决方案:fastIO,路径压缩。
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
using namespace std;
int f[100000];
int j[100000];
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int x,int y)//y
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
f[xx]=yy;
j[yy]+=j[xx];
}
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
for(int i=1;i<=10020;i++)
{
f[i]=i;
j[i]=1;
}
map<string,int >mp;
mp.clear();
int cont=1;
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
char a[50];
char b[50];
scanf("%s%s",a,b);
if(mp[a]==0)
{
mp[a]=cont;
cont++;
}
if(mp[b]==0)
{
mp[b]=cont;
cont++;
}
int x=mp[a];
int y=mp[b];
merge(x,y);
printf("%d\n",j[find(x)]);
}
}
}
}