/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
map<string,long int> mp ;
long int cnt =0 ;
long int pr[100005] ;
long int frnd[100005] ;
string str1, str2 ;
long int find_r(long int r)
{
if(pr[r]==r)
return r ;
return pr[r]=find_r(pr[r]) ;
}
void disjoint_set(long int n1, long int n2)
{
long int u = find_r(n1) ;
long int v = find_r(n2) ;
if(u!=v)
{
pr[u]=v;
frnd[v]+=frnd[u] ;
pf("%ld\n",frnd[v]) ;
}
else
pf("%ld\n",frnd[v]) ;
}
int main()
{
long int test,n ;
sc("%ld",&test) ;
while(test--)
{
sc("%ld",&n) ;
mp.clear() ;
cnt =0 ;
while(n--)
{
cin>>str1>>str2 ;
if(mp.find(str1)==mp.end())
{
mp[str1] =++cnt ;
pr[cnt] =cnt ;
frnd[cnt] =1 ;
}
if(mp.find(str2)==mp.end())
{
mp[str2] = ++cnt ;
pr[cnt] =cnt ;
frnd[cnt] =1 ;
}
long int n1,n2 ;
n1 =mp[str1] ;
n2 =mp[str2] ;
disjoint_set(n1,n2) ;
}
}
return 0;
}
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
map<string,long int> mp ;
long int cnt =0 ;
long int pr[100005] ;
long int frnd[100005] ;
string str1, str2 ;
long int find_r(long int r)
{
if(pr[r]==r)
return r ;
return pr[r]=find_r(pr[r]) ;
}
void disjoint_set(long int n1, long int n2)
{
long int u = find_r(n1) ;
long int v = find_r(n2) ;
if(u!=v)
{
pr[u]=v;
frnd[v]+=frnd[u] ;
pf("%ld\n",frnd[v]) ;
}
else
pf("%ld\n",frnd[v]) ;
}
int main()
{
long int test,n ;
sc("%ld",&test) ;
while(test--)
{
sc("%ld",&n) ;
mp.clear() ;
cnt =0 ;
while(n--)
{
cin>>str1>>str2 ;
if(mp.find(str1)==mp.end())
{
mp[str1] =++cnt ;
pr[cnt] =cnt ;
frnd[cnt] =1 ;
}
if(mp.find(str2)==mp.end())
{
mp[str2] = ++cnt ;
pr[cnt] =cnt ;
frnd[cnt] =1 ;
}
long int n1,n2 ;
n1 =mp[str1] ;
n2 =mp[str2] ;
disjoint_set(n1,n2) ;
}
}
return 0;
}
No comments:
Post a Comment