uva 11503 Virtual Friends Solution


uva 11503 Virtual Friends


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
#include<string>

using namespace std;
map<string, int> mymap;
map<string, int> ::iterator mit;

int val[100005], parent[100005];

int findParent(int x){
if (parent[x] == x)
return x;
else
return findParent(parent[x]);
}
int main(){
int i, x, y, n, test;
string str1, str2;
cin >> test;
while (test--){
cin >> n;
for (i = 0; i < 100005; i++){
parent[i] = i;
val[i] = 1;
}
mymap.clear();

for (i = 0; i < n; i++){
cin >> str1 >> str2;
mit = mymap.find(str1);
if (mit == mymap.end()){
x = mymap.size();
mymap[str1] = x;
}
else{
x = mit->second;
}

mit = mymap.find(str2);
if (mit == mymap.end()){
y = mymap.size();
mymap[str2] = y;
}
else{
y = mit->second;
}

x = findParent(x);
y = findParent(y);

if (x != y){
val[x] = val[x] + val[y];
parent[y] = x;
}
cout << val[x] << endl;
}

}
return 0;
}

Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System