1139 First Contact

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:

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:

4
1002 2004
1003 2002
1003 2003
1004 2002
4
2001 1002
2001 1003
2002 1003
2002 1004
0
1
2003 2001
0

 

题意:

  题目可以抽象成一个无向图的问题,首先给出两个结点,问这两个节点之间是否存在另外两个结点,(题目增加了难度,又给每个节点怎加了性别这样的一个属性)

思路:

  刚开始我想的是用一个邻接矩阵来表示这个图,因为给出的编号并不是从0开始的我就想着用一个map将他们转换成从0开始,可是这样无疑使问题更加复杂。于是就看了一下别人的博客,发现我这样想是因为我的思路收到了课本上图的定义的限制。邻接矩阵可以用vector数组来表示并不代表着邻接表不可以用数组来表示。

  注意所要查询的两个人有可能是直接的朋友关系,查找的时候要把这种直接的朋友关系给去掉。

 

Code:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int main() {
 5     int N, M;
 6     cin >> N >> M;
 7     vector<int> grap[10005];
 8     vector<bool> boy(10005, false);
 9     string v1, v2;
10     int num1, num2;
11     for (int i = 0; i < M; ++i) {
12         cin >> v1 >> v2;
13         num1 = abs(stoi(v1));
14         num2 = abs(stoi(v2));
15         grap[num1].push_back(num2);
16         grap[num2].push_back(num1);
17         if (v1[0] != '-') boy[num1] = true;
18         if (v2[0] != '-') boy[num2] = true;
19     }
20     int k;
21     cin >> k;
22     string vstart, vend;
23     vector<pair<int, int> > ans;
24     for (int i = 0; i < k; ++i) {
25         ans.clear();
26         cin >> vstart >> vend;
27         num1 = abs(stoi(vstart));
28         num2 = abs(stoi(vend));
29         for (int f1 : grap[num1])
30             if (f1 != num2 && f1 != num1 && boy[f1] == boy[num1])
31                 for (int f2 : grap[f1])
32                     if (f2 != num1 && f2 != num2 && boy[f2] == boy[num2])
33                         for (int f3 : grap[f2])
34                             if (f3 == num2) ans.push_back({f1, f2});
35         sort(ans.begin(), ans.end());
36         cout << ans.size() << endl;
37         for (int i = 0; i < ans.size(); ++i) {
38             // printf("%04d %04d\n", i.first, i.second);
39             cout.setf(ios::right);
40             cout.fill('0');
41             cout << setw(4) << ans[i].first << " ";
42             cout.setf(ios::right);
43             cout.fill('0');
44             cout << setw(4) << ans[i].second << endl;
45         }
46     }
47     return 0;
48 }

  c++格式化字符串一次只能够格式化一个输出。

 

posted @ 2020-04-15 22:58  Veritas_des_Liberty  阅读(239)  评论(0编辑  收藏  举报