PAT_A1139#First Contact

Source:

PAT A1139 First Contact (30 分)

Description:

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

Keys:

  • 哈希映射

Attention:

  • 首先各结点的id是不同的,在这个基础上,增加负号区分男女生,一开始想多了0,0
  • 注意+0和-0
  • 注意f1!=p2 && f2!=p1

Code:

  1 /*
  2 Data: 2019-08-07 20:48:39
  3 Problem: PAT_A1139#First Contact
  4 AC: 36:48
  5 
  6 题目大意:
  7 早年的蓝孩子和吕孩子都是很羞涩的0,0
  8 联系方式:B1 -> B2 -> G2 -> G1
  9 给一张关系网,能否帮助B1联系到G1
 10 输入:
 11 第一行给出,人数N<=300,关系数M
 12 接下来M行,A与B认识,女生添加负号
 13 接下来一行,查询次数K<=100
 14 接下来K行,A和B,帮A给B送秋波
 15 输出:
 16 第一行给出,多少对朋友可以牵线搭桥
 17 接下来N行,字典序递增
 18 
 19 基本思路:
 20 frd存储同性之间的关系网,
 21 net判断任意两人之间是否为朋友,
 22 依次遍历A的朋友和B的朋友,
 23 如果A的朋友和B的朋友有关系,则存储这队朋友;
 24 排序,打印;
 25 */
 26 #include<cstdio>
 27 #include<string>
 28 #include<vector>
 29 #include<map>
 30 #include<iostream>
 31 #include<algorithm>
 32 using namespace std;
 33 const int M=1e4;
 34 struct node
 35 {
 36     int f1,f2;
 37 };
 38 map<int,int> mp,net;
 39 vector<int> frd[M];
 40 
 41 bool cmp(const node &a, const node &b)
 42 {
 43     if(a.f1 != b.f1)
 44         return a.f1 < b.f1;
 45     else
 46         return a.f2 < b.f2;
 47 }
 48 
 49 int main()
 50 {
 51 #ifdef ONLINE_JUDGE
 52 #else
 53     freopen("Test.txt", "r", stdin);
 54 #endif // ONLINE_JUDGE
 55 
 56     int n,m,p1,p2;
 57     string v1,v2;
 58     scanf("%d%d", &n,&m);
 59     for(int i=0; i<m; i++)
 60     {
 61         cin >> v1 >> v2;
 62         p1=abs(atoi(v1.c_str()));
 63         p2=abs(atoi(v2.c_str()));
 64         mp[p1]=mp[p2]=1;
 65         net[p1*M+p2]=net[p2*M+p1]=1;
 66         if(v1.size()==v2.size())
 67         {
 68             frd[p1].push_back(p2);
 69             frd[p2].push_back(p1);
 70         }
 71     }
 72     scanf("%d", &m);
 73     while(m--)
 74     {
 75         scanf("%d%d", &p1,&p2);
 76         p1=abs(p1);
 77         p2=abs(p2);
 78         if(mp[p1]==0 || mp[p2]==0)
 79         {
 80             printf("0\n");
 81             continue;
 82         }
 83         vector<node> ans;
 84         for(int i=0; i<frd[p1].size(); i++)
 85         {
 86             for(int j=0; j<frd[p2].size(); j++)
 87             {
 88                 int f1=frd[p1][i],f2=frd[p2][j];
 89                 if(p1==f2 || p2==f1) continue;
 90                 if(net[f1*M+f2])
 91                     ans.push_back(node{f1,f2});
 92             }
 93         }
 94         sort(ans.begin(),ans.end(),cmp);
 95         printf("%d\n", ans.size());
 96         for(int i=0; i<ans.size(); i++)
 97             printf("%04d %04d\n", ans[i].f1,ans[i].f2);
 98     }
 99 
100     return 0;
101 }

 

posted @ 2019-07-04 17:21  林東雨  阅读(258)  评论(0编辑  收藏  举报