• PAT-1139. First Contact (30)


    1139. First Contact (30)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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
    

    提交代码

    这道题有两个细节:

      可能会出现-0000,0000和-0000在数值上相同,所以需要输入字符串,根据字符串长度判断正负,然后再转成数字。

      A->B,A的friend不能是B,B的friend不能是A。且A->C->D->B,C和A性别相同,D和B性别相同。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int N, M, K;
    
    int gender[10004];
    vector<int> friends[10004];
    int cnt = 0;
    struct Friends {
        int fri1, fri2;
        bool operator < (const Friends &f) const {
            if(fri1 == f.fri1) return fri2 < f.fri2;
            return fri1 < f.fri1;
        }
    } fris[10004];
    
    void dfs(int x, int y, int fx, int flag) {
        for(int i = 0; i < friends[x].size(); i++) {
            //cout<< x<< " f:"<< friends[x][i]<< "g:"<< gender[friends[x][i]]<< "gender[y]:"<< gender[y]<< "flag:"<< flag<< endl;
            if(flag == 0 && gender[x] == gender[friends[x][i]] && friends[x][i] != y) {
                dfs(friends[x][i], y, x, flag+1);
            }
            else if(flag == 1 && gender[y] == gender[friends[x][i]] && friends[x][i] != y && friends[x][i] != fx) {
                //cout<< "friends[x][i]:"<< friends[x][i]<< endl;
                dfs(friends[x][i], y, x, flag+1);
                //cout<< cnt<< ":"<< fri1[cnt]<< " "<< fri2[cnt]<< endl;
                //cnt++;
            }
            else if(flag == 2 && friends[x][i] == y) {
                fris[cnt].fri1 = fx;
                fris[cnt].fri2 = x;
                cnt++;
            }
        }
    }
    
    int strToInt(string str) {
        int x = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str[i] == '-') {
                continue;
            }
            x *= 10;
            x += str[i]-'0';
        }
        return x;
    }
    
    int main()
    {
        cin>> N>> M;
        string sx, sy;
        int x, y;
        for(int i = 0; i < M; i++) {
            //scanf("%d%d", &x, &y);
            cin>> sx>> sy;
            if(sx.length() == 5) {
                //x = -x;
                x = strToInt(sx);
                gender[x] = -1;
            }
            else {
                x = strToInt(sx);
            }
            if(sy.length() == 5) {
                //y = -y;
                y = strToInt(sy);
                gender[y] = -1;
            }
            else {
                y = strToInt(sy);
            }
            friends[x].push_back(y);
            friends[y].push_back(x);
        }
        cin >> K;
        for(int i =0; i < K; i++) {
            cnt = 0;
            scanf("%d %d", &x, &y);
            if(x < 0) {
                x = -x;
            }
            if(y < 0) {
                y = -y;
            }
            dfs(x, y, 0, 0);
            printf("%d
    ", cnt);
            sort(fris, fris+cnt);
            for(int i = 0; i < cnt; i++) {
                printf("%04d %04d
    ", fris[i].fri1, fris[i].fri2);
            }
        }
        return 0;
    }
  • 相关阅读:
    使用NUnit做单元测试(总结版)
    http、ajax调试利器:Fiddler!!!(资料汇总)
    驳《从团购网站看中国人的创新精神》
    破解迷团:在asp.net  2.0服务器上运行.net 3.5(一)
    扩充NetCMS的功能:添加{TM:Repeater}{/TM:Repeater}标签
    TFS 2010 基础配置手动升级到高级配置 安装实践
    .net 中的 StringBuilder 和 TextWriter 区别
    最大团问题
    全排列问题
    ado.net连接池
  • 原文地址:https://www.cnblogs.com/ACMessi/p/8493748.html
Copyright © 2020-2023  润新知