int main(){
    int nTest;
    cin >> nTest;

    while( nTest-- ){
        int f;
        string f1, f2;
        cin >> f;
        parent.clear();
        rank.clear();

        for( int i = 0; i < f; ++i ){
            cin >> f1 >> f2;
            if( parent[f1] == "" )
                parent[f1] = f1;
            if( parent[f2] == "" )
                parent[f2] = f2;
            merge( f1, f2 );
            cout << rank[find(f1)] + 1 << "\n";
        }
    }
}
Example #2
0
int main()
{
	int n;	cin>>n;
	int cntR=0,cntl=0;
	m.clear();
	while(!q.empty())q.pop();
	string s,s1,s2;
	for(int i=0;i<n;i++)
	{
		cin>>s;
		int cr=0;
		for(int i=0;i<s.length();i++) 
		{
			s[i]=tolower(s[i]);
			if (s[i]=='r') cr++;
		}
		//cout<<s<<cr<<endl;
		q.push(MP(s,cr));
	}
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>s1>>s2;
		int cr1=0;
		for(int i=0;i<s1.length();i++)
		{
			s1[i]=tolower(s1[i]);
			if (s1[i]=='r') cr1++;
		}
		int cr2=0;
		for(int i=0;i<s2.length();i++)
		{
			s2[i]=tolower(s2[i]);
			if (s2[i]=='r') cr2++;
		}
		//cout<<s1<<cr1<<s2<<cr2<<endl;
		m.insert(pss(MP(s1,cr1),MP(s2,cr2)));
	}
	while(!q.empty())
	{
		psi tmp=q.front();
		q.pop();
		if(m[tmp].first=="")
		{
			cntl+=tmp.first.length();
			cntR+=tmp.second;
		}
		else
		{
			int cntr1=tmp.second;
			int cntr2=m[tmp].second;
			int len1=tmp.first.length();
			int len2=m[tmp].first.length();
			
			if(cntr2<cntr1) cntR+=cntr2,cntl+=len2;
			else if(cntr2==cntr1) cntR+=cntr2,cntl+=(len1>len2?len2:len1);
			else cntR+=cntr1,cntl+=len1; 
			//cout<<cntr1<<cntr2<<cntR<<endl;
			//cout<<len1<<len2<<cntl<<endl;
		}
	}
	cout<<cntR<<" "<<cntl<<endl;
	return 0;
}