码迷,mamicode.com
首页 > 其他好文 > 详细

【PAT1139】First Contact (30)

时间:2018-03-15 19:17:50      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:possibly   gen   span   printf   ati   cat   mes   guarantee   deque   

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.四位数中可能前几位是0,需要用%04d来格式化输出
2.可能会出现0000和-0000,故输入必须得是char[]
3.需要避免C==B和D==A的情况


#include <iostream>
#include <string>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <deque>
#include <unordered_set>
#include <algorithm>
#include <unordered_map>
#include <stack>
#include <cstdio>
using namespace std;

struct struct_query {
    int first;
    int second;
};
map<int, vector<int>> relation;
vector<struct_query> query;
bool arr[10000][10000];
bool sortbynum(struct_query &sq1, struct_query &sq2)
{
    if (sq1.first != sq2.first)
        return sq1.first < sq2.first;
    else
        return sq1.second < sq2.second;
}
int main()
{
    
    int num_people, num_relation;
    cin >> num_people >> num_relation;
    for (int i = 0;i < num_relation;i++)
    {
        char sfirst[6], ssecond[6];
        scanf("%s%s", &sfirst, &ssecond);
        
        int first = stoi(sfirst);
        int second = stoi(ssecond);
        if (first == 0)
            if (sfirst[0] == -)
                first = -55555;
            else
                first = 55555;
        if(second==0)
            if (ssecond[0] == -)
                second = -55555;
            else
                second = 55555;
        relation[first].push_back(second);
        relation[second].push_back(first);
    }
    int queries;
    cin >> queries;
    for (int i = 0;i < queries;i++)
    {
        char sfirst[6], ssecond[6];
        scanf("%s%s", &sfirst, &ssecond);

        int first = stoi(sfirst);
        int second = stoi(ssecond);
        if (first == 0)
            if (sfirst[0] == -)
                first = -55555;
            else
                first = 55555;
        if (second == 0)
            if (ssecond[0] == -)
                second = -55555;
            else
                second = 55555;

        struct_query sq;

        sq.first = first;
        sq.second = second;
        query.push_back(sq);
    }
    int flag = 0;
    vector<vector<struct_query>> result;
    for (auto pair : query)    //每个query
    {
        vector<int> a_friend = relation[pair.first], b_friend = relation[pair.second];
        vector<struct_query> temp;
        for (auto af : a_friend)    //每个C
        {

            if (pair.first * af > 0)    //pair.first -> A   af -> C
            {
                vector<int> c_friend = relation[af];
                
                for (auto cf : c_friend)    //每个D
                {
                    for (auto bf : b_friend)
                    {
                        if (pair.second!=af && pair.first!=bf && cf == bf && (bf*pair.second) > 0)    //A!=D  B!=C  且同性
                        {
                            struct_query sq;
                            sq.first = abs(af)>10000?0:abs(af);
                            sq.second = abs(cf)>10000?0:abs(cf);
                            temp.push_back(sq);
                        }
                    }
                }
            }

        }
        sort(temp.begin(), temp.end(), sortbynum);
        result.push_back(temp);
    }
    for (auto vector_sq : result)
    {
        int len = vector_sq.size();
        cout << len << endl;
        for (auto sq : vector_sq)
        {

            printf("%04d %04d\n", sq.first, sq.second);
        }
    }

    return 0;
}

 

【PAT1139】First Contact (30)

标签:possibly   gen   span   printf   ati   cat   mes   guarantee   deque   

原文地址:https://www.cnblogs.com/xiao-gan/p/8575115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!