1. 程式人生 > 實用技巧 >pat 1139

pat 1139

1139First 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.用陣列arr標記兩個人是否是朋友(鄰接矩陣表示),用v標記所有人的同性朋友(鄰接表表示)
2.對於一對想要在一起的A和B,他們需要先找A的所有同性朋友C,再找B的所有同性朋友D,當C和D兩人是朋友的時候則可以輸出C和D
3.A在尋找同性朋友時,需要避免找到他想要的伴侶B,所以噹噹前朋友就是B或者B的同性朋友就是A時捨棄該結果
4.輸出時候要以4位數的方式輸出,所以要%04d
5.如果用int接收一對朋友,-0000和0000對於int來說都是0,將無法得知這個人的性別,也就會影響他找他的同性朋友的那個步驟,所以考慮用字串接收,因為題目中已經表示會以符號位加四位的方式給出輸入,所以只要判斷字串是否大小相等,如果大小相等說明這兩個人是同性
6.結果陣列因為必須按照第一個人的編號從小到大排序(當第一個相等時按照第二個人編號的從小到大排序),所以要用sort對ans陣列排序後再輸出結果

Update: 新PAT系統中原始碼導致了一個測試點記憶體超限,使用unordered_map<int, bool> arr 替代二維陣列可避免記憶體超限(2018-05-28更新)(摘自柳神,原文連結柳婼pat1139)

程式碼如下:

#include<cstdio>
#include<string>
#include<unordered_map>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
vector<int> v[10000];
unordered_map<int,bool> arr;
struct node{
    int a,b;
};
bool cmp(node a,node b){
    return a.a!=b.a?a.a<b.a:a.b<b.b;
}
int main(){
    int n,m;
    string a,b;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        cin>>a>>b;
        if(a.length()==b.length()){
            v[abs(stoi(a))].push_back(abs(stoi(b)));
            v[abs(stoi(b))].push_back(abs(stoi(a)));    
        }

        arr[abs(stoi(a))*10000+abs(stoi(b))]=arr[abs(stoi(b))*10000+abs(stoi(a))]=true;
    }
    int k;
    scanf("%d",&k);
    int c,d;
    for(int i=0;i<k;i++){
        vector<node> ans;
        cin>>c>>d;
        for(int j=0;j<v[abs(c)].size();j++){
            for(int k=0;k<v[abs(d)].size();k++){
                if(v[abs(c)][j]==abs(d)||v[abs(d)][k]==abs(c))    continue;
                if(arr[v[abs(c)][j]*10000+v[abs(d)][k]]==true)
                    ans.push_back(node{v[abs(c)][j],v[abs(d)][k]});
            }
        }
        printf("%d\n",ans.size());
        sort(ans.begin(),ans.end(),cmp);
        for(int j=0;j<ans.size();j++){
            printf("%04d %04d\n",ans[j].a,ans[j].b);
        }
    }
    return 0;
}