• 1139 First Contact


    Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle in the early years. When a boy A had a crush on a girl B, he would usually not contact her directly in the first place. Instead, he might ask another boy C, one of his close friends, to ask another girl D, who was a friend of both B and C, to send a message to B -- quite a long shot, isn't it? Girls would do analogously.

    Here given a network of friendship relations, you are supposed to help a boy or a girl to list all their friends who can possibly help them making the first contact.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives two positive integers N (1 < N ≤ 300) and M, being the total number of people and the number of friendship relations, respectively. Then M lines follow, each gives a pair of friends. Here a person is represented by a 4-digit ID. To tell their genders, we use a negative sign to represent girls.

    After the relations, a positive integer K (≤ 100) is given, which is the number of queries. Then K lines of queries follow, each gives a pair of lovers, separated by a space. It is assumed that the first one is having a crush on the second one.

    Output Specification:

    For each query, first print in a line the number of different pairs of friends they can find to help them, then in each line print the IDs of a pair of friends.

    If the lovers A and B are of opposite genders, you must first print the friend of A who is of the same gender of A, then the friend of B, who is of the same gender of B. If they are of the same gender, then both friends must be in the same gender as theirs. It is guaranteed that each person has only one gender.

    The friends must be printed in non-decreasing order of the first IDs, and for the same first ones, in increasing order of the seconds ones.

    Sample Input:

    1. 10 18
    2. -2001 1001
    3. -2002 -2001
    4. 1004 1001
    5. -2004 -2001
    6. -2003 1005
    7. 1005 -2001
    8. 1001 -2003
    9. 1002 1001
    10. 1002 -2004
    11. -2004 1001
    12. 1003 -2002
    13. -2003 1003
    14. 1004 -2002
    15. -2001 -2003
    16. 1001 1003
    17. 1003 -2001
    18. 1002 -2001
    19. -2002 -2003
    20. 5
    21. 1001 -2001
    22. -2003 1001
    23. 1005 -2001
    24. -2002 -2004
    25. 1111 -2003

    Sample Output:

    1. 4
    2. 1002 2004
    3. 1003 2002
    4. 1003 2003
    5. 1004 2002
    6. 4
    7. 2001 1002
    8. 2001 1003
    9. 2002 1003
    10. 2002 1004
    11. 0
    12. 1
    13. 2003 2001
    14. 0
    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. using namespace std;
    10. vector<int>g[30010];
    11. bool g2[30010][30010];
    12. bool vis[30010];
    13. int n, m, k, u, v, cnt1, cnt2, ans;
    14. int f1[30010], f2[30010];
    15. string s1, s2;
    16. struct mid {
    17. int p1, p2;
    18. } a[30010];
    19. bool cmp(mid m1, mid m2) {
    20. return m1.p1 == m2.p1 ? m1.p2 < m2.p2 : m1.p1 < m2.p1;
    21. }
    22. int main() {
    23. cin >> n >> m;
    24. for (int i = 0; i < m; i++) {
    25. cin >> s1 >> s2;
    26. if (s1[0] == '-') {
    27. s1.erase(0, 1);
    28. u = 20000 + atoi(s1.c_str());
    29. } else {
    30. u = atoi(s1.c_str());
    31. }
    32. if (s2[0] == '-') {
    33. s2.erase(0, 1);
    34. v = 20000 + atoi(s2.c_str());
    35. } else {
    36. v = atoi(s2.c_str());
    37. }
    38. g[u].push_back(v);
    39. g[v].push_back(u);
    40. g2[u][v] = g2[v][u] = 1;
    41. }
    42. cin >> k;
    43. while (k--) {
    44. cin >> s1 >> s2;
    45. if (s1[0] == '-') {
    46. s1.erase(0, 1);
    47. u = 20000 + atoi(s1.c_str());
    48. } else {
    49. u = atoi(s1.c_str());
    50. }
    51. if (s2[0] == '-') {
    52. s2.erase(0, 1);
    53. v = 20000 + atoi(s2.c_str());
    54. } else {
    55. v = atoi(s2.c_str());
    56. }
    57. memset(vis, 0, sizeof(vis));
    58. cnt1 = 0, cnt2 = 0, ans = 0;
    59. for (int i = 0; i < g[u].size(); i++) {
    60. if ((u + g[u][i] > 40000 || u + g[u][i] < 20000) && g[u][i] != v) {
    61. f1[cnt1++] = g[u][i];
    62. }
    63. }
    64. for (int i = 0; i < g[v].size(); i++) {
    65. if ((v + g[v][i] > 40000 || v + g[v][i] < 20000) && g[v][i] != u) {
    66. f2[cnt2++] = g[v][i];
    67. }
    68. }
    69. for (int i = 0; i < cnt1; i++) {
    70. for (int j = 0; j < cnt2; j++) {
    71. if (g2[f1[i]][f2[j]] && f1[i] != f2[j]) {
    72. a[ans].p1 = f1[i];
    73. a[ans++].p2 = f2[j];
    74. }
    75. }
    76. }
    77. sort(a, a + ans, cmp);
    78. cout << ans << endl;
    79. for (int i = 0; i < ans; i++) {
    80. if (a[i].p1 >= 20000) {
    81. a[i].p1 -= 20000;
    82. }
    83. if (a[i].p2 >= 20000) {
    84. a[i].p2 -= 20000;
    85. }
    86. cout << setw(4) << setfill('0') << a[i].p1 << ' ' << setw(4) << setfill('0') << a[i].p2 << endl;
    87. }
    88. }
    89. return 0;
    90. }

     测试点参考博客:1139 First Contact (30分)--PAT甲级真题(测试点不通过来看看)_SamsonKun的博客-CSDN博客

  • 相关阅读:
    始祖双碳新闻 | 2022年8月18日碳中和行业早知道
    动态规划---图像压缩
    数据库的建立、增、删、改、查
    pandas某一列的某一行 .loc
    kubernetes技术
    Java加载的class无法正常使用的问题
    C++PrimerPlus(第6版)中文版:Chapter16.2智能指针模版类smrtptrs.cpp
    MySQL 的“回表”是什么
    如何下载国外硕博论文?
    机器学习从零到入门 集成学习
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/128018296