您的位置:首页 > 其它

hdu 3172 Virtual Friends(map)

2015-07-24 10:46 330 查看

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6529 Accepted Submission(s): 1845



[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
题意:先给出几个样例,再给出你组操作,每组操作两个人是朋友,操作完后输出每组操作后
朋友圈里包含几个人
C++ 的map容器在这比较好用,学习了,一下第一次用,虽然错了几次,还是比较开心的#=_=#
这题的输入真坑,,,
2015,7,24

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#define M 200000+200
using namespace std;
int f[M],num[M];
char s[M][23],c[M][23];
void init()
{
for(int i=0;i<M;i++)
{
f[i]=i; num[i]=1;//记录人数,开始都是1个人
}
}
int Find(int k)
{
if(f[k]!=k)
f[k]=Find(f[k]);
return f[k];
}
int merge(int a,int b)
{
int x=Find(a);
int y=Find(b);
if(x!=y)
{
num[y]+=num[x];//num数组记录父节点下有几个子节点,就是记录人数,
f[x]=y;
}
return num[y];
}
int main()
{
int t,n,v,i,j;
while(~scanf("%d",&t))
{
while(t--)
{
v=1;
init();
map<string,int> m;//定义map容器
m.clear();//清空容器
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s",s[i],c[i]);
if(!m[s[i]])	m[s[i]]=v++;//如果这个人没有标过号,就把他标号
if(!m[c[i]])	m[c[i]]=v++;
}
for(j=0;j<n;j++){
printf("%d\n",merge(m[s[j]],m[c[j]]));;
}
}
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: