It seems to be a easy question -- finding out the number of sets where people in the set trust each other
But I am getting WA continuously.
Can anyone help to check my code?
Thanks really much!
Charles
- Code: Select all
#include <iostream>
#include <map>
using namespace std;
map<string, int> People;
map<int, bool> m;
int trust[1001][1001];
int group[1001];
int main() {
int P, T, ppl, ppl2;
string temp, temp2;
while (scanf("%d %d", &P, &T), P || T) {
cin.get();
memset(trust, 0, sizeof(trust));
People.clear();
m.clear();
for (int x = 0; x < P; x++) {
getline(cin, temp);
People[temp] = x;
group[x] = x;
}
for (int x = 0; x < T; x++) {
getline(cin, temp);
getline(cin, temp2);
ppl = People[temp];
ppl2 = People[temp2];
trust[ppl][ppl2] = true;
if (group[ppl2] != group[ppl] && trust[ppl2][ppl]) {
for (int y = 0; y < P; y++)
if (group[y] == group[ppl2] && y != ppl2)
group[y] = group[ppl];
group[ppl2] = group[ppl];
}
}
for (int x = 0; x < P; x++)
m[group[x]] = true;
printf("%d\n", (int)m.size());
}
}
