您的位置:首页 > 其它

HDU 3172 Virtual Friends

2015-06-14 16:47 260 查看
[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

 
思路:这题挺简单的,map+并查集
但是这里比较坑的就是输入
whil e(("%d", &t) != EOF)
{
while (t--)
}
才行,然后wa了很久,才发现我又把Find 函数写错了。。。。打脸

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#define MAX 200005
using namespace std;
int siz[MAX];
int Father[MAX];

int Find (int x)
{
if(Father[x] != x)
{
Father[x] = Find(Father[x]);
}
return Father[x];
}

void Merge(int x, int y)
{
int ix = Find(x);
int iy = Find(y);
if(ix != iy)
{
Father[iy] = ix;
siz[ix] += siz[iy];
}
}

int main()
{
int t,n,cnt,count,root;
char a[25],b[25];
while (scanf("%d",&t) != EOF)
{
while (t--)
{
cnt = count = 0;
map<string,int> s;
scanf("%d",&n);
for(int i = 0; i < MAX; i++)
{
Father[i] = i;
siz[i] = 1;
}
for (int i = 0; i < n; i++)
{
scanf("%s%s",&a,&b);
if(s.find(a) == s.end())
{
cnt++;
s.insert(pair<string,int>(a,cnt));
}
if(s.find(b) == s.end())
{
cnt++;
s.insert(pair<string,int>(b,cnt));
}
Merge(s[a],s[b]);
printf("%d\n",siz[Find(s[a])]);
}
}
}

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