1139 First Contact(30 分)

1139 First Contact(30 分)
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.
题意比较好懂 同性交友了解一下??
坑的地方就是-0000这个例子 要用string搞

#include <bits/stdc++.h>
using namespace std;
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define mp(a,b) make_pair(a,b)
#define db(a) (cout<<"***"<<a<<endl)
#define pb push_back
#define maxn 99200
typedef pair<int,int> Pair;
int boy[maxn];
vector<int>v[maxn];
set<int>s[maxn];
bool diff(int a,int b){
    return boy[a]!=boy[b];
}
set<Pair> st;
int main(){
    int n,m;
    cin>>n>>m;
    FORP(i,0,maxn) boy[i]=1;//all boys
    FORP(i,1,m){
        string aa,bb;
        cin>>aa>>bb;
        int a,b;
        sscanf(aa.c_str(),"%d",&a);
        sscanf(bb.c_str(),"%d",&b);
        if(aa.size()==5) boy[abs(a)]=-1;
        if(bb.size()==5) boy[abs(b)]=-1;
        a=abs(a); b=abs(b);
        v[a].pb(b);
        s[a].insert(b);
        v[b].pb(a);
        s[b].insert(a);
    }
    int k;cin>>k;
    FORP(i,1,k){
        int a,b;
        cin>>a>>b;
        a=abs(a);
        b=abs(b);
        vector<int> fir;//第一个请求对象
        vector<int> sec;//第er个请求对象
        vector<Pair> ans;
        st.clear();
        for(int j=0;j<v[a].size();j++){
            int to=v[a][j];
            if(!diff(a,to)) fir.pb(to);
        }
        for(int j=0;j<v[b].size();j++){
            int to=v[b][j];
            if(!diff(b,to)) sec.pb(to);
        }
        for(int j=0;j<fir.size();j++){
            int to=fir[j];
            for(int q=0;q<sec.size();q++){
                int final=sec[q];
                if(s[to].find(final)!=s[to].end()&&to!=b&&final!=a){
                    st.insert(mp(to,final));
                }
            }
        }
        cout<<st.size()<<endl;
        set<Pair>::iterator It;
        for(It=st.begin();It!=st.end();++It){
            printf("%04d %04d\n",(*It).first,(*It).second );
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值