B 1065 单身狗(散列的应用)

    技术2022-07-11  95

    题目

    思路

    这题明明写的是200ms要求,但可以暴力破解,这是我万万没想到的- -; 散列记录夫妻,hash【id】=cpid; 再用一个散列记录到场; 之后暴力破解遍历0~100001;

    测试点3:注意00000的输出,有格式要求,不能输出成0;

    AC代码

    #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int hash[100010]; memset(hash,-1,sizeof(hash)); int a,b; for(int i=0;i<n;i++){ cin>>a; cin>>b; hash[a]=b; hash[b]=a; } int man[100010]={0}; int m; cin>>m; for(int i=0;i<m;i++){ cin>>a; man[a]=1; } int cnt=0; int ans[100010]={0}; for(int i=0;i<100010;i++){ if(man[i]==1 && man[hash[i]]!=1){ ans[cnt++]=i; } } cout<<cnt<<endl; for(int i=0;i<cnt;i++) { if(i!=cnt-1) cout<<setw(5)<<setfill('0')<<ans[i]<<' '; else cout<<ans[i]; } return 0; }
    Processed: 0.012, SQL: 9