HDU - 3172 Virtual Friends

题意:

求每个集合中有多少个人。在线输出。

这个题本本没有什么,,

但是他的输入数据够烦人的。

输入是多组数组,每组数据又有T个数据,

所以输入用该这样写。 

while(scanf("%d",&T) != EOF){

    while(T--)

    init();

}

练容器的题目。

我这个题写了半天,一直TLE,,最后找到了原因,竟然是数组开小了。mmp。

#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#define mem(x,v) memset(x,v,sizeof(x)) 
#define go(i,a,b)  for (int i = a; i <= b; i++)
#define og(i,a,b)  for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;
const double EPS = 1e-10;
const int INF = 0x3f3f3f3f;
const int N = 2e5+10;
int fa[N],n,k,ans,num[N];
map<string,int>mp;
char s[30],t[30];
int find(int k){
	if (fa[k] == k) return k; else return fa[k] = find(fa[k]);
}

void init(){
	mp.clear();
	int x,y,fx,fy;
	scanf("%d",&n);
	go(i,0,2*n) fa[i] = i,num[i] = 1;
	k = 1;
	go(i,1,n){
		scanf("%s%s",s,t);
		if (!mp[s]) mp[s] = k++;
		if (!mp[t]) mp[t] = k++;
		x = mp[s]; y = mp[t];
		fx = find(x); fy = find(y);
		if (fx != fy) fa[fx] = fy,num[fy] += num[fx];
		printf("%d\n",num[fy]);
	} 
}
int main(){
	int T; 
	while(scanf("%d",&T) != EOF){
		while(T--)
		init();
	}


	return 0;
}

猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/81735352