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的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. pat甲级1139

    1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...

  3. PTA甲级B1061 Dating

    目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...

  4. PAT甲级1139 First Contact

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...

  5. PTA甲级—链表

    1032 Sharing (25分) 回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点. 我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案.后来发现这里忽略了两个链 ...

  6. PTA甲级—STL使用

    1051 Pop Sequence (25分) [stack] 简答的栈模拟题,只要把过程想清楚就能做出来. 扫描到某个元素时候,假如比栈顶元素还大,说明包括其本身的在内的数字都应该入栈.将栈顶元素和 ...

  7. PTA甲级—数学

    1.简单数学 1008 Elevator (20分) 模拟题 #include <cstdio> #include <cstring> #include <string& ...

  8. PTA甲级—常用技巧与算法

    散列 1078 Hashing (25 分) Quadratic probing (with positive increments only) is used to solve the collis ...

  9. PTA 1139 1138 1137 1136

    PAT 1139 1138 1137 1136 一个月不写题,有点生疏..脑子跟不上手速,还可以啦,反正今天很开心. PAT 1139 First Contact 18/30 找个时间再修bug 23 ...

随机推荐

  1. EntityManager 的复杂查询

    EntityManager 是用来对实体Bean 进行操作的辅助类.他可以用来产生/删除持久化的实体Bean,通过主键查找实体bean,也可以通过EJB3 QL 语言查找满足条件的实体Bean.实体B ...

  2. 1047--Remove All Adjacent Duplicates In String

    public class RemoveAllAdjacentDuplicatesInString { /* 解法一:栈 */ public String removeDuplicates(String ...

  3. Mysql高性能优化规范

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意 ...

  4. 并发编程(六)--进程/线程池、协程、gevent第三方库

    一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上百个或上千个,手动创建就较为繁琐,这时就可 ...

  5. Java IO全面

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10857412.html 一:IO流梳理——字符流.字节流.输入流.输出流 见另一篇博文:https://ww ...

  6. javascript之DOM(一节点类型Node)

    DOM(Document Object Model)是针对HTML和XML文档的一个API.DOM描述的是一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分.起源于DHML,现为W3C的推 ...

  7. Idea实用功能手记

    一. 自动优化导包(自动删除.导入包) 下面是Auto Import设置步骤详解. Settings→Editor→General→Auto Import 然后勾选Add unambiguous im ...

  8. PacMan 03——追踪玩家

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  9. PAT 乙级 1048.数字加密 C++/Java

    题目来源 本题要求实现一种数字加密方法.首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 ...

  10. Vue中swiper手动滑动后不能自动播放的解决方法

    用户操作swiper之后,是否禁止autoplay.默认为true:停止.如果设置为false,用户操作swiper之后自动切换不会停止,每次都会重新启动autoplay.操作包括触碰,拖动,点击pa ...