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
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <set> 6 #include <utility> 7 using namespace std; 8 const int maxn=10010; 9 int n,m,k; 10 set<int> same[maxn],dif[maxn]; 11 int gender[maxn]={0}; 12 vector<int> path[maxn],tmppath; 13 bool vis[maxn]; 14 int num=0; 15 int main(){ 16 scanf("%d %d",&n,&m); 17 for(int i=0;i<m;i++){ 18 string p1,p2; 19 cin>>p1>>p2; 20 int aid,bid; 21 aid=abs(stoi(p1)); 22 bid=abs(stoi(p2)); 23 if(p1[0]=='-'){ 24 gender[aid]=-1; 25 } 26 if(p2[0]=='-'){ 27 gender[bid]=-1; 28 } 29 if(gender[aid]==gender[bid]){ 30 same[aid].insert(bid); 31 same[bid].insert(aid); 32 } 33 else{ 34 dif[aid].insert(bid); 35 dif[bid].insert(aid); 36 } 37 } 38 scanf("%d",&k); 39 int x=0; 40 for(int i=0;i<k;i++){ 41 int p1,p2; 42 scanf("%d %d",&p1,&p2); 43 vector<int> ans; 44 fill(vis,vis+maxn,false); 45 vis[abs(p1)]=true; 46 vis[abs(p2)]=true; 47 if(gender[abs(p1)]==gender[abs(p2)]){ 48 for(auto it=same[abs(p1)].begin();it!=same[abs(p1)].end();it++){ 49 if(vis[*it]==false){ 50 for(auto it2=same[abs(p2)].begin();it2!=same[abs(p2)].end();it2++){ 51 if(vis[*it2]==false){ 52 if(same[*it].find(*it2)!=same[*it].end()){ 53 ans.push_back(*it); 54 ans.push_back(*it2); 55 } 56 } 57 } 58 } 59 } 60 } 61 else{ 62 for(auto it=same[abs(p1)].begin();it!=same[abs(p1)].end();it++){ 63 if(vis[*it]==false){ 64 for(auto it2=same[abs(p2)].begin();it2!=same[abs(p2)].end();it2++){ 65 if(vis[*it2]==false){ 66 if(dif[*it].find(*it2)!=dif[*it].end()){ 67 ans.push_back(*it); 68 ans.push_back(*it2); 69 } 70 } 71 } 72 } 73 } 74 } 75 printf("%d ",ans.size()/2); 76 for(int j=0;j<ans.size();j+=2){ 77 printf("%04d %04d ",ans[j],ans[j+1]); 78 } 79 } 80 81 }
注意点:这种有正负两种的要开一个数组保存结果,然后分情况的可以开多个数组或是set保存不同结果,不要都保存在一起。最后输出4位整数要注意一下。