1139 First Contact (30 分)

(set)存图。

注意点

  1. A在寻找同性朋友时,需要避免找到他想要的伴侣B,所以当当前朋友就是B或者B的同性朋友就是A时舍弃该结果。
  2. 负号标记的是女生。如果用int接收,-0000和0000对于int来说都是0,无法判断出0000的性别,所以考虑用字符串接收。
const int N=1e4+10;
unordered_set<int> mp[N];
bool sex[N];
int n,m,q;

int main()
{
    cin>>n>>m;

    while(m--)
    {
        string a,b;
        cin>>a>>b;
        int ta=abs(stoi(a)),tb=abs(stoi(b));
        if(a[0] != '-') sex[ta]=true;
        if(b[0] != '-') sex[tb]=true;
        mp[ta].insert(tb);
        mp[tb].insert(ta);
    }

    cin>>q;
    while(q--)
    {
        int a,b;
        cin>>a>>b;
        a=abs(a),b=abs(b);

        vector<PII> res;
        for(auto u:mp[a])
            for(auto v:mp[b])
            {
                if(u == b || v == a) continue;
                if(sex[a] == sex[u] && sex[b] == sex[v] && mp[u].count(v))
                    res.pb({u,v});
            }

        cout<<res.size()<<endl;
        sort(res.begin(),res.end());
        for(int i=0;i<res.size();i++)
            printf("%04d %04d
",res[i].fi,res[i].se);
    }
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14519509.html