1139 First Contact (30 point(s))

【信息科学与工程学】【数据科学】数据科学领域——第三篇 数学08 几何学00总篇 编号英文名称中文名称简要说明/归属1Foundations of Geometry几何基础公理化方法, Hilbert《几何基础》2Euclidean Geometry (Synthetic)欧几里得几何(综合法)《几何原本》传统3Analytic Geometry解析几何坐标方法, Descartes, Fermat4Plane Geometry平面几何三角形、圆、共点共线等经典问题5Solid Geometry立体几何三维空间中的图形与度量6Non-Euclidean Geometry非欧几何平行公设不 阅读详情

1139 First Contact (30 point(s))

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. 以string的形式读入编号,如果该编号尚未存在,需要插入相关信息,记录结点的编号和姓名。鉴于编号是4位的,不能直接开那么大的邻接矩阵,采用map实现编号和自定下标的映射。

2. 在此以字符串形式读入起始和末尾结点,并取绝对值得到x,y,然后遍历每一组可能的数对,如果结点的性别满足且x、y、i、j互不相等,则合法。(这里可以优化一下,比如要i满足要求再搜索j)

3. 然后对数对进行排序,输出即可。

注意点:

1. 注意不能用int来读入,测试用例会存在-0000这样的输入。

2. 读入的结点可能不存在。比如用例,实际上只出现了9个结点,为什么N=10呢?是题目故意错了。我觉得它是想表达有只出现在查询里的1111不存在吧。

3. pair<int,int> 的排序重载就是先按照first排序,再按照second排序,不需自己写了。

4. 给出的起始和末尾结点也是带符号的,需要取绝对值。

大神思路: 

还是使用vector<int> graph[]解决稀疏矩阵的存储,避免使用麻烦的map来做映射(当然这样就只需要300*300的矩阵了)。

#include <bits/stdc++.h>
using namespace std;
vector<int>graph[10005];//图
bool boy[10005];//下标为ID,元素表示该ID是否是男
int N,M,K,vstart,vend;
int main(){
    scanf("%d%d",&N,&M);
    for(int i=0;i<M;++i){//读取边的数据
        string a,b;
        cin>>a>>b;
        int ia=abs(stoi(a)),ib=abs(stoi(b));//将字符串化为正整数
        graph[ia].push_back(ib);//向图中加入边
        graph[ib].push_back(ia);//向图中加入边
        boy[ia]=(a[0]!='-');//表示该人性别
        boy[ib]=(b[0]!='-');//表示该人性别
    }
    scanf("%d",&K);
    while(K--){
        scanf("%d%d",&vstart,&vend);//读取首尾结点
        vector<pair<int,int>>result;//存储符合题目要求的两个结点
        for(int i:graph[abs(vstart)])//遍历首节点的朋友
            if(i!=abs(vend)&&i!=abs(vstart)&&boy[i]==boy[abs(vstart)])//找到非首尾结点且与首节点性别相同的朋友作为第一个节点
                for(int j:graph[i])//遍历第一个节点的朋友
                    if(j!=abs(vend)&&j!=abs(vstart)&&boy[j]==boy[abs(vend)])//找到非首尾结点且与尾节点性别相同的朋友作为第二个节点
                        for(int k:graph[j])//遍历第二个节点的朋友
                            if(k==abs(vend))//尾结点是第二个节点的朋友
                                result.push_back({i,j});//i,j两个节点符合要求
        printf("%d\n",result.size());
        sort(result.begin(),result.end());//排序
        for(auto&i:result)//输出
            printf("%04d %04d\n",i.first,i.second);
    }
    return 0;
}

我的AC代码: 

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int N,M,K;
const int MAX = 307;
map<int,int> mp;//ID to index;
int gender[MAX];//index to gender;male 1 female -1
int id[MAX];//index to ID
int idx =0;
bool graph[MAX][MAX];
bool cmp(pair<int,int> p1,pair<int,int> p2){
	if(p1.first!=p2.first) return p1.first<p2.first;
	return p1.second<p2.second; 
}
int main(void){
	cin>>N>>M;string a,b;
	memset(graph,false,sizeof(graph));
	mp.clear();
	for(int i=0;i<M;i++){
		int na,nb;
		int ga=1;int gb=1;
		cin>>a;
		if(a[0]=='-'){
			a.erase(a.begin());
			ga = -1;
		} 
		na = stoi(a);
		if(mp.find(na)==mp.end()){
			mp[na]=idx;
			id[idx]=na;
			gender[idx]=ga;
			idx++;
		}
		cin>>b;
		if(b[0]=='-'){
			b.erase(b.begin());
			gb = -1;
		}
		nb = stoi(b);
		if(mp.find(nb)==mp.end()){
			mp[nb]=idx;
			id[idx]=nb;
			gender[idx]=gb;
			idx++;
		}
		graph[mp[na]][mp[nb]]=true;
		graph[mp[nb]][mp[na]]=true;
	}
	cin>>K;int start,end; 
	vector<pair<int,int> > v;
	while(K--){
		cin>>start>>end;
		if(mp.find(abs(start))==mp.end()||mp.find(abs(end))==mp.end()){
			cout<<"0"<<endl;continue;
		} 
		int x = mp[abs(start)];int y=mp[abs(end)];
		v.clear();
		for(int i=0;i<idx;i++){
			for(int j=0;j<idx;j++){
				if(x!=i&&i!=j&&j!=y&&x!=j&&i!=y&&graph[x][i]&&graph[i][j]&&graph[j][y]){
					if(gender[x]*gender[i]==1&&gender[y]*gender[j]==1) v.push_back(pair<int,int>(id[i],id[j]));
				} 
			}
		}
		sort(v.begin(),v.end(),cmp);
		cout<<v.size()<<endl;
		for(auto it:v) printf("%04d %04d\n",it.first,it.second);
	}	
	return 0;
}

 

【信息科学与工程学】【通信工程】第一百九十一篇 路由器体系结构03 编号类型领域问题详细的数学分析(逐步推理)参数列表(表达式 / 数值范围 / 边界 / 测量)关联知识839DCI演进 · 收敛DCI 2030 年三大技术收敛方向步骤1:方向一:IPoDWDM 成为绝对主流(Omdia 2027 59% → 2030 80%+),OTN 退化为 OLS 纯光层。步骤2:方向二:800ZR+ 取代 400ZR 成为主力速率,PCS 使 800G 达 600–1000km。步骤3:方向三:AI 集群 DCI 催生 1.6T/3.2T 超短距(<10km)相干模块(OSFP-X 阅读详情

相关推荐

【信息科学与工程学】计算机科学与自动化——第六十六篇 算子篇 第一章 通用算子01 多模态数据(图形/图像/文本/语音)中推断人类内在状态、策略、动机和利益的算法算子知识库

编号领域算法/方法算子名称算子的数学方程式/表达复杂度 (示例)关联知识11量子场论正则量子化产生/湮灭算符对易关系: [â_p, â†_q] = δ⁽³⁾(p​ - q)应用于态空间,计算复杂度取决于截断福克空间,粒子数表象,二次量子化12统计力学蒙特卡洛模拟马尔可夫链转移算子T(s → s‘) :满足细致平衡条件 π(s)T(s→s’)=π(s‘)T(s’→s)收敛到平衡分布需 ~O(τ), τ为相关时间细致平衡,Metropolis-Hastings准则,重要性采样13电磁学边界元法单层势/双层势算子

weixin_49199313的博客 310

【1139】First Contact (30分)

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<cmath> #include<string.h> #include<string> #include<algorithm> #inc...

发现问题,并解决问题,批判性思维 672

【信息科学与工程学】【数据科学】数据科学领域-第三篇 数学基础04 集合论02

编号类型概念类别概念名称数学定义/描述数学表示性质特征重要特例相关理论问题的数学分析参数列表及参数数值范围关联知识1基础集合论本体集合若干确定的、互不相同的对象的总体A,B,…确定性、互异性、无序性∅,N,RZFC 公理系元素归属判定 x∈A势 ∥A∥∈{finite}∪[ℵ0​,+∞)朴素集合论→公理化2基础集合运算并/交/差/补按逻辑联结词定义的集合操作A∪B,A∩B,A∖B交换律、结合律、分配律、De MorganA∪∅=A布尔代数多集合推广 ⋃i∈I​Ai​I为指标集,可数/不可数测度论3基础构造幂

weixin_49199313的博客 371

PAT 1139 First Contact

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 i...

weixin_30341745的博客 340

政府科技管理者如何提升科技成果转化项目进场交易的备案效率?.docx

政府科技管理者如何提升科技成果转化项目进场交易的备案效率?

路由工具之策略路由policy-based-routing、MQC三板斧流分类、流行为、流策略

PBR(Policy-Based Routing,策略路由)策略路由可理解为控制路由,因为PBR策略的优先级高于传统路由表的转发,所以它主要用于影响数据包转发的技术。

Hades_Ling的优快云博客 5583

PAT A1139 First Contact (30point(s))

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 i...

qq_42347617的博客 489

结构方程模型amos的操作与应用pdf_AMOS结构方程进行医学问卷量表分析教程(一阶简单路径效应分析)...

在生物医药的数据统计分析中,要研究量表和量表之间的关系时候,由于维度题项较多,而不再是单一的变量,因此我们要使用AMOS结构方程进行处理。​ 举个例子,比如对住院患者的相关研究,设置了患者在住院期间的适应性量表和自我效能的量表,想考察患者适应性与自我效能之间的影响关系,或者说适应性的强弱是否会影响到患者自我效能的高低。此时适应性量表和自我效能量表都不是单一的变量,而是有N个题项构成的...

weixin_39637614的博客 7711

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 direct...

weixin_30409849的博客 434

飞牛NAS挂载115网盘避坑指南:为什么你的大文件总是传输失败?

本文深入解析了在飞牛NAS上通过AList挂载115网盘时,大文件传输失败的深层原因,包括风控机制、网络协议及兼容性问题。文章提供了从基础排查到高阶优化的系统性解决方案,并建议采用Kopia分片备份等终极策略,以实现稳定可靠的云端数据流转。

weixin_29306011的博客 1695

怎么在豆包里做生成式引擎优化(GEO)

本文系统介绍了豆包平台生成式引擎优化(GEO)的全套方法体系。文章首先解析了豆包与传统搜索引擎的核心差异,强调其多模态理解和上下文感知特性,并指出GEO需从关键词匹配转向用户意图动态解析。随后详细阐述了GEO优化的三大核心策略:用户意图解析技术、实时内容适配框架和权威信号强化方法,配以技术实现路径和案例说明。最后提出GEO与SEO融合的未来趋势,建议企业构建统一知识库,实现全渠道声誉管理。全文包含大量实操代码和工具链指导,为企业在AI时代的内容优化提供系统解决方案。

解锁AI流量新红利!🚀 GEO优化(生成引擎优化)助您掌控AI搜索,让AI成为您的品牌最佳代言人! 2384

1139 First Contact (30point(s)) - C语言 PAT 甲级

* 记录关系图和性别 * 循环遍历,记录符合条件的路径 注意: * 不能直接按照整数读取,有 -0000 存在 * 若起始节点和终节点本身是好友,应判断此种情况,避免循环提前结束

有三只土豆 765

超实用!用 Ollama + DeepSeek + Dify 搭建本地知识库,提升企业效率

本文介绍了如何使用 Ollama + DeepSeek + Dify 搭建本地知识库,帮助企业高效管理信息。通过简单配置,可实现智能问答、文档检索等功能,提升效率并保障数据安全。

Happiness is everywhere 4294

1139 First Contact (30 point(s))

题解 技巧性在于交际网络中,只录入同性关系。 #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;map&gt; #include&lt;algorithm&gt; #include&lt;vector&gt; using namespace std; const int MAXN = 1e4; struct node ...

徐伯莱 400

vue2/3 - 详细对接海康监控实时视频预览和录像回放功能,vue接入使用海康视频监控播放器实时推流等功能,支持hik/rtsp/rtmp/hls/ws取流协议(详细教程与示例代码,一键复制开箱即用

vue2,vue3,海康,vue海康视频监控,海康开放平台,如何接入海康的实时监控,海康威视,Vue实现海康监控视频播放,vue3对接海康视频监控播放、回放的方法流程,实现海康H5视频插件播放视频的实例,web网页接入海康,海康威视的视频vue的录像回放和实时预览,前端vue3项目接入海康威视h5player.js播放器,vue2接入海康威视视频监控平台,使用海康监控H5视频播放,对接海康视频监控,海康视频web插件,web前端在vue中通过海康插件嵌入视频,实现实时预览,vue3海康监控播放器组件,vue

✅ 精选推荐文章 1万+

Spring AI Alibaba实战训练营-16 NL2SQL (自然语言转 SQL)应用开发指南

本文介绍了一个基于AI的智能BI应用开发项目,主要实现自然语言转SQL(NL2SQL)功能,包含对话模块、MCP模块、向量数据管理模块。项目采用Spring AI Alibaba框架,集成通义千问大模型,支持内存向量库和AnalyticDB向量数据库两种存储方案。重点展示了chat模块的三个控制器实现:AnalyticNl2SqlController用于生产环境,SimpleChatController适用于开发测试,Nl2sqlForGraphController支持基于图的复杂对话流程。系统通过将业务知

program哲学 1589

【PAT】A1139 First Contact (30 point(s))

文章目录A1139 First Contact (30 point(s))Input Specification:Output Specification:Sample Input:Sample Output: A1139 First Contact (30 point(s)) Unlike in nowadays, the way that boys and girls expressing t...

Duke 593

华为ONU远程登录管理配置

华为P602E、EG8040CONU、EG8240H5、W626E等ONU、ONT默认无法配置管理IP,无法远程telnet登录管理。校园网项目中因故障诊断需要远程登录ONU可以通过OLT的远程WEB代理功能链接登录,具体配置如下:

symstandsun的博客 6111

*PAT_甲级_1139 First Contact (30point(s)) (C++)【图的遍历】

1,题目描述 analogously:类似地; 类似; 近似地; crush:压坏; 压伤; 挤压变形; 把…挤入,将…塞进(狭小的空间内); 压碎; 捣碎;(通常指年轻人对年长者的短暂的) 热恋,迷恋; 果汁饮料; 2,思路 3,AC代码 4,解题过程 -0000或0000 ...

&再见萤火虫&的博客 1424

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

PAT(A) 1139 First Contact (30 point(s)) 测试点

测试点2:输入存在“-0000” 测试点3、4、5:AB ≠ CD #include<bits/stdc++.h> using namespace std; int N, M, K; string v1, v2; vector<int> gender; vector<vector<int>> v; map<string, int> m1; // m1: id->index map<int, string> m2; // m2.

Anpotan的博客 357

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

PAT甲级 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 i...

moffy_abo的博客 1788

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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 i...

pacmo 691

PAT 1139 First Contact (30point(s))(解题记录)

I don’t want to say more about this question… Note that the ID can be -0000!! So we should store ID in a string instead of int valid. I have no time to simply my code,so I’m sorry that it would be a little tedious. #include <iostream> #include <st

momoomoo的博客 355

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 i...

海绵的博客 283
上一篇: 1117 Eddington Number (25 point(s))
下一篇: 1124 Raffle for Weibo Followers (20 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值