//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 100010
#define MOD 1000000007
int n,m,p[MAX],ht[MAX],sz[MAX];
map<string,int> mm;
int findp(int x){
return (p[x]==x)?x:findp(p[x]);
}
void unionset(int x,int y){
if(findp(x)!=findp(y)){
x=findp(x);y=findp(y);
if(ht[x]>ht[y]){
p[y]=x;
sz[x]+=sz[y];
}
else{
p[x]=y;
sz[y]+=sz[x];
if(ht[x]==ht[y])
ht[y]++;
}
}
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string pp,q;
si(t);
while(t--){
mm.clear();
si(n);
c=1;
clr(p,0);
clr(ht,0);
clr(sz,0);
f(i,1,MAX){
p[i]=i;
sz[i]=1;
}
f(i,0,n){
cin>>pp>>q;
if(mm.find(pp)==mm.end())
mm[pp]=c++;
if(mm.find(q)==mm.end())
mm[q]=c++;
unionset(mm[pp],mm[q]);
x=mm[pp];y=mm[q];
printf("%d\n",max(sz[findp(mm[pp])],sz[findp(mm[q])]));
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 100010
#define MOD 1000000007
int n,m,p[MAX],ht[MAX],sz[MAX];
map<string,int> mm;
int findp(int x){
return (p[x]==x)?x:findp(p[x]);
}
void unionset(int x,int y){
if(findp(x)!=findp(y)){
x=findp(x);y=findp(y);
if(ht[x]>ht[y]){
p[y]=x;
sz[x]+=sz[y];
}
else{
p[x]=y;
sz[y]+=sz[x];
if(ht[x]==ht[y])
ht[y]++;
}
}
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string pp,q;
si(t);
while(t--){
mm.clear();
si(n);
c=1;
clr(p,0);
clr(ht,0);
clr(sz,0);
f(i,1,MAX){
p[i]=i;
sz[i]=1;
}
f(i,0,n){
cin>>pp>>q;
if(mm.find(pp)==mm.end())
mm[pp]=c++;
if(mm.find(q)==mm.end())
mm[q]=c++;
unionset(mm[pp],mm[q]);
x=mm[pp];y=mm[q];
printf("%d\n",max(sz[findp(mm[pp])],sz[findp(mm[q])]));
}
}
return 0;
}
No comments:
Post a Comment