Could someone gives me some I/O?
I got WA...
here is my code, thx
Code: Select all
#include <iostream>
#include <string>
#include <map>
#define MAX_PEOPLE 200001
using namespace std ;
int *group = new int[MAX_PEOPLE],
*table = new int[MAX_PEOPLE];
int check(int x)
{
int root = table[x];
if(root != x)
return (table[x] = check(root));
else
return x;
}
int UNION(int x, int y)
{
int rootA = check(x), rootB = check(y);
if(table[rootA] != table[rootB]){
if(group[rootA] < group[rootB]){
table[rootA] = rootB;
group[rootA] = (group[rootB] += group[rootA]);
}
else{
table[rootB] = rootA;
group[rootB] = (group[rootA] += group[rootB]);
}
}
return group[rootA];
}
int main()
{
int T, F, i, posA, posB, people;
string name1, name2;
map<string, int> hashmap;
cin.sync_with_stdio(false); cout.sync_with_stdio(false);
cin >> T;
while(T){
memset(table, 0, MAX_PEOPLE); memset(group, 0, MAX_PEOPLE);
cin >> F;
people = 0;
for(i=0; i<F; ++i){
cin >> name1 >> name2;
if(hashmap.find(name1) == hashmap.end()){
hashmap[name1] = people;
table[people] = people;
group[people] = 1;
posA = people;
++people;
}
else
posA = hashmap[name1];
if(hashmap.find(name2) == hashmap.end()){
hashmap[name2] = people;
table[people] = people;
group[people] = 1;
posB = people;
++people;
}
else
posB = hashmap[name2];
cout << UNION(posA, posB);
putchar('\n');
}
--T;
}
delete [] group; delete [] table;
return 0;
}