• pat甲级1139


    1139 First Contact(30 分)

    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:

    10 18
    -2001 1001
    -2002 -2001
    1004 1001
    -2004 -2001
    -2003 1005
    1005 -2001
    1001 -2003
    1002 1001
    1002 -2004
    -2004 1001
    1003 -2002
    -2003 1003
    1004 -2002
    -2001 -2003
    1001 1003
    1003 -2001
    1002 -2001
    -2002 -2003
    5
    1001 -2001
    -2003 1001
    1005 -2001
    -2002 -2004
    1111 -2003
    

    Sample Output:

    4
    1002 2004
    1003 2002
    1003 2003
    1004 2002
    4
    2001 1002
    2001 1003
    2002 1003
    2002 1004
    0
    1
    2003 2001
    0

    1、用printf("%04d", id)来输出四位id。

    2、用邻接矩阵来存储是否相邻,用邻接表来存储每一个顶点的邻接点,否则最后一个测试点会超时。

    3、输入id时要用字符串输入,因为-0000,0000如果输入到整型都为0,无法判断性别。

     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 bool gender[10000], G[10000][10000];
     8 vector < vector<int>> G2(10000);
     9 struct bridge
    10 {
    11     int f1, f2;
    12 };
    13 
    14 bool cmp(bridge a, bridge b)
    15 {
    16     if (a.f1 != b.f1) return a.f1 < b.f1;
    17     return a.f2 < b.f2;
    18 }
    19 
    20 int main()
    21 {
    22     int N, M, K;
    23     cin >> N >> M;
    24     int i, v, w;
    25     string s1, s2;
    26     for (i = 0; i < M; i++)
    27     {
    28         cin >> s1 >> s2;
    29         v = abs(stoi(s1));
    30         w = abs(stoi(s2));
    31         if (s1[0] == '-') gender[v] = true;
    32         if (s2[0] == '-') gender[w] = true;
    33         G[v][w] = true;
    34         G[w][v] = true;
    35         G2[v].push_back(w);
    36         G2[w].push_back(v);
    37     }
    38     cin >> K;
    39     for (i = 0; i < K; i++)
    40     {
    41         scanf("%d%d", &v, &w);
    42         v = abs(v);
    43         w = abs(w);
    44         vector<bridge> vec;
    45         for (int c : G2[v])
    46         {
    47             if (c == w) continue;
    48             if (!G[v][c] || (gender[c] != gender[v])) continue;
    49             for (int d : G2[c])
    50             {
    51                 if (d == v) continue;
    52                 if (G[d][c] && G[d][w] && gender[d] == gender[w])
    53                 {
    54                     bridge b;
    55                     b.f1 = c;
    56                     b.f2 = d;
    57                     vec.push_back(b);
    58                 }
    59             }
    60         }
    61         sort(vec.begin(), vec.end(), cmp);
    62         printf("%d
    ", vec.size());
    63         for (bridge b : vec)
    64             printf("%04d %04d
    ", b.f1, b.f2);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    C#与C++中struct和class的小结
    C#中string的小结
    树的一些操作——遍历,前序和中序建立后续
    一个快速、高效的Levenshtein算法实现——代码实现
    整数拆分
    阶乘结果中0的个数
    普莱菲尔密码矩阵生成算法
    CTF密码学总结
    盲文对照表
    实验吧-古典密码
  • 原文地址:https://www.cnblogs.com/lxc1910/p/9566954.html
Copyright © 2020-2023  润新知