PAT甲级1139 First Contact

最坑的是题目出现了-0,开始觉得应该没有这么狠,以为0隐含是Male就很细了。所以就全部用的int读的,结果这样死活过不去一个case
看了题解才发现是真有-0…,所以改成string后便PASS了

#include<bits/stdc++.h>

using namespace std;

unordered_map<string, set<string>> G;

typedef pair<int,int> PAII;

bool is_same_sex(int p1,int p2) {
    if(p1 == 0)
        return p2 >= 0;
    if(p2 == 0)
        return p1 >= 0;
    return (double) p1 / (double) p2 > 0.0;
}

bool is_same_sex(string p1,string p2) {
    return(p1.length() == p2.length());
}

void check(string p1, string p2) {
    vector<pair<int,int>> ans;
    
    for(auto & f1 : G[p1]) {
        if(! is_same_sex(p1, f1))
            continue;
        if(f1 == p2)
            continue;

        for(auto & f2 : G[p2]) {
            if(! is_same_sex(p2, f2))
                continue;
            if(f2 == p1)
                continue;
            if(f1 == f2 || G[f1].find(f2) == G[f1].end())
                continue;
            ans.push_back(PAII{abs(stoi(f1)), abs(stoi(f2))});
        }
    }
    
    if(ans.size() > 0){
        sort(ans.begin(), ans.end(),[&](PAII & a, PAII & b) {
            if(a.first == b.first)
                return a.second <= b.second;
            return a.first < b.first;
        }); 
        unique(ans.begin(), ans.end());
        cout << ans.size() << endl;
        for(auto & p : ans) {
            printf("%04d %04d\n", p.first, p.second);
        }
    } else {
        cout << 0 << endl;
    }
}
int main() {
    int n, n_friend;
    cin >> n >> n_friend;
    for(int i = 0; i < n_friend; ++i) {
        string a,b;
        cin >> a >> b;
        G[a].insert(b);
        G[b].insert(a);
    }
    int n_pair;
    cin >> n_pair;
    while(n_pair--) {
        string p1,p2;
        cin >> p1 >> p2;
        check(p1,p2);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值