PAT甲级 1139

本文探讨了一个有趣的算法问题,即在一个友谊网络中,如何为一位想要追求心仪对象的人找到合适的中间人,通过一系列复杂的关系链,实现两人的初次接触。问题涉及到图论中的查找路径和匹配算法,同时考虑了性别匹配和直接联系的排除条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 题意:A如果想和B在一起,那他就要先找自己的同性朋友C,再找B的同性朋友D,如果D和C是朋友,则输出C和D; 如果A的同性朋友是B或者B的同性朋友是A是要避免输出这种结果。

这道题我并没有的到满分,系统提示超时~~~~~

#include <iostream>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<string>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
bool gender[10001],G[10001][10001];
vector <vector<int>> G2(10001);
struct edge{
int f1,f2;};
bool cmp(edge a,edge b){
if(a.f1!=b.f1) return a.f1<b.f1;
 return a.f2<b.f2;

}
int main()
{
   int n,m,k;
   cin>>n>>m;
   int i,u,v;
   string s1,s2;
   for(i=0;i<m;i++){
    cin>>s1>>s2;
    u=abs(stoi(s1));
    v=abs(stoi(s2));
    if(s1[0]=='-') gender[u]=true;
    if(s2[0]=='-') gender[v]=true;
    G[u][v]=true;
    G[v][u]=true;
    G2[u].push_back(v);
    G2[v].push_back(u);

       }
       cin>>k;
       for(i=0;i<k;i++){
        cin>>u>>v;
        u=abs(u);
        v=abs(v);
        vector<edge> vectice;
        for(int x:G2[u]){
            if(x==v) continue;
            if(!G[u][x]||(gender[x]!=gender[u]))  continue;
            for(int y:G2[x]){
                if(y==u)continue;
                if(G[y][x]&&G[y][v]&&gender[y]==gender[v]){
                    edge z;
                    z.f1=x;
                    z.f2=y;
                    vectice.push_back(z);

                }
            }
        }

        sort(vectice.begin(),vectice.end(),cmp);
        cout<<vectice.size()<<endl;
        for(edge h:vectice)
            printf("%04d %04d\n",h.f1,h.f2);

               }
    return 0;
}

 

### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值