PTA 甲级 1139
https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312
其实这道题目不难,但是有很多坑点!
首先数据是性别+id,id不是数字!
输出的时候必须前面补0,
另外什么自己和自己连边,重复的边都要注意,
更注意的是 -0000,这个数据,不能用int来读,我艹
#include <iostream>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <string.h>
using namespace std;
int n;
int m;
struct Node
{
int value;
int next;
}edge[200005];
struct Answer
{
int a;
int b;
}res[200005];
int Compare(Answer a,Answer b)
{
if(a.a == b.a)
return a.b < b.b;
return a.a < b.a;
}
map<pair<int,int>,int> dic;
map<pair<int,int>,int> dic2;
int pos;
int head[10005];
int gender[10005];
void Add(int x,int y)
{
edge[pos].value = y;
edge[pos].next = head[x];
head[x] = pos++;
}
int tag;
void fun(int x,int y)
{
tag=0;
dic2.clear();
int i = head[x];
int a[305];
int j=0;
while(i != -1)
{
if(gender[edge[i].value] == gender[x]
&& edge[i].value != y && edge[i].value!=x) {
a[j++] = edge[i].value;
}
i = edge[i].next;
}
i = head[y];
int b[305];
int k=0;
while(i!=-1)
{
if(gender[edge[i].value] == gender[y]
&& edge[i].value != x && edge[i].value!=y) {
b[k++] = edge[i].value;
}
i = edge[i].next;
}
for(int p=0;p<j;p++)
{
for(int q=0;q<k;q++)
{
if(a[p]==b[q])
continue;
if(dic[make_pair(a[p],b[q])]==1)
{
Answer s;
s.a = a[p];
s.b = b[q];
res[tag++] = s;
}
}
}
sort(res,res+tag,Compare);
printf("%d\n",tag);
for(int i=0;i<tag;i++)
{
printf("%04d %04d\n",res[i].a,res[i].b);
}
}
int main()
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
pos=0;
string xx,yy;
int x;
int y;
for(int i=0;i<m;i++)
{
cin>>xx>>yy;
x = atoi(xx.c_str());
y = atoi(yy.c_str());
if(x<0 || xx[0]=='-')
gender[abs(x)]=-1;
else
gender[x]=1;
if(y<0 || yy[0]=='-')
gender[abs(y)]=-1;
else
gender[y]=1;
x = abs(x);
y = abs(y);
if(dic[make_pair(x,y)]==1)
continue;
Add(x,y);
Add(y,x);
dic[make_pair(x,y)]=1;
dic[make_pair(y,x)]=1;
}
int k;
scanf("%d",&k);
for(int i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
x = abs(x);
y = abs(y);
fun(x,y);
}
return 0;
}
PTA 甲级 1139的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- pat甲级1139
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- PTA甲级B1061 Dating
目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...
- PAT甲级1139 First Contact
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...
- PTA甲级—链表
1032 Sharing (25分) 回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点. 我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案.后来发现这里忽略了两个链 ...
- PTA甲级—STL使用
1051 Pop Sequence (25分) [stack] 简答的栈模拟题,只要把过程想清楚就能做出来. 扫描到某个元素时候,假如比栈顶元素还大,说明包括其本身的在内的数字都应该入栈.将栈顶元素和 ...
- PTA甲级—数学
1.简单数学 1008 Elevator (20分) 模拟题 #include <cstdio> #include <cstring> #include <string& ...
- PTA甲级—常用技巧与算法
散列 1078 Hashing (25 分) Quadratic probing (with positive increments only) is used to solve the collis ...
- PTA 1139 1138 1137 1136
PAT 1139 1138 1137 1136 一个月不写题,有点生疏..脑子跟不上手速,还可以啦,反正今天很开心. PAT 1139 First Contact 18/30 找个时间再修bug 23 ...
随机推荐
- OEL7.6安装Oracle Database 19C(VERSION 19.3.0.0)
1.eDelivery中下载Oracle Database 19C和Oel的安装介质,并安装好操作系统 2.安装Oracle环境准备工具 环境准备工具会自动完成用户和用户组的创建.系统参数配置.依赖包 ...
- 剑指offer-08 二叉树的下一个节点
剑指offer第8题,本来想找leetcode上对应的题,后来没找到,直接去牛客网上刷了. 题目描述: 给定一个二叉树和其中的一个结点(pNode),请找出中序遍历顺序的下一个结点并且返回.注意,树中 ...
- JavaScript三种方法获取地址栏参数的方法
今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split(“?”)这样一步步的分解出需要的参数. 后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单 ...
- 搜狐视频 登录 md5 加密破解
这是一个简单的md5加密,可以学习一下js加密的破解流程,当一个入门级的教程 第一步:请求抓包 password是32位的字母和数字组合,猜测可能是md5加密,我们md5在线工具上试一下 发现我们的猜 ...
- Linux传输指令-scp
本地传到远程 scp -rf bt.plist cjp@centos:~/下载 从远程下载到本地 scp -rf cjp@centos:~/下载 ~ 参数详解 参数 描述 -a 尽可能将档案状态.权限 ...
- 20180610模拟赛T1——脱离地牢
Description 在一个神秘的国度里,年轻的王子Paris与美丽的公主Helen在一起过着幸福的生活.他们都随身带有一块带磁性的阴阳魔法石,身居地狱的魔王Satan早就想着得到这两块石头了,只要 ...
- Utterance-level Aggregation for Speaker Recognition in The Wild
文章[1]主要针对的是语句长度不定,含有不相关信号的说话人识别. 深度网络设计的关键在于主干(帧级)网络的类型[the type of trunk (frame level) network]和有时间 ...
- unzip命令(转)
unzip命令用于解压缩由zip命令压缩的“.zip”压缩包. 语法 unzip(选项)(参数) 选项 -c:将解压缩的结果显示到屏幕上,并对字符做适当的转换: -f:更新现有的文件: -l:显示压缩 ...
- 如何使用docker部署springcloud???
如何使用docker部署springcloud??? springcloud 必须基于 springboot 项目来开发, 这里讲解springboot项目的部署, springcloud项目也是同理 ...
- three.js 加载STL文件
注意: TrackballControls.js 和 TDSLoader.js 都引用了 three.module.js,特别注意引用的路径 比如: 直接代码咯 <!DOCTYPE html&g ...