1139 First Contact(30 分)(C++)

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

题目大意:给定一个图,然后给一些一对节点,要给这两个节点分别找出所有的属性与各自他们属性相同的节点对,这两个节点x直接相通。其实属性存储什么千万别想的太麻烦。在存储的时候,就将不同的路径按照节点端点属性是否相同存储好。因为要找的节点对属性一个与各自属性相同,那么就剩找出的这个节点对的属性是否相同

两种情况:

1、给出的节点对同属性,那么找出的节点属性相同。

2、给出的节点对不同属性,那么找出的节点属性不相同。

注意点:有可能给出的两个节点本身就是相通,但是对方并不能作为结果。

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
typedef struct{
    int fa,fb;
}friends;
int cmp(friends f1,friends f2){
    if(f1.fa!=f2.fa)
        return f1.fa<f2.fa;
    else
        return f1.fb<f2.fb;
}
std::vector<int> same[10005],diff[10005];
std::vector<friends> result;
int n,m,k;
int main(){
    scanf("%d %d",&n,&m);
    for(int i=0;i<m;i++){
        string a1,b1;bool flag=true;
        cin>>a1>>b1;
        if(a1.length()!=b1.length())
            flag=false;
        int a,b;
        a=abs(stoi(a1));b=abs(stoi(b1));
        if(flag==true){
            same[a].push_back(b);
            same[b].push_back(a);
        }
        if(flag==false){
            diff[a].push_back(b);
            diff[b].push_back(a);
        }
    }
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        result.clear();
        set<int>s;
        int a,b;
        bool flag=true;
        string a1,b1;
        cin>>a1>>b1;
        if(a1.length()!=b1.length())
            flag=false;
        a=abs(stoi(a1));b=abs(stoi(b1));
        for(int i=0;i<same[a].size();i++){
            if(same[a][i]!=b)
            s.insert(same[a][i]);
        }
        for(int i=0;i<same[b].size();i++){
            if(same[b][i]==a)
                continue;
            if(flag==false){
                for(int j=0;j<diff[same[b][i]].size();j++){
                    if(s.find(diff[same[b][i]][j])!=s.end()){
                        friends temp;
                        temp.fa=diff[same[b][i]][j];temp.fb=same[b][i];
                        result.push_back(temp);
                    }
                }
            }
            else{
                for(int j=0;j<same[same[b][i]].size();j++){
                    if(s.find(same[same[b][i]][j])!=s.end()){
                        friends temp;
                        temp.fa=same[same[b][i]][j];temp.fb=same[b][i];
                        result.push_back(temp);
                    }
                }
            }
        }
        sort(result.begin(), result.end(),cmp);
        printf("%d\n",result.size());
        for(int i=0;i<result.size();i++){
            printf("%04d %04d\n",result[i].fa,result[i].fb);
        }
    }
    system("pause");
}

猜你喜欢

转载自blog.csdn.net/qq_41562704/article/details/82526034