题意:给你f个关系,每个关系有两个人名,表示互相认识。
求每一次的输出,输出有多少个人刚刚成为朋友。(如果没有新的朋友加入,则输出原来的人数)
注意:输入格式巨坑,需要这样
while(scanf("%d",&case)!=EOF){
while(case--){}
}
测试数据如下:
1 7 a b a a a c c g g h n n h n ans: 2 2 3 4 5 1 6
遇到了玄学问题(╯﹏╰)
#include<stdio.h>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
#define MAXN 100000+10
int pre[MAXN],num[MAXN];
int n;
int ans=0;
void init(int n){
ans=0;
for(int i=1;i<MAXN;i++){//MAXN 变为 2*n 会 TE 不懂
pre[i]=i;
num[i]=1;
}
}
int find(int x){
if(x==pre[x])
return x;
int px = find(pre[x]);
num[x]+=num[pre[x]];
return pre[x]=px;
}
void merge(int x,int y){
int xx=find(x);
int yy=find(y);
if(xx!=yy){
pre[yy]=xx;
num[xx]+=num[yy];
}
ans=num[xx];
}
map<string,int>mp;
int main(){
int t;
while(scanf("%d",&t)!=EOF){
while(t--){
mp.clear();
scanf("%d",&n);
int id=1;
init();
while(n--){
int a,b;
char str[15],str1[15];
scanf("%s%s",str,str1);
if(mp[str]==0)
mp[str]=id++;
if(mp[str1]==0)
mp[str1]=id++;
merge(mp[str],mp[str1]);
printf("%d\n",ans);
}
}
}
return 0;
}