Dating Network. There are a number of data, each set of data to give the N friends network, each dating network to give a M-friendship relationship, ask the dating network there are several people.
Use and check set to do, the idea is to take the right and the idea of the set, the weight of each relationship as 1, each more a person in the relationship between the root node plus 1. The problem to note is that it has a large amount of test data, CIN, cout with too much time out, so in the output to use printf. There is also a person name, so you can use map to create a map.
#include <cstdio> #include <string> #include <map> #include <iostream> using namespace std;
const int MAXN = 100005;
int FA[MAXN], NUM[MAXN];
Map <string, int> Fri;
int find (int x) {return (fa[x] = = x)? x:fa[x] = Find (Fa[x]);}
void Union (int x, int y) {int fx, FY; FX = find (x);
FY = find (y);
if (FX = = FY) {printf ("%d\n", Num[fx]);
return;
} else {fa[fy] = FX;
NUM[FX] + = Num[fy];
printf ("%d\n", Num[fx]);
return;
}} int main () {//freopen ("In.txt", "R", stdin);
int n;
while (CIN >> N) {while (n--) {fri.clear ();
int m, number = 1;
Cin >> m;
if (M = = 0) {cout << "0" << Endl;
Continue;
} while (M-) {string A, B;
Cin >> a >> b; if (!fRi[a]) {Fri[a] = number + +;
Fa[fri[a]] = Fri[a];
Num[fri[a]] = 1;
} if (!fri[b]) {Fri[b] = number + +;
FA[FRI[B]] = fri[b];
NUM[FRI[B]] = 1;
} Union (Fri[a], fri[b]);
}}} return 0;
}