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 ...
随机推荐
- CDH报错:PersistenceException: [PersistenceUnit: cmf.server] Unable to build EntityManagerFactory
1.在启动CDH中master的服务cloudera-scm-server start并立刻挂掉了,提示如下错误 org.springframework.beans.factory.BeanCreat ...
- 红黑树实现(c/c++)
红黑树 简介 一直想写的一种数据结构,非常厉害的思想,插入,删除,查找,修改,都是\(log_2 n\)的时间复杂度. 比AVL更强大的是,插入删除综合效率比AVL要优秀一点. 性质 一颗红黑树是满足 ...
- Elasticsearch 在 7.X版本中去除type的概念
背景说明 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库. El ...
- Odoo视图的共有标签
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826405.html 1)name (必选) 通过name值,查找标签 2)model 与view相关联的 ...
- sublimeText3和phpstrom使用
一.sublimtext3 下载地址:http://www.sublimetext.com/3 1.1 安装package control 插件,用来获取和管理插件(sublime包管理工具 ...
- 【Appium】Android 按键码
keycode也是appium很强大的功能,鉴于官网不翻墙无法打开,特此备忘. 电话键 KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYCODE_HOM ...
- 浅析Volatile关键字
浅析Volatile关键字 在java中线程并发中,线程之间通信方式分为两种:共享内存和消息传递.共享内存指的是多个线程之间共享内存的属性状态:消息传递指的是线程之间发送信息来通信.在介绍volati ...
- np.append()
1.numpy.append() np.append(arr,values,axis=None) 功能:在array后面追加values 参数: arr:array_like 值将附加到此数组的副本. ...
- jupyter配置成coding神器
参考链接: [1]http://resuly.me/2017/11/03/jupyter-config-for-windows/ [2]主题更换 切换主题:jt 主题名 -T 主题种类:chester ...
- 如何用<dl>标签做表格而不用table标签
我们都知道很多的内容编辑器(TinyMCE编辑器.fck)都有插入表格功能,快速方便,但是这些表格用到的<table>标签,可以查看html源代码就能发现,table标签对搜索引擎不是很友 ...