南邮 OJ 1381 Friends

Friends

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 79            测试通过 : 27 

比赛描述

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.




输入

The first line of input contains a single integer specifying the number of test cases to

follow. Each test case begins with a line containing an integer F, the number of

friendships formed, and 0 < F < 100. 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 continuous

string of 1 to 20 letters (uppercase/lowercase differences do not matter).


输出

For each test case, print "Case n", where n is the test case number. In each test case,

whenever a friendship is formed, print a line containing an integer that indicates the

number of people in the social network of the two people who have just become friends.


样例输入

2
3
Fred Barney
Barney Betty
Betty Wilma
2
Ben Sam
Sam Joe

样例输出

Case 1
2
3
4
Case 2
2
3

提示

undefined

题目来源

Internet




/* WA1
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define MAX_N 200
using namespace std;

int fa[MAX_N];
int num[MAX_N];

int getGroupNo(int k){
	return fa[k]==k ? k : fa[k] = getGroupNo(fa[k]);
}

void makeGroup(int i, int j){
	int fi,fj;
	fi = getGroupNo(i);
	fj = getGroupNo(j);
	fa[fi] = fj;
	num[fj] += num[fi];
}

int main(){
	freopen("test.txt","r",stdin);
	int cas,ca,n,i1,i2,no;
	string s1,s2;
	map<string, int> siMap;
	scanf("%d",&cas);
	for(ca=1;ca<=cas;ca++){
		no = 0;
		siMap.clear();
		printf("Case %d\n",ca);
		scanf("%d",&n);
		while(n--){
			cin>>s1>>s2;
			if(siMap.count(s1)){
				i1 = siMap[s1];
			}else{
				i1 = no;
				siMap.insert(make_pair(s1,no));
				fa[no] = no;
				num[no] = 1;
				no++;
			}
			if(siMap.count(s2)){
				i2 = siMap[s2];
			}else{
				i2 = no;
				siMap.insert(make_pair(s2,no));
				fa[no] = no;
				num[no] = 1;
				no++;
			}
			makeGroup(i1,i2);
			printf("%d\n",num[getGroupNo(i2)]);
		}
	}
}
*/

// 15MS
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define MAX_N 300
using namespace std;

int fa[MAX_N];
int num[MAX_N];

int getGroupNo(int k){
	return fa[k]==k ? k : fa[k] = getGroupNo(fa[k]);
}

void makeGroup(int i, int j){
	int fi,fj;
	fi = getGroupNo(i);
	fj = getGroupNo(j);
	if(fi!=fj){					//WA1
		fa[fi] = fj;
		num[fj] += num[fi];
	}
}

void ToLower(string &s){
	int i,len=(int)s.length();
	for(i=0;i<len;i++){
		if(s[i]>='A'&& s[i]<='Z'){
			s[i] += 'a'-'A';
		}
	}
}

int main(){
//	freopen("test.txt","r",stdin);
	int cas,ca,n,i1,i2,no;
	string s1,s2;
	map<string, int> siMap;
	while(scanf("%d",&cas)==1){
		for(ca=1;ca<=cas;ca++){
			no = 0;
			siMap.clear();
			printf("Case %d\n",ca);
			scanf("%d",&n);
			while(n--){
				cin>>s1>>s2;
				ToLower(s1);
				ToLower(s2);
				if(siMap.count(s1)){
					i1 = siMap[s1];
				}else{
					i1 = no;
					siMap.insert(make_pair(s1,no));
					fa[no] = no;
					num[no] = 1;
					no++;
				}
				if(siMap.count(s2)){
					i2 = siMap[s2];
				}else{
					i2 = no;
					siMap.insert(make_pair(s2,no));
					fa[no] = no;
					num[no] = 1;
					no++;
				}
				makeGroup(i1,i2);
				printf("%d\n",num[getGroupNo(i2)]);
			}
		}
	}
}






你可能感兴趣的:(ACM,Friends,南邮OJ)