1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include<iostream> #include<map> using namespace std; map<string, string> leader;
string input[] = { "周芷若","张无忌", "张无忌","韩小昭", "成昆","陈友谅", "杨逍","纪晓芙", };
string test[] = { "周芷若","韩小昭", "张无忌","成昆", };
void setLeader() { int i = 1; int totalPerson = 8; for (i = 0;i < totalPerson; i++) { leader[input[i]] = input[i]; } }
string findLeader(string s) { string r = s; while (leader[r] != r) { r = leader[r]; } return r; }
void uniteSet(string leaderX, string leaderY) { leader[leaderX] = leaderY; }
int main() { int numberOfSets = 7; setLeader();
int i = 0; int j = 0; int n = 4; for (j = 0;j < n;j++) { string u = input[i++]; string v = input[i++];
u = findLeader(u); v = findLeader(v);
if (u != v) { uniteSet(u, v); numberOfSets--; } } i = 0; n = 2; for (j = 0;j < n;j++) { string u = test[i++]; string v = test[i++];
u = findLeader(u); v = findLeader(v);
if (u != v) { cout << "NO" << endl; } else { cout << "YES" << endl; } } cout << numberOfSets << endl; return 0; }
|