zoukankan      html  css  js  c++  java
  • A1139. 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

    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<set>
    #include<vector>
    #include<algorithm>
    #include<string>
    using namespace std;
    vector<string> fri, sec;
    map<string, map<string, int>>G;
    int N, M;
    typedef struct{
        string a, b;
    }node;
    node list[10000];
    bool cmp(node &nd1, node &nd2){
        if(nd1.a != nd2.a){
            return nd1.a < nd2.a;
        }else{
            return nd1.b < nd2.b;
        }
    }
    int main(){
        scanf("%d%d", &N, &M);
        for(int i = 0; i < M; i++){
            string v1, v2;
            cin >> v1 >> v2;
            G[v1][v2] = 1;
            G[v2][v1] = 1;
        }
        int K;
        scanf("%d", &K);
        for(int i = 0; i < K; i++){
            string v1, v2;
            cin >> v1 >> v2;
            fri.clear();
            sec.clear();
            map<string, int>::iterator it;
            for(it = G[v1].begin(); it != G[v1].end(); it++){
                if(it->first != v1 && it->first != v2 && (it->first[0] != '-' && v1[0] != '-' || it->first[0] == '-' && v1[0] == '-'))
                    fri.push_back(it->first);
            }
            for(it = G[v2].begin(); it != G[v2].end(); it++){
                if(it->first != v1 && it->first != v2 &&(it->first[0] != '-' && v2[0] != '-' || it->first[0] == '-' && v2[0] == '-'))
                    sec.push_back(it->first);
            }
            int pt = 0;
            for(int j = 0; j < fri.size(); j++){
                for(int k = 0; k < sec.size(); k++){
                    if(G[fri[j]].count(sec[k]) != 0){
                        string s1 = fri[j], s2 = sec[k];
                        if(s1[0] == '-'){
                            s1.erase(0, 1);
                        }
                        if(s2[0] == '-'){
                            s2.erase(0, 1);
                        }
                        list[pt].a = s1;
                        list[pt++].b = s2;
                    }
                }
            }
            sort(list, list + pt, cmp);
            printf("%d
    ", pt);
            for(int k = 0; k < pt; k++){
                cout << list[k].a <<" " <<list[k].b << endl;
            }
        }
        cin >> N;
        return 0;
    }
    View Code

    总结:

    1、题意:A男和D女要想建立联系,需要中间人B和C,联系方式为A->B->C->D,其中B与A为同性,C与D为同性。给出一组A、D,求所有的B、C。

    2、首先将整个图G存下来。先找B集合,B集合为与A有联系的同性;再找C集合,C集合为与D有联系的同性。 然后根据图G,找出B与C集合中相互有联系的点,即为所求。

    3、关于如何存储图G。1)若使用临接矩阵则需要建立id到数组下标的映射,由于本题使用下标很多,这样做比较麻烦。2)还可以采用邻接表,使用二维的map,如map< map<int,int>>,mp[id1][id2] = 1表示id1与id2有联系, 或者使用map<int, int> mp[N]。 3)在别人的博客里看到了更好的办法,由于本题id都是4位纯数字,则若id1与id2有联系,将id1*10000 + id2 与id2*10000 + id1存在map<int, 随便>中,即可表示出id1与id2的关系。由此,若id是位数较少的字母+数字混合,也可以用这种办法

    4、如何存储每个人的id。使用数字存储的话,+0和-0没法区分,从而无法区别性别会出现错误。 使用string存储较好,但要注意最后一个测试点可能超时。

    5、在2中寻找B集合时,B有可能会包含A要联系的妹子D,这是不允许的,需要把D排除掉。寻找C集合同理。

  • 相关阅读:
    Java SE 第十一讲(面向对象之封装)续二
    Java SE 第二十六讲 包与导入语句剖析
    Java SE 第三十一,二,三 Java数组剖析,Java数组内存地址解析
    Java SE 第三十四,五,六讲 Array类解析及数组疑难剖析,冒泡排序,交换排序以及快速排序
    Java SE 第三十八,九,四十,四十一,四十二,三四IDE详细介绍,ArrayList源代码深入剖析,L
    java高效的获取指定的精度的double数
    C++ 字符常量
    C++ endl
    C++基本数据类型
    vs2005的treeview简单使用之无限级别菜单建立
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/9560712.html
Copyright © 2011-2022 走看看