链接:http://xcacm.hfut.edu.cn/problem.php?id=1234
以后关于字符的输入都用cin吧,换成scanf居然wa了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#define Max 10005
using namespace std;
int pre[Max],Rank[Max];
int sum;
void init()
{
for(int i=;i<Max;i++)
pre[i]=i;
return;
}
int find(int x)
{
if(pre[x]==x)
return x;
return pre[x]=find(pre[x]);
}
int unite(int a,int b)
{
int x=find(a);
int y=find(b);
if(Rank[x]==Rank[y])
return ;
sum--;
if(Rank[x]<Rank[y])
pre[y]=x;
else
pre[x]=y;
return ;
}
int main()
{
int n,m;
char ch;
string s,a,b;
map<string,int> u;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n)!=EOF)
{
sum=n;
u.clear();
init();
for(int i=;i<n;i++)
{
cin>>s;
u[s]=i+;
Rank[i+]=i+;
}
cin>>m;
for(int i=;i<m;i++)
{
cin>>ch;
if(ch=='a')
{
cin>>a>>b;
unite(u[a],u[b]);
}
else if (ch=='q')
{
cin>>a;
int r=find(u[a]);
printf("%d\n",Rank[r]);
}
else if (ch=='t')
printf("%d\n",sum);
}
}
return ;
}