PAT1139First Contact (30)

Posted xiao-gan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT1139First 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.四位数中可能前几位是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;
}

 

以上是关于PAT1139First Contact (30)的主要内容,如果未能解决你的问题,请参考以下文章

A 1139 First Contact (30分)

1139 First Contact (30分) 题目详解

1139 First Contact (30 分)难度: 一般 / 知识点: 模拟

Codeforces1139D_CF1139DSteps to One (Mobius_DP)

Entity Framework - CTP4 - Code First - 如何关闭自动复数?

PAT 1072 Gas Station (30)