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 ...
随机推荐
- nginx 的 重定向
1. ngx.redirect(uri, status?) 301/302重定向 redirect 为外部重定向,有两种形式: rewrite ^ /foo? redirect; # ngi ...
- ansible自动化运维02
ansible清单管理 inventory文件通常用于定义要管理主机的认证信息,例如:ssh登录用户名,密码,以及key相关信息. 举个例子:定义清单组 注意:组名为pro,关键字段children表 ...
- vue - 基础(3)
1.数据的双向绑定 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Graylog-Sidecar
收集linux日志-filebeat 安装sidecar 下载graylog-sidecar-1.0.2-1.x86_64.rpm rpm -ivh graylog-sidecar-1.0.2-1.x ...
- python相关资源
http://blog.csdn.net/ken_wanglin/article/details/53095967 http://www.infoq.com/cn/articles/Python-wr ...
- 【pytorch】torch.utils.data.DataLoader
简介 DataLoader是PyTorch中的一种数据类型.用于训练/验证/测试时的数据按批读取. torch.utils.data.DataLoader(dataset, batch_size=1, ...
- 原生php分页的封装,只封装函数,可适用所有的表
<?php/** * 封装分页函数 * $table [字符串] 表名 * @$size [数字][每页显示条数] */function fenye($table, $size){ $link ...
- Django 1.11 bootstrap样式文件无法加载问题解决
先吐槽一波,多看官方教程,多找对应版本解决方法,多思考!... 在调试模式下面,打开页面无法加载bootstrap.min.css样式,解决思路如下: 查看settings文件INSTALL_APP配 ...
- HTML5 - websocket的应用 之 简易聊天室
需要知识点: 前端知识 jq操作dom nodejs socket.io 关于websocket api的知识点,见上篇章<HTML5-Websocket>. 聊天室思路/原理: A和B聊 ...
- 语法上的小trick
语法上的小trick 构造函数 虽然不写构造函数也是可以的,但是可能会开翻车,所以还是写上吧.: 提供三种写法: 使用的时候只用: 注意,这里的A[i]=gg(3,3,3)的"gg&qu ...