PAT 甲级 1139 First Contact (30分)

博主 996 2020-07-03

题目链接

Description
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.题目要求分别输出A and B的朋友,同时因为输出的朋友一定为同性朋友,所以只需要记录同性朋友的id。
2.需要找到的C和D必须是朋友关系,无论男女都需要记录为朋友关系,将输入的M条记录都记录为朋友,由于题设id都为4位数,通过id1*10000+id2可构造一个唯一的label,通过map记录 [朋友的label] -> [是否为朋友(true or false)] 的映射。
3.将每一条查询得到的满足要求的记录按输出要求排序输出即可。

坑点:
1.可能存在 -0000 的输入,表示id为0000的女生

code:

#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn = 10010;
unordered_map<int, bool> isFriend;//记录二人为朋友 通过(id1*10000 + id2) -> true的方式
vector<int > sameSexFriend[maxn];//记录同性朋友的编号

struct node{
    int id1;
    int id2;
};

bool cmp(node a, node b){
    if (a.id1 != b.id1) {
        return a.id1 < b.id1;
    }else return a.id2 < b.id2;
}

int main(){
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < M; ++i) {
        string s1, s2;
        cin >> s1 >> s2;
        int flag1 = (s1[0] == '-' ? -1 : 1);//-1表示女 1表示男
        int flag2 = (s2[0] == '-' ? -1 : 1);
        
        int id1 = atoi(flag1 == -1 ? s1.substr(1, s1.length()-1).c_str() : s1.c_str());
        int id2 = atoi(flag2 == -1 ? s2.substr(1, s2.length()-1).c_str() : s2.c_str());
        //cout << "id1:" << id1 << ",id2:" << id2 << endl;
        
        //记录二者为朋友
        isFriend[id1*10000 + id2] = true;
        isFriend[id2*10000 + id1] = true;
        
        //如果是同性朋友 需要记录朋友编号
        if (flag1 == flag2) {
            sameSexFriend[id1].push_back(id2);
            sameSexFriend[id2].push_back(id1);
        }

    }
    
    int queryNum;
    cin >> queryNum;
    for (int i = 0; i < queryNum; ++i) {
        string s1, s2;
        cin >> s1 >> s2;
        
        int id1 = atoi(s1[0] == '-' ? s1.substr(1, s1.length()-1).c_str() : s1.c_str());
        int id2 = atoi(s2[0] == '-' ? s2.substr(1, s2.length()-1).c_str() : s2.c_str());
        
        vector<node> ans;
        ans.clear();
        //分别从id1的同性朋友找a,id2的同性朋友找b,a和b为朋友,自动满足题目性别要求
        for (int m = 0; m < sameSexFriend[id1].size(); ++m) {
            for (int n = 0; n < sameSexFriend[id2].size(); ++n) {
                int fri1 = sameSexFriend[id1][m];
                int fri2 = sameSexFriend[id2][n];
                if (isFriend[fri1*10000+fri2] == true &&
                    id1 != fri2 && id2 != fri1 &&
                    fri1 != fri2) {//fri1和fri2为朋友 且不是id1和id2 且不是同一个人
                    ans.push_back(node{fri1, fri2});
                }
            }
        }
        sort(ans.begin(), ans.end(), cmp);
        cout << ans.size() << endl;
        for (int k = 0; k < ans.size(); ++k) {
            printf("%04d %04d\n", ans[k].id1, ans[k].id2);
        }
    }
    
    return 0;
}