A1139 First Contact (30 point(s))

图的应用 连通可以表示为v[].push_back() / m[a*maxn+b]=1

1. 原文

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0

2. 解析

boyB - boyC < –> girlD-firlA

v[a].push_back(b)记录同性指向,m[a*maxn+b]=1记录所有边信息

当查询的两个的v[ a ] [ i ] * maxn + v[ b ] [ i ] 存在边时就是结果

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<cstdio>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10000;
map<int,int> contact;
vector<int> v[maxn];
struct node
{
int a,b;
node(int A,int B){a=A; b=B;}
};
bool cmp(node a,node b){
if (a.a!=b.a)
{
return a.a<b.a;
}else{
return a.b<b.b;
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
char ca[7],cb[7];
string stra,strb;
int a,b;
//0000 -0000 int无法记录
for (int i = 0; i < m; ++i)
{
scanf("%s%s",ca,cb);
stra=ca;
strb=cb;
a=abs(atoi(stra.c_str()));
b=abs(atoi(strb.c_str()));
if (stra.length()==strb.length())
{
v[a].push_back(b);
v[b].push_back(a);
}
contact[a*maxn+b]=contact[b*maxn+a]=1;
}
int k;
scanf("%d",&k);
while(k--){
vector<node> ans;
scanf("%d%d",&a,&b);
a=abs(a);
b=abs(b);
for (int i = 0; i < v[a].size(); ++i)
{
for (int j = 0; j < v[b].size(); ++j)
{
if (v[a][i]==b||v[b][j]==a)
{
continue;
}
if (contact[v[a][i]*maxn+v[b][j]]==1)
{
ans.push_back(node(v[a][i],v[b][j]));
}
}
}
sort(ans.begin(), ans.end(),cmp);
printf("%lu\n",ans.size());
for (int i = 0; i < ans.size(); ++i)
{
printf("%04d %04d\n",ans[i].a,ans[i].b);
}
}
return 0;
}

结果输出为4位数字%04d 利用cout输出不方便
代码错误 比较a.a!=b.a

本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1139/
  • 发布时间: 2019-09-02 18:16
  • 更新时间: 2021-10-29 14:10
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!